Skip to main content
GameDev.net gamedev.net
🔒 Locked

Math wizards.. vector facing another vector..

Started by _Kami_ Aug 3, 2003 at 6:42 PM 9 replies 1.4k views
Original Post
_Kami_
_Kami_
My trigonometry sucks lets say i have drawn a spaceship at v1(0,0,0) (in my view the spaceship will always face straigth into the screen btw (away from view)) i want it to face v2(x,y,z) whats the formula for this again..
www.ageofconan.com
BGCJR
BGCJR
the same vector increased along the line of sight to the distance you want the ship.


[edited by - BGCJR on August 3, 2003 8:33:24 PM]
Game Core
_Kami_
_Kami_
i dont understand..
if i want it to face v2 i need to rotate the spaceship around x,y and z axis.. i want to find out what kind of rotation i need to to.
www.ageofconan.com
DuncanBojangles
DuncanBojangles
Do you know the magnitude of the first and second vector? If so, just normalize both vectors, then to find the angle to rotate the spaceship by to face the second vector, use this:
(world_vector.x - spaceship_vector.x) * 90 = angle to turn on x
(world_vector.y - spaceship_vector.y) * 90 = angle to turn on y
(world_vector.z - spaceship_vector.z) * 90 = angle to turn on z
(in degrees)

And that''s it. If you want to know how to move that spaceship to the second vector a certain distance, just use this formula. You only need two axis angles, since the third can be figured out using the magnitude.


const float PIOVER180 = (3.1415/180);

struct vector
{
float x, y, z, magnitude;
}

vector transformation(float magnitude, float angle_from_x_axis, float angle_from_y_axis)
{
vector the_vector;
float b;
b = speed * cos((90 - float angle_from_x_axis) * PIOVER180);
the_vector.x = b * sin(angle_from_y_axis * PIOVER180);
the_vector.y = speed * cos(float angle_from_x_axis * PIOVER180);
the_vector.z = b * cos(angle_from_y_axis * PIOVER180);
the_vector.magnitude = sqrt((the_vector.x * the_vector.x) + (the_vector.z * the_vector.z) + (the_vector.y * the_vector.y));
return the_vector;
}



"Donkey, if it were me, you''d be dead."
I cna ytpe 300 wrods pre mniute.
"Donkey, if it were me, you'd be dead."I cna ytpe 300 wrods pre mniute.
_Kami_
_Kami_
ok, now i feel really dumb.
my new snippet...

vect normShip = (vstart+helper).normalize();

vect normEarth = earthVect;
normEarth.normalize();

GLfloat xrot = (normEarth.x-normShip.x)*90.0f;
GLfloat yrot = (normEarth.y-normShip.y)*90.0f;
GLfloat zrot = (normEarth.z-normShip.z)*90.0f;
glRotatef(xrot,1.0f,0.0f,0.0f);// Rotate On The X Axis
glRotatef(yrot,0.0f,1.0f,0.0f);// Rotate On The y Axis
glRotatef(zrot,0.0f,0.0f,1.0f);// Rotate On The z Axis
drawShip();

i think i misunderstood you, cause this aint working.

the ship is supposed to face earth.. but :/
www.ageofconan.com
varokasp
varokasp
I don't know much about 3d programming, but I think only two angles are needed when you wanna lookat something. You need to know how many degrees to the left(or right) and how many up(or down).
To be exact,
1. how many degrees around z-axis
2. how many degrees around y-axis (or x-axis)
and there's this thing called spherical coordinates which can helps you, I can't recall it right now. You can look it up in most of calculus textbook

[edited by - varokasp on August 4, 2003 10:50:22 PM]
DuncanBojangles
DuncanBojangles
Okay, so you are not moving the ship? Or not trying to find its coordinates in 3d space? Whatever, that's what the second half of my post was about. Firstly, when you do your rotations, do the Z, then Y, then X. It really matters. If you don't do it this way, things aren't what you expected them to be. What is the vector you are using for the Earth? Because the ship, using the math I showed, will orient itself to the vector of the Earth. So if the Earth is pointing towards the screen, the spaceship will point toward the screen. Give me a while to come up with the math on this one, working in 3d coordinates was never my strong point.

