Advertisement

steering objects

Started by October 29, 2000 08:48 AM
20 comments, last by baldurk 24 years ago
normals tell you which way the polygon is facing. they are used a lot in bsp based engines. also, you better learn the math before you try to understand 3d programming. it''ll just get more confusing.

JoeMont001@aol.com www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
ok kiddo, I gotta give you respect for being 13 and programming. Mind you you must learn what normals are. At very least you can impress the girls in high-school with your uncanny mathmatical prowess.

Imagine your triangle or polygon is made of card. If you were to stick the eraser end of a pencil with crazy glue so that it stuck straight up off the card ( perpendicular ), pointing away from it, that would be a "normal" to the triangle. You''ve probably heard people describe this as a normal to the plane, all that means is the pencil is the normal and the card is the plane.

If you need some code I suggest you checkout my racing game for the code to calculate normals, use them and also "reduce them to units". I''m not going to go into the reduction as it''d just confuse things.

Later,
fs

http://www.CornflakeZone.com
//-- Modelling and animation in every dimension --//
Advertisement
Hi baldurk!

So you''re wanting to move the object around like, let''s say quake or a racing game?

What you want to do is when you hit a key it''ll increase the y rotation (turn it) or make it go forward.

So let''s say you hit the right key

To make it spin right you''d do something like this
yrot--;

or left

yrot++;

Now to make it go forward you''d do something like this when you hit the up key.

x += (float)sin(yrot * (pi/180) ) * 1;
z -= (float)cos(yrot * (pi/180) ) * 1;

To make it go backward you''d simply change the +''s to -''s and the -''s to +''s.

Now that you have the x, z, and y rotation values you can now use these to rotate and move your object.

If you read tutorial 10 or 23, it''ll show you how to do this
Probably in better explanation than I can give you right now :p

Don''t pay attention to these guys right now, they''re being way too complicated for you right now, but sooner or later you''ll have to do it like that. This is the easy way

Hope that helps!

Don''t give up, I''m only 14...when I was first learning this stuff it was kinda hard. Now it''s a peice of cake!

Good luck to you and your project!






~-=-=-=-=-=-=~
~Justin Eslinger~
~.."BlackScar"..~
~-=-=-=-=-=-=~
~-=-=-=-=-=-=~~Justin Eslinger~~.."BlackScar"..~~-=-=-=-=-=-=~
thanks for all the replies to my question!

I now understand (vaguely) what normals are.

wait a minute, Ill be back later the school bell just rung
You can learn anything if you put your mind to it. I''m 14. When I was 12 I programmed a wireframe 3D engine in DJGPP with no libraries with correct matrix transformations. Only now I''m starting OpenGL, and working on a game:
Tank Wars 3D

Oh, and fshana: your right about the chicks going for the math kids
You go Wolfman!!! Wow I''m pretty impressed by you guys being so young and all. I feel so ancient. Keep up the great work !
fs

http://www.CornflakeZone.com
//-- Modelling and animation in every dimension --//
Advertisement
btw, fshana, how do you know I am a boy?



anyway I now have a simple tank moving around, I fixed the bug without normals.

My next question is: how do I use normals in OpenGL?

don`t worry about feeling old, I hardly know anything about 3d programming.

btw I am going to upload (in the next few minutes) my simple tank program to:
http://members.xoom.com/baldurk/3d/tank.exe

and don`t worry, its less than 200kb
the keys are forward,back,left and right to steer then f1 to change to window mode (or of course alt+enter) and r to reset if you drive off the screen, although that is beta quality
Normals are only good for lighting. I think if it''s driving you want you should check out my code see the link below. Anyway here''s the low-down on normals...

For each face or triangle you do something like this:

// Make a silly struct
Struct Point3D {
float x,y,z ;
} Point3D

// Declare some stuff....
Point3D Pointa, Pointb, Pointc, out;

glBegin(GL_TRIANGLES);

glNormal( out.x , out.y, out.z );

glVertex( Pointa.x ,Pointa.y, Pointa.z );
glVertex( Pointb.x,Pointb.y, Pointb.z );
glVertex( Pointc.x,Pointc.y, Pointc.z );

You''re problem is what''s out.x, out.y, out.z. Easy,

Use these simple functions, calling CalcNormal passing in a, b and c and getting back out filled with the normal coordinates of the plane a,b,c

void GLCalcNormal(Point3D a, Point3D b, Point3D c, Point3D *out)
{
Point3D v1,v2;

// Calculate two vectors from the three points
v1.x = a.x - b.x;
v1.y = a.y - b.y;
v1.z = a.z - b.z;

v2.x = b.x - c.x;
v2.y = b.y - c.y;
v2.z = b.z - c.z;

// Take the cross product of the two vectors to get
// the normal vector which will be stored in out
out->x = v1.y * v2.z - v1.z * v2.y;
out->y = v1.z * v2.x - v1.x * v2.z;
out->z = v1.x * v2.y - v1.y * v2.x;

// Normalize the vector (shorten length to one)
GLReduceToUnit(out);
}

void GLElement::GLReduceToUnit(Point3D *vector)
{
float length;

// Calculate the length of the vector
length = (float)sqrt( (vector->x * vector->x ) +
(vector->y * vector->y ) +
(vector->z * vector->z ) );

// Keep the program from blowing up by providing an exceptable
// value for vectors that may calculated too close to zero.
if (length == 0.0f) length = 1.0f;

// Dividing each element by the length will result in a
// unit normal vector.
vector->x /= length;
vector->y /= length;
vector->z /= length;
}


I''ll say it one more time though, if it''s DRIVING code you want, get this code , it''ll show give you a framework to build on and you don''t need normals.

http://www.cornflakezone.com/download/2dracing.zip





http://www.CornflakeZone.com
//-- Modelling and animation in every dimension --//
You don''t need to use normals, but you should learn what they are anyway.

Anyway, to drive something around you need to keep track of a couple of things: It''s position and orientation. Let''s start simple, and say your working in 2D. You can still render in 3D but the tank will drive around on a plane.

Now, The position you store with two values:
    float posx;float posy;[/source]The rotation you can store with an angle between 0 and 360:float rotation;Now when the program starts, initialize the variables. Then you check keypresses. If the player presses LEFT or RIGHT then you either add or subract from the rotation variable. When the player presses UP you need to move the tank forward. How do you do that? I''ll just give you the equations:[source]posx = posx + cos(rotation*PI/180)*speed;posy = posy + sin(rotation*PI/180)*speed;    


If you need more help post

This topic is closed to new replies.

Advertisement