IonicWind Software

Aurora Compiler => 3D Graphics => Topic started by: kryton9 on August 20, 2006, 04:21:19 PM

Title: Vectors and Matrices
Post by: kryton9 on August 20, 2006, 04:21:19 PM
Working in 3D, Vectors and Matrices are a must.

I thought I would start this topic and we can all work out and share info here.

There are tons of websites with all the info you could want about 3d math, the problem is they all show the same complicated formulas and give no real examples.

I have been hitting a solid wall in everything I have been trying to do lately, because of the lack of practical info.

I decided today to spend the next few days working on this stuff and sharing what I learn. I hope anyone who knows more about this stuff can help.

I am going to do simple vector math in console apps, once it is confirmed that what I am doing is correct, I will work on doing the same in 3d. This will be a great help for anyone in the future and for me as I need to refer to notes all the time as my memory is really bad.

If we get through that then onto matrices.

I will post the first code soon, got it started. This is the only site that cut it to the core and gives examples, I will start with these: http://forum.thegamecreators.com/?m=forum_view&t=64491&b=1
Title: Example Vectors - Creating a directional Vector from 2 positional vectors.
Post by: kryton9 on August 20, 2006, 05:09:35 PM
/*
by kryton9 8-20-2006

Using info from this site: http://forum.thegamecreators.com/?m=forum_view&t=64491&b=1

Vectors - Creating a directional Vector from 2 positional vectors.
Consider the following positional vectors:

a = (2,3,4)
b = (10,-3,6)

You first have to think of these as directional vectors from (0,0,0).
To create a directional vector you have to get to the origin and then
to your destination point. This means you have to take the minus of
one vector and add it to your destination point.

so a to b can be thought of as -a+ b. Then all you do is work it out.

so -a + b = (-2+10,-3-3,-4+6) = (8,-6,2)

so your directional vector a to b is (8,-6,2)
*/
global sub main
{

vector3 vObject,VTarget,VAnswer;
   
vObject.x = 2;
vObject.y = 3;
vObject.z = 4;
   
VTarget.x = 10;
VTarget.y = -3;
VTarget.z = 6;
   
print();
print("vObject = ("+NumToStr(vObject.x)+", "+NumToStr(vObject.y)+", "+NumToStr(vObject.z)+")");
print("vTarget = ("+NumToStr(VTarget.x)+", "+NumToStr(VTarget.y)+", "+NumToStr(VTarget.z)+")");
print();
print("-vObject + vTarget = (-2+10,-3-3,-4+6) = (8,-6,2)");
print();
print("To match the formula, we do the following");
print("in assigning the values to the vectors:");
print();
print("vObject.x = -2;");
print("vObject.y = -3;");
print("vObject.z = -4;");
vObject.x = -2;
vObject.y = -3;
vObject.z = -4;
print(); 
print("VTarget.x = 10;");
print("VTarget.y = -3;");
print("VTarget.z = 6;");
print();
VAnswer = Vec3Add(VObject,VTarget);
print("vAnswer = Vec3Add(vObject,vTarget);");
print();
print("vAnswer = ("+NumToStr(VAnswer.x)+", "+NumToStr(VAnswer.y)+", "+NumToStr(VAnswer.z)+")");
print();
print();
print("Reversing the vectors in Vec3Add, has no impact on the result.");
VAnswer = Vec3Add(VTarget,VObject);
print("vAnswer = Vec3Add(VTarget,VObject);");
print();
print("vAnswer = ("+NumToStr(VAnswer.x)+", "+NumToStr(VAnswer.y)+", "+NumToStr(VAnswer.z)+")");
print();
print();
print("If you look at the above, you realize we did a subtraction.");
print("We placed minus in front of all the elements of the vObject.");
print("We need to remove that minus now since we will use the subtract function");
print();
print("vObject.x = 2;");
print("vObject.y = 3;");
print("vObject.z = 4;");
vObject.x = 2;
vObject.y = 3;
vObject.z = 4;
print();
VAnswer = Vec3Sub(vTarget,vObject);
print("vAnswer = Vec3Sub(vTarget,vObject);");
print();
print("vAnswer = ("+NumToStr(VAnswer.x)+", "+NumToStr(VAnswer.y)+", "+NumToStr(VAnswer.z)+")");
print();
print();
print("Reversing the vectors in Vec3Sub, does impact the result.");
VAnswer = Vec3Sub(VObject,VTarget);
print("vAnswer = Vec3Sub(VObject,VTarget);");
print();
print("vAnswer = ("+NumToStr(VAnswer.x)+", "+NumToStr(VAnswer.y)+", "+NumToStr(VAnswer.z)+")  ******* This is wrong");
print();
print();
print("Make sure to scroll to the top of the page.");
while GetKey() == "";
}


