#### Archived

This topic is now archived and is closed to further replies.

# Please i need help with Orientation !!!

This topic is 6129 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Guys, my question is really easy, but i'm so dumb in math that i can't figure it out !!! i have two objects in my game -enemy and player-, i want the enemy to be drawn to the orientation of the player ... that's it ... i have the x,y,z positon of both the enemy and the player ... what i need is a function that would rotate the enemy so that when i draw it it would look like as if it was heading for the player -not moving to the right while it's face is drawn to the back- ... please help me ... maybe i was not clear enough ... what i want is that when i render the enemy -which is a tank- i want it to be drawn as if it's pointed at me ... you know what i mean ... just imagine that the tank is moving towards me ... i want to rotate it so that it's turret is directed to me ... i just want the function to calculate the amount of degrees i have to rotate it ...       |   ↓  |  /       | ------------       |   x   |  <-       | where x is the player, and the | and - and / are the enemy ... you see the oriantation .... thanks in advance... "If you don''t like something, change it. If you can''t change it, change the way you think about it!" "He who asks is a fool for five minutes, but he who does not ask remains a fool forever." "Imagination is more important than knowledge, for knowledge is limited while imagination embraces the entire world."(Einstein) "The normal teacher tells. The good teacher explains. The superior teacher demonstrates. The great teacher inspires."(William Arthur Ward) Edited by - fixxxer on January 10, 2002 4:40:01 AM

##### Share on other sites
You need at least a forward vector and up vector for the tank. In a left handed coordinate system the cross product of the Up vector and the Forward vector will give you the Right vector. Assuming Up and Forward are unit vectors, i.e. magnitude one, then all three will be unit vectors. If you take the dot product of the each of these vectors with a vector from the tank to the player you will get the coordinates of the player in relation to the tank. It is arbitrary which is x, y and z, but generally x is forward, y is up and z is right. So now you have cartesian coordinates for the player within a coordinate system where the tank is at the origin and the x, y and z axes point forward, up and right.

Given spherical coordinates (r, theta, phi) the equivalent cartesian coordinates are (r*cos(theta)*sin(phi), r*sin(theta)*sin(phi), r*cos(phi)). You need to convert the other direction which is not as easily represented. You use the property r=sqrt(x^2+y^2+z^2) to get started. That is the distance from the tank to the player if (x,y,z) is the coordinates calculated above. Now z=r*cos(phi) or cos(phi)=z/r so phi=acos(z/r). Phi ranges from 0 to pi with 0 being straight up and pi being straight down. You want a range from -pi/2 to pi/2 with -pi/2 being straight down and pi/2 being straight up. So the inclination, zeta, is zeta=pi/2-phi. Theta=atan2(y,x) and theta is the rotation needed for the turret. So you need to rotate the turret by theta and raise/lower the barrel by zeta to point exactly at the player. If you are far enough away for gravity to be a significant factor then zeta may not be all that useful.

##### Share on other sites
2 positions.
p1, p2,

You need the angle from p2 to p1.

The tank must orient its turret or top along this angle.

problem 1.

Get the x, y, z of p2-p1.

|p2.x - p1.x| = x
Repeat for y and z.

angle...you will be converting from Cartesian to Polar coords...
So you go from x,y,z to r,theta,gamma
r = sqrt(x^2+y^2+z^2)
InvTan (y/x) = theta
InvTan (y/z) = gamma //I think. I`m not sure about working with the Z axis. But r and theta are correct; I know that.

This gives you some data.
r = distance of p1 and p2 from each other.
theta = angle in the XY plane from p2 to p1.
gamma = angle in the YZ plane.

Perhaps this will start your brain sparking.

~V''lion

##### Share on other sites
Thanks a lot guys with your help, and some little thinking i was able to figure it out ... you guys rock ...

LilBudyWizer Thanks a lot man for the professional explaination ... WOW! i never thought anyone can be this much helpful and supportive ...

Thanks a million Vlion ... i appricate everything you wrote

again you guys rock

here is how it finally worked for me ...

float dx = wx_pos - player.wx_pos;
float dy = wy_pos - player.wy_pos;

if (dx < 0)
{
dx = dx * -1;
}

if (dy < 0)
{
dy = dy * -1;
}

{
cTanksceneroty = 180 + cTanksceneroty;
}

{
cTanksceneroty = 90 + cTanksceneroty;
}

{
cTanksceneroty = 270 + cTanksceneroty;
}

{
cTanksceneroty = cTanksceneroty;
}

glRotatef(cTanksceneroty,0,1.0f,0);

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632988
• Total Posts
3009737
• ### Who's Online (See full list)

There are no registered users currently online

×