• ### Announcements

#### Archived

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

# Dot product problem!

## Recommended Posts

Cybertron    122
I have a ship that rotates on all axes using yaw/pitch/roll. The turret fires projectiles using vectors for movement. I also have a vector of the same magnitude of movement straight ahead of the ship. I calculate the angle between these 2 lines to get the angle the turret is pointing. I have 3 lines that show the arc and the direction of the angle, which is supost to point towards the target Unfortunatly when the Yaw is less than 170 degrees (approx) the arrow points INTO the ship instead of away from it (towards the target). The funny thing is when I roll the ship 180 degrees the arrow poins in the right direction!!! Because I am only interested in the yaw I use the X and Z values of the vector for the dot product and normilization CAN ANYONE HELP! this is drivin me crazy! I have an idea, but don''t know how to implement it. Maybe I could use a vector that points LEFT. sort of like some of the D3DX functions

##### Share on other sites
Deyja    920
If it''s pointing exactly the wrong direction (meaning, its parallel to where it should be, but facing backwards) just negate the x-y-z values (multiply each by -1) and it''ll be pointing the right way. There must be something wrong somewhere in the math to be giving you that error - likely, you haven''t taken into account that the dotproduct will never give you an angle over 180 degrees. It wouldn''t affect the roll, and pitch shouldn''t be able to go all the way around - so you''re only seeing it in the yaw. You''ll have to figure out how to decide wether or not to negate it.

##### Share on other sites
Timkin    864
More than likely you''ve failed to maintain a consitent set of coordinates. Since you''re using Euler angles to define orientation then you are most likely using trigonometry to convert to displacements. You have probably failed to verify quadrants correctly when performing one of these transformations. Go back and check your math (on paper) and then your code.

Cheers,

Timkin

##### Share on other sites
sQuid    149
I think what Deyja said is right, there are two possible solutions to acos(foo) = bar since cos(theta) = cos(-theta). Most math libraries will always give you the one that''s less than PI.

##### Share on other sites
Cybertron    122
thanks alot!!! I don't think its a rotation problem. the ship flys forward, so it must work. I tried negating it when the angle is less than 180, and it works!!! I will do some more testing, to be sure it works 100%

this is what I have:

if ((lpCombatShip->fYaw < 180) && (lpCombatShip->fYaw > 1)){	fTurretYaw = RAD_TO_DEG(acos(-fDotProduct)) - 90;}else{	fTurretYaw = RAD_TO_DEG(acos(fDotProduct)) + 90;}

I am not sure about why I need to add/subtract 90 degrees, but im guessing that the angles I use for rotation have 0 out to the side and the angle returned for the dot product is to the top. I put that in to make the angle drawn in 3D match the 2D version.

[EDIT]

I have tweaked it a bit more to beter determin weather it should use the normal or inverted cos. It still acts up when the ship is behind the target (on the Z axis) and the rotation is 180 degress.

Is there any official solution to this problem???

[edited by - Cybertron on March 19, 2002 4:36:31 PM]