Title: Vectors and Matrices 3d example 01
Post by: kryton9 on August 21, 2006, 12:27:19 AM
Made a 3D version to use the info from the first post.

I get the answer vector, but can't seem to apply it properly. Paul, or some other 3d programming guru is going to have to show how to make this work.
Look for this in the code to see where to do the magic. // <<<<<<<<<<<< How do we get it to work correctly :)

Commands:
Escape Key exit program
WSAD control camera forward/backwardÂÃ,  Left/Right
RF control camera up/down
SpaceBar reset camera
M move vector to object position
C calc answer and try applying the direction vector
B move vector back to start location
N set vector to start direction

Screenshot of what you might see.
Title: Re: Vectors and Matrices
Post by: kryton9 on August 21, 2006, 12:29:31 AM
Paul feel free to take this program and maybe make a tutorial for all those 3d math functions you have in Aurora and we can then visually see its impact and maybe be able to use them in practical applications. Thanks.

If you need any more models to help illustrate, let me know will be glad to make them.
Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on August 21, 2006, 06:48:22 AM
Kryikey Kryton!
I was impressed with the hover mech (trust me very impressed), and now this beautiful thing you have started!
Man your making me feel like I am falling behind!
Title: Re: Vectors and Matrices
Post by: sapero on August 21, 2006, 08:46:23 AM
I'm wondering why all windowed dx examples are closed only by keyboard ???
If we have a window, it should allow close from systemmenu:

class C3DScreen2:C3DScreen{declare virtual OnClose() {Destroy();}}