"Donkey, if it were me, you'd be dead."
I cna ytpe 300 wrods pre mniute.

edit:
Okay, one minute later and the brain has figured it out, with a liitle help from Mountain Dew. Use points to describe the location of the Spaceship and Earth, and a vector to describe the Spaceship's direction. To find the angle between the spaceship and the Earth, use this. Basically, set it up, and find a vector that describes the direction from the spaceship point to the earth point, then use the code I showed you earlier to match two vectors; spaceship to spaceship-earth vector, and voila! Earth at 0,0,0 spaceship at 10,10,10 with vector 0,10,0 magnitude 10.

edit two:
commented code.


struct point
{
float x,y,z;
};//the point structure

vector spaceship, spaceship_earth;//spaceship is spaceship's vector, the second one is the vector describing the relation between the spaceship's position and the earth's position.

spaceship.x = 0;
spaceship.y = 10;
spaceship.z = 0;
spaceship.magnitude = 10;
float angle_to_turn_on_x, angle_to_turn_on_y, angle_to_turn_on_z;
vector spaceship_normal = spaceship.normalize();//normalize it to work with the later code

point spaceship_point, earth;
spaceship_point.x = 10;
spaceship_point.y = 10;
spaceship_point.z = 10;
earth.x = 0;
earth.y = 0;
earth.z = 0;
spaceship_earth.x = spaceship_point.x - earth.x;
spaceship_earth.y = spaceship_point.y - earth.y;
spaceship_earth.z = spaceship_point.z - earth.z;
spaceship_earth.normalize();
//that is the vector describing how the spaceship should point to the earth

angle_to_turn_on_x = (spaceship_earth.x - spaceship_normal.x) * 90;
angle_to_turn_on_y = (spaceship_earth.y - spaceship_normal.y) * 90;
angle_to_turn_on_z = (spaceship_earth.z - spaceship_normal.z) * 90;
//this is the code from the earlier post that makes the second vector face the first vector


glPushMatrix();
glTranslatef(spaceship_point.x, spaceship_point.y, spaceship_point.z);
glRotatef(angle_to_turn_on_z, 0.0, 0.0, 1.0);
glRotatef(angle_to_turn_on_y, 0.0, 1.0, 0.0);
glRotatef(angle_to_turn_on_x, 1.0, 0.0, 0.0);
//draw ship here

glPopMatrix();


I hope this helps, cause I love math but explaining it can be hard. If you have any questions, just post again and I'll be more than happy to help.

[edited by - duncanbojangles on August 4, 2003 12:12:12 AM]

edit three:
Do you just want the camera to face Earth? Or do you wnat to show the spaceship in third-person and orient the spaceship correctly?

[edited by - duncanbojangles on August 4, 2003 12:20:07 AM]
"Donkey, if it were me, you'd be dead."I cna ytpe 300 wrods pre mniute.
_Kami_
_Kami_
Ok, thanks for th great reply

When i went to bed yesterday - frustrated as i was.. i realized i was mixing up vectors and vertexes!!!

No wonder your code did not work

i have a ship at p1, and earth at p2

lets say i draw earth in the center of the screen.

If i move the ship around in a circle i would like the nose of the ship to point towards earth at all times.

The sceen is showed in 3''rd person, so camera is at z=4 or something.

Sorry again for asking my question in such a stupid way .. :D
www.ageofconan.com
DuncanBojangles
DuncanBojangles
Using the code I posted should work, it is just making a vector from the two points and matching the spaceship''s vector to that vector. Simple, really. That way, wherever you move your spaceship, the nose will always be pointed towards Earth. If you need anymore math questions answered, just post again, I''m sure I''ll be able to come up with something.

"Donkey, if it were me, you''d be dead."
I cna ytpe 300 wrods pre mniute.
"Donkey, if it were me, you'd be dead."I cna ytpe 300 wrods pre mniute.
_Kami_
_Kami_
yup, tried it out last night and it worked like a charm

thanks a lot!
www.ageofconan.com
DuncanBojangles
DuncanBojangles
That fills me with so much pride it hurts! Thanks for asking the question, it really gave the brain a good workout.
"Donkey, if it were me, you'd be dead."I cna ytpe 300 wrods pre mniute.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.