global sub main()
{
C3DScreen2 screen;
CDirectInput keyboard;
screen.CreateWindowed(0,0,1024,768,AWS_VISIBLE|AWS_OVERLAPPEDWINDOW|AWS_CENTERED,0,"example",NULL,false);

do (
// render here
}until (keyboard.KeyDown(DIK_ESCAPE) || !screen.IsValid());
Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on August 21, 2006, 09:23:55 AM
Good question, generally speaking all 3d games are written for full screen and the window has no menu.
Title: Re: Vectors and Matrices
Post by: kryton9 on August 21, 2006, 10:30:52 AM
Thanks Zumwalt, well you got me inspired with your great demo and now I want to get over this hump or wall that keeps cropping up all the time in working with 3D.

Sapero, I will add the system menu in the next version. You are right, if it is a window it should really have that.
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on August 21, 2006, 10:47:18 AM
Quote from: sapero on August 21, 2006, 08:46:23 AM
I'm wondering why all windowed dx examples are closed only by keyboard ???
If we have a window, it should allow close from systemmenu:

Some of them are closed by the button.  Oh wait, you don't have those examples yet ;)

Just create a derived class as you did an you can handle any Windows message in the 3D screen.
Title: Re: Vectors and Matrices
Post by: kryton9 on August 21, 2006, 11:25:11 AM
Updated the code as Sapero suggested. I also cleaned up the code a bit, but didn't solve the main problem. The new version can be downloaded at the second post, reply #2.
Title: Re: Vectors and Matrices
Post by: kryton9 on August 21, 2006, 12:52:01 PM
I tried all of these today and still no luck :(

Code Snippet:
if (input.KeyDown(DIK_C))
{
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  //vAnswer = Vec3Sub(vTarget,vObject);
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  //vSet = Vec3Normalize(vAnswer);
//svAnswer="Answer Vector: "+NumToStr(vAnswer.x,4)+" "+NumToStr(vAnswer.y,4)+" "+NumToStr(vAnswer.z,4);
//sVectorThruTarget="The Vector is not going through the Target :(";
//vector.Orient(vAnswer.x,vAnswer.y,vAnswer.z,vUpVector.x,vUpVector.y,vUpVector.z);// <<<<<<<<<<<< How do we get it to work correctly :)
//vector.Orient(vSet.x,vSet.y,vSet.z,vUpVector.x,vUpVector.y,vUpVector.z);// <<<<<<<<<<<< How do we get it to work correctly :)
//vector.rotate(vAnswer.x,vAnswer.y,vAnswer.z);
//vector.rotate(vSet.x,vSet.y,-vSet.z);
//sVectorThruTarget = NumToStr(ACOS(Vec3Dot(Vec3Normalize(vObject),Vec3Normalize(vTarget))),4);
//vector.rotate(vSet.x,ACOS(Vec3Dot(Vec3Normalize(vObject),Vec3Normalize(vTarget))),vSet.z);
//vector.Orient(vSet.x,ACOS(Vec3Dot(Vec3Normalize(vObject),Vec3Normalize(vTarget))),vSet.z,vUpVector.x,vUpVector.y,vUpVector.z);
}
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on August 21, 2006, 12:59:00 PM
Keep working on it.  I can't look at it for you for at least a few days.
Title: Re: Vectors and Matrices
Post by: kryton9 on August 21, 2006, 02:36:49 PM
Added arrow keys and numpad 4 and 6 to control the vector object and some more stats, at least something to maybe help figure this out.
Escape Key exit program
WSAD control camera forward/backward  Left/Right
RF control camera up/down
SpaceBar reset camera
M move vector to object position
C calc answer and try applying the direction vector
B move vector back to start location
N set vector to start direction

/* 3d vector example 01
8-20-2006
by kryton9 */

class MyC3DScreen:C3DScreen{declare virtual OnClose() {Destroy();}}
declare import,timeGetTime(),int;

global sub main()
{
vector3 vObject,vObjectDir,vTarget,vAnswer,vOriginal,vUpVector,vNow,vCamera,vSet,vAnswerNormalized;
ÂÃ,  ÂÃ, 
vAnswer.x = 0;
vAnswer.y = 0;
vAnswer.z = 0;

vObject.x = 2;
vObject.y = 3;
vObject.z = 4;
ÂÃ,  ÂÃ, 
vTarget.x = 10;
vTarget.y = -3;
vTarget.z = 6;

MyC3DScreen screen;
C3DCamera camera;
C3DObject scene;
C3DLight light;
CDirectInput input;

C3DMesh axis,object,target,vector;

int TextColor = RGBA(255,100,0,200);
float fRate=1;


float fadjust,fTarget,fCamLR,fCamUD,fCamFB,vectorPitch,vectorYaw;
fCamLR=17.0f;fCamUD=5.0f;fCamFB=-35.0f;
fTarget = 1.0f / /*FPS*/60.0f * 1000.0f;
fAdjust = 1.0f;vectorPitch = 0.0f;vectorYaw = 0.0f;vectorRoll=0.0f;

string sWinTitle = "Vectors Example 01";string svAnswer="";string sVectorThruTarget="";string svAnswerNormalized ="";
screen.CreateWindowed(0,0,1024,768,AWS_CAPTION|AWS_VISIBLE|AWS_OVERLAPPEDWINDOW|AWS_SIZE|AWS_CENTERED,0,sWinTitle,NULL,false);

input.Init(screen);
camera.Create(screen);
camera.Position(5,5,-50);
camera.SetBackPlane(10000);


screen.Clear(RGBA(0,0,0,255));
screen.BeginScene(camera);
screen.RenderText(0,10,"Loading objects...",TextColor);
screen.RenderScene();

ÂÃ,  ÂÃ,  light.Create(screen,LIGHT_POINT,1);
light.SetAttenuation(0,1/200.0,0);
light.SetSpecular(.5,.5,.5,1);
light.SetAmbient (.7,.7,.7,1);
light.Position(0,150,-300);

//axis,object,target,vector
axis.LoadSkinnedX(screen,GetStartPath()ÂÃ,  ÂÃ, +ÂÃ,  "k9 3d axis.X");
object.LoadSkinnedX(screen,GetStartPath() +"k9 3d object.X");
target.LoadSkinnedX(screen,GetStartPath() +"k9 3d target.X");
vector.LoadSkinnedX(screen,GetStartPath() +"k9 3d vector.X");

scene.CreateScene(screen);vUpVector = camera.GetUpVector();
scene.AddChild(light);
scene.AddChild(axis); axis.position(0,0,0);
scene.AddChild(object); object.Position(2,3,4);
scene.AddChild(target); target.Position(10,-3,5);
scene.AddChild(vector); vector.Position(-5,-10,5);vOriginal = vector.getDirection();


int fps = 0;
int startTime;
do
{
// Camera Controls
if input.KeyDown(DIK_W){fCamFB += 0.5*fRate;} // move camera forwards
if input.KeyDown(DIK_S){fCamFB -= 0.5*fRate;} // move camera backwards
if input.KeyDown(DIK_D){fCamLR += 0.5*fRate;} // move camera right
if input.KeyDown(DIK_A){fCamLR -= 0.5*fRate;} // move camera left
if input.KeyDown(DIK_R){fCamUD += 0.5*fRate;} // move camera up
if input.KeyDown(DIK_F){fCamUD -= 0.5*fRate;} //move camera down

if input.KeyDown(DIK_SPACE){fCamLR=17.0f;fCamUD=5.0f;fCamFB=-35.0f;} //reset camera

if input.KeyDown(DIK_M)//move vector to object location
{
vector.Position(2,3,4);vObjectDir = object.GetDirection();
fCamLR=23.0f;fCamUD=7.5f;fCamFB=-58.0f;// adjust camera to see better
}
if (input.KeyDown(DIK_B)){ vector.position(-5,-10,5);} //move vector to start location
if (input.KeyDown(DIK_N)){ vector.orient(vOriginal.x,vOriginal.y,vOriginal.z,vUpVector.x,vUpVector.y,vUpVector.z);} //restore back original vector direction

if (input.KeyDown(DIK_C))
{
vAnswer = Vec3Sub(vTarget,vObject);
vAnswerNormalized = Vec3Normalize(vAnswer);
svAnswerNormalized = "Answer Vector Normalized: "+NumToStr(vAnswerNormalized.x,4)+" "+NumToStr(vAnswerNormalized.y,4)+" "+NumToStr(vAnswerNormalized.z,4);
//vSet = Vec3Normalize(vAnswer);
svAnswer="Answer Vector: "+NumToStr(vAnswer.x,4)+" "+NumToStr(vAnswer.y,4)+" "+NumToStr(vAnswer.z,4);
sVectorThruTarget="The Vector is not going through the Target :(";
vectorPitch = 0.0f;vectorYaw =0.0f;vectorRoll = 0.0f;
//vector.Orient(vAnswer.x,vAnswer.y,vAnswer.z,vUpVector.x,vUpVector.y,vUpVector.z);// <<<<<<<<<<<< How do we get it to work correctly :)
//vector.Orient(vSet.x,vSet.y,vSet.z,vUpVector.x,vUpVector.y,vUpVector.z);// <<<<<<<<<<<< How do we get it to work correctly :)
vector.rotate(vAnswer.x,vAnswer.y,vAnswer.z);
//vector.rotate(vAnswerNormalized.x+(vNow.x+vAnswerNormalized.x),vAnswerNormalized.y+(vNow.y+vAnswerNormalized.y),vAnswerNormalized.z+(vNow.z+vAnswerNormalized.z));
//sVectorThruTarget = NumToStr(ACOS(Vec3Dot(Vec3Normalize(vObject),Vec3Normalize(vTarget))),4);
//vector.rotate(vSet.x,ACOS(Vec3Dot(Vec3Normalize(vObject),Vec3Normalize(vTarget))),vSet.z);
//vector.Orient(vSet.x,ACOS(Vec3Dot(Vec3Normalize(vObject),Vec3Normalize(vTarget))),vSet.z,vUpVector.x,vUpVector.y,vUpVector.z);
}
// Vector Object Controls
IF input.KeyDown(DIK_NUMPAD4)
{ vectorRoll += 0.01*fRate;vector.Rotate(vAnswer.x+vectorPitch,vAnswer.y+vectorYaw,vAnswer.z+vectorRoll);}
IF input.KeyDown(DIK_NUMPAD6)
{ vectorRoll -= 0.01*fRate;vector.Rotate(vAnswer.x+vectorPitch,vAnswer.y+vectorYaw,vAnswer.z+vectorRoll);}
IF input.KeyDown(DIK_RIGHT)
{ vectorYaw -= 0.01*fRate;vector.Rotate(vAnswer.x+vectorPitch,vAnswer.y+vectorYaw,vAnswer.z+vectorRoll);}
IF input.KeyDown(DIK_LEFT)
{ vectorYaw += 0.01*fRate;vector.Rotate(vAnswer.x+vectorPitch,vAnswer.y+vectorYaw,vAnswer.z+vectorRoll);}
IF (input.KeyDown(DIK_UP))
{ÂÃ,  ÂÃ, vectorPitch += 0.01*fRate;vector.Rotate(vAnswer.x+vectorPitch,vAnswer.y+vectorYaw,vAnswer.z+vectorRoll);}
IF (input.KeyDown(DIK_DOWN))
{ vectorPitch -= 0.01*fRate;vector.Rotate(vAnswer.x+vectorPitch,vAnswer.y+vectorYaw,vAnswer.z+vectorRoll);}

camera.Position (fCamLR,fCamUD,fCamFB);
vCamera= camera.GetPosition();vNow = vector.getDirection();

startTime = timeGetTime();
screen.Clear(RGBA(0,0,0,255));
screen.BeginScene(camera);

scene.Draw();
y=0;s=14;
screen.RenderText(5,y,"FPS: "+NumToStr(fps),TextColor);y+=s;
screen.RenderText(5,y,"",TextColor);y+=s;

screen.RenderText(5,y,"CameraX: "+NumToStr(vCamera.x,1),TextColor);y+=s;
screen.RenderText(5,y,"CameraY: "+NumToStr(vCamera.y,1),TextColor);y+=s;
screen.RenderText(5,y,"CameraZ: "+NumToStr(vCamera.z,1),TextColor);y+=s;
screen.RenderText(5,y,"",TextColor);y+=s;

screen.RenderText(5,y,"vectorPitch: "+NumToStr(vectorPitch,4),TextColor);y+=s;
screen.RenderText(5,y,"vectorYaw: "+NumToStr(vectorYaw,4),TextColor);y+=s;
screen.RenderText(5,y,"vectorRoll: "+NumToStr(vectorRoll,4),TextColor);y+=s;
screen.RenderText(5,y,"",TextColor);y+=s;

screen.RenderText(5,y,"Object Direction Vector: "+NumToStr(vObjectDir.x,4)+" "+NumToStr(vObjectDir.y,4)+" "+NumToStr(vObjectDir.z,4),TextColor);y+=s;
screen.RenderText(5,y,"",TextColor);y+=s;

screen.RenderText(5,y,"Vector Original Direction: "+NumToStr(vOriginal.x,4)+" "+NumToStr(vOriginal.y,4)+" "+NumToStr(vOriginal.z,4),TextColor);y+=s;
screen.RenderText(5,y,"",TextColor);y+=s;

screen.RenderText(5,y,svAnswer,TextColor);y+=s;
screen.RenderText(5,y,"",TextColor);y+=s;

screen.RenderText(5,y,svAnswerNormalized,TextColor);y+=s;
screen.RenderText(5,y,"",TextColor);y+=s;

screen.RenderText(5,y,"Vector Now Direction: "+NumToStr(vNow.x,4)+" "+NumToStr(vNow.y,4)+" "+NumToStr(vNow.z,4),TextColor);y+=s;
screen.RenderText(5,y,"",TextColor);y+=s;

screen.RenderText(5,y,sVectorThruTarget,TextColor);y+=s;
screen.RenderText(5,y,"",TextColor);y+=s;
screen.RenderText(5,y,"vNow-vAnswerNormalized: "+NumToStr(vNow.x-vAnswerNormalized.x,4)+" "+NumToStr(vNow.y-vAnswerNormalized.y,4)+" "+NumToStr(vNow.z-vAnswerNormalized.z,4),TextColor);y+=s;
screen.RenderText(5,y,"",TextColor);y+=s;


fps = screen.RenderScene();
fAdjust = (timeGetTime() - startTime) / fTarget;
}
until (input.KeyDown(DIK_ESCAPE) || !screen.IsValid());
scene.Free();screen.CloseScreen();
}

Title: Re: Vectors and Matrices
Post by: John S on August 21, 2006, 10:50:57 PM
very nice work
Title: Re: Vectors and Matrices
Post by: kryton9 on August 21, 2006, 10:56:02 PM
Thanks hopefully will get it working sometime.
If anyone has ideas for using this setup and needs extra models let me know. I can make what you need if it will help us figure this stuff out!!
Title: Playing with the new lookat for objects command
Post by: kryton9 on September 02, 2006, 02:50:08 AM
I modified my previous program to try out the new lookat command. Strange things happen, not only in where it points, but also the arrow at times disappears and other times is all distorted.

Commands:
WSADRF move the camera, the camera always looks at 0,0,0
The arrow keys as well as NumPad 4 and 6 let you spin the arrow around
0-8 move the location of the arrow to one of the points in 3d space
F1-F9 select the coordinates to look at
SpaceBar resets the camera

When the arrow dissapears or becomes really distorted. Just hit 0 to move it back to 0,0,0 and then try the different F keys till it becomes normal again. Sometimes hitting the arrow keys
brings it back too.

Still fun to play with and you can use it to do your tests with other 3d commands and use this as  a foundation. As you can see by all the commented lines, I have been trying to figure stuff out like crazy and not getting much luck :)

Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on September 02, 2006, 03:13:28 AM
When your playing with the LookAt method remember that it is just a point in 3D space.  If that point is very close, or inside your mesh, then you'll have strange results.

For example if you have a cube that is 100x100x100 postioned at 0,0,0 and you tell it to look at 0,0,50 then the look at point is in the middle of your mesh, in which case it may flip in the wrong direction.  Draw some lines on paper to help visualize it.

The LookAt method rotates the mesh so that an imaginary vector pointing straight out from the face lines up with the point, it uses the same code for the C2DCamera::LookAt method. 

When you want to look at another mesh, and that mesh is very close, look at a point beyond that mesh as if the vector penetrates the target to reach that point.  In other words scale the point to a larger value.
Title: Re: Vectors and Matrices
Post by: Kale on September 02, 2006, 07:03:32 AM
To learn about Vectors and Matrices try this book:

3D Math Primer for Graphics and Game Development (http://www.amazon.co.uk/exec/obidos/ASIN/1556229119/026-1016728-4355657?%5Fencoding=UTF8)

Very easy to follow and lots of good examples.
Title: Re: Vectors and Matrices
Post by: Barney on September 02, 2006, 08:47:25 AM
This is a good place to visit regarding game programming. It's in German, but there's some good info in English for those who don't understand German. Look at the left part of the screen and click on Mathlib link.

http://www.zfx.info/

Barney
Title: Re: Vectors and Matrices
Post by: kryton9 on September 02, 2006, 11:32:16 AM
Thanks for the links guys.

The LookAt is not looking where it should it seems, it works fine for the camera, but for one object to look at another something is not right?
Try the program out to see.
Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on September 02, 2006, 12:00:21 PM
Its not that simple.
It doesn't reset.

I am standing in a room looking out a window, I don't move, the mailbox doesn't move, so therefor the lookat just worked.
I move, go downstairs, open the front door, and I look out at the same mailbox.
My initial view hasn't changed, its as if I am still up in the room, looking out the window, at the mailbox, but my orientation to the mailbox is different, my position is different, everything but the mailbox location is different.

Download my tank game for an example of the problem.
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on September 02, 2006, 12:04:31 PM
Works fine here ;).  I'll write a demo for you this weekend, I've tried to give you clues in the posts I've made but you need more it seems.  The mesh itself has to be designed to face in the correct location when it is initially loaded, otherwise the LookAt command is meaningless.

LookAt with a mesh will have limited use if your using them with characters that are meant to walk or roll like a tank.  Only because the lookat vector will cause the mesh to rotate on all three axis from the center point.  Which for a camera is normal.  But a person walking on a game board would look kind of strange looking at a point on the ground, since it would rotate the mesh on the X axis.
Title: Re: Vectors and Matrices
Post by: kryton9 on September 02, 2006, 12:13:03 PM
Paul can you use the models and program I made to show where I am going wrong. Thanks Will save you time instead of writing something from scratch.

Just to show how what I am trying to do to make it work. Thanks will help immensly. Paul, I made all my models to face correctly from my modeler. That is if you see, I use no rotations or transforms. I write this because I know you wrote to me about the mesh in creation and facing direction.

The version to use is in reply # 15.
Title: Re: Vectors and Matrices
Post by: kryton9 on September 02, 2006, 12:16:03 PM
Quote from: zumwalt on September 02, 2006, 12:00:21 PM
Its not that simple.
It doesn't reset.

I am standing in a room looking out a window, I don't move, the mailbox doesn't move, so therefor the lookat just worked.
I move, go downstairs, open the front door, and I look out at the same mailbox.
My initial view hasn't changed, its as if I am still up in the room, looking out the window, at the mailbox, but my orientation to the mailbox is different, my position is different, everything but the mailbox location is different.

Download my tank game for an example of the problem.

Even if I don't move the arrow at all and tell it took at a direction it doesn't seem to point in the right direction.

I can understand about what you are saying, but like I said, even the default first lookat from the location o,o,o doesn't seem to work the way I though it would.
Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on September 02, 2006, 12:27:16 PM
Paul, I Ignore the Y value on my LookAt call.
As I stated, it does look at the correct target in the DO look on first pass.
This is shown on ÂÃ, my tank.

Now, here is the hierarchy:
Tank body, has tank turret as a child, tank turret has a trans as a child, of that trans, I have a machine gun as a child.
When the gun loads, it loads facing the same direction the barrel faces, it faces the same direction the body faces.
Everything is facing the same direction when it loads.

Now comes the loop with positioning:

tX=midTarget.x;
tY=midTarget.y;
tZ=midTarget.z;

trans.lookat(tX,0.0f,tZ);



midTarget is NEW on every do loop cycle.
Please, download my tank game and look at the red sphere between the tank and the mech.
At all times, the gun should face the red ball.
tX has a new value, tZ has a new value coordinates

Add this to the code to see...


s.RenderText(8,178,"tX: "+NumToStr(tX),RGBA(255,255,0,255));
s.RenderText(8,188,"tY: "+NumToStr(tY),RGBA(255,255,0,255));
s.RenderText(8,198,"tZ: "+NumToStr(tZ),RGBA(255,255,0,255));


So that all said, the lookat is not taking any NEW coordinates once it is called the first time for me.
Anxiously waiting the code to see what on earth I am doing wrong.
Thanks Paul :)
Title: Re: Vectors and Matrices
Post by: kryton9 on September 02, 2006, 12:31:42 PM
Got it working. I had to redo my arrow.ÂÃ,  As I wrote my last reply, it dawned on me that I had the arrow pointing up instead of down the z axis in the positive direction :)

Here is the updated arrow model. It is named k9 3d vector.XÂÃ,  , once unzipped that is.

With this overiding the version of the model in reply # 15, it all works.

Just press F1-F8 to see it go right through the points :)

Note:ÂÃ,  If you select point 6 let say to move to. Do not select F6 to look at, that is it will try to look at itself and from what Paul wrote causes problems, as it should. So what ever point you select 1-8 to move to, just don't select the corresponding F# to look at. It works fine all the time now, woo hoo!!

What all this means: We have to make our models in our modeling software face the positive z direction. This means when loaded into Aurora, by default it should be facing away from us and into the screen. Hope that helps.

Download the zip file in reply #15, then once that is unzipped. Download and unzip k9 arrow.zip to overwrite the older model. Enjoy!!
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on September 02, 2006, 01:13:02 PM
Now you got it ;)

Zumwalt... also when using a LookAt point remember that the point has to be in world space, not local space.  So make sure the bWorld parameter of a meshes GetPosition is TRUE.  In other words the mesh your trying to look at. 
Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on September 02, 2006, 01:27:23 PM
mechPOS = m1.getPosition(true);

Didn't change anything, still hammering on it, going to go get food, I am missing something.
Not even with Krytons new mesh does it make a difference, for me at the moment.
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on September 02, 2006, 01:54:23 PM
OK the problem your having is that the LookAt command doesn't take into account any translations done by parent objects.  I'll have to think on that one for a moment.  It was designed to work from top level object to top level object.
Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on September 02, 2006, 04:03:38 PM
Thanks Paul.
Just about everything in the 3d world will require an equal on the translation level and not just the top level.
Makes things tougher to figure out initially but good in the long run.
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on September 02, 2006, 04:13:45 PM
Again it was adapted from the Camera LookAt method, and a camera is always top level, so give me a bit of slack ;)

I'll have some solution for it before the next update.  I have to work backwords though the matrices to convert a world space lookat point to a local one for the 'looker', while respecting a possible infinate number of parent world transforms. So it's not a quicky fix.

Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on September 02, 2006, 05:06:32 PM
No rush, I am going through a book I bought on Beginning Math and Physics for Game Programmers.
Its lacking though.
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on September 03, 2006, 01:23:13 AM
Actually the solution was easy, just not enough coffee at the time.  Just a simple matter of converting world space to local space using the inverse of the parent(s) transformation matrix. 

I'll have an update tomorrow probably.
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on September 03, 2006, 01:56:13 AM
And you also need an adjustment to your code as well after the update.

      tX=midTarget.x;
      tY=midTarget.y;
      tZ=midTarget.z;
      trans.lookat(tX,0.0f,tZ);

Would be wrong since it is not a Y value of 0.0(world) you want to look at, but the current Y value of the transformation object in world cooridnates.  Which allows the machine gun to rotate without changing it's current Y position.  So I changed it to:

      kryton9POS = km.GetPosition(0);
      VECTOR3 transPosWorld = trans.getposition(1);

......
      tX=midTarget.x;
      tY=midTarget.y;
      tZ=midTarget.z;
      trans.lookat(tX,TransPosWorld.y,tZ);

And I can now drive the tank everywhere and the machine gun rotates to follow the correct location.

Paul.
Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on September 03, 2006, 10:15:44 AM
Thanks a ton Paul, you are definately the man!
Can't wait for this patch.
Title: Re: Vectors and Matrices
Post by: kryton9 on September 03, 2006, 12:30:43 PM
That will be cool to see. Great!
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on September 03, 2006, 07:55:47 PM
Just in case you missed it zumwalt, the update was posted today ;)
Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on September 03, 2006, 08:05:25 PM
Thanks I just got it like a few minutes ago and updated the code, the latest download build has it encorporated.
I took my final on my CCSI exam today, won't get results for another 10 days, but that was an all day event.
Thanks a ton for doing this.
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on September 03, 2006, 08:07:14 PM
How do feel you did on the exam? 
Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on September 03, 2006, 08:10:19 PM
Not so good, hopefully either a C or a B, no way I got an A, but it cost me 7 hours of my life :(
I got stuck on the 3 major investigative tools an agent uses when investigating malicious software.
The book didn't describe them, nor was it in the lectures.
I'll talk to the professor about it tomorrow hopefully.
I have 4 more of those CCSI type classes left before I qualify for any FBI classes.
Title: Re: Vectors and Matrices
Post by: Ionic Wind Support Team on September 03, 2006, 08:15:35 PM
I've had a few exams like that.  Tend to suck the life out of you for a couple of days ;)
Title: Re: Vectors and Matrices
Post by: J B Wood (Zumwalt) on September 03, 2006, 08:17:55 PM
It will definately ruin my holiday if I think about it to much, going to not worry about it now, I had a 94% in the class going into the final, with an ungraded 12 page report, the report is worth 20% of my grade, the final worth 25% of my grade, between the two, they can take my A to a F.