# Dot Product - Craps Out At 0 and 180 Degrees

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

## Recommended Posts

Hey Gang, The project I'm working on uses a mouse cursor's position to determine the direction to fire a bullet. Think FPS controls in a 2D game. Here's the problem. I want my projectiles to rotate to face the direction they are being shot in. I'm doing this by getting the Dot Product between the missle's direction and the missle's direction with no rotation. I've already realized that radians do not account for rotation over 180 degrees, but I was able to get passed that by checking the direction of the missle, and flipping it if needed. That all seems to work fine, except when the angle of the missle approaches 0 and 180 degrees. It seems to have a sort of 'dead-zone' where it stops incrementing the angle between the vectors. Here are the macro functions I'm using to convert between radians and degrees (incase the problem is there):
#define DEG2RAD(x) (x * (D3DX_PI / 180.0f)) // Converts degrees to radians
#define RAD2DEG(x) (x * (180.0f / D3DX_PI)) // Converts radians to degrees

Has anyone run into this, or have a possible solution for me? Thanks again all! Matt Hughson

##### Share on other sites
Quote:
 Original post by matthughsonI've already realized that radians do not account for rotation over 180 degrees

Radians are just an alternate measurement scale for angles. There is no limit to the angle representable in radians.

Your problem is most likely due to a CrossProduct operation in your code. The cross product of two parallel vectors is (0,0,0). Your vectors will be parallel when the angle between them is either 0 or 180.

My guess is that you are trying to normalise the cross product of two parallel vectors thus causing a divide by zero error.

##### Share on other sites
Whats wrong with 2pi radians? that equals 360 degrees. Radians are not capped.

The dot product of orthogonal, or parallel vectors, will be 0. You must watch out for this.

##### Share on other sites
Quote:
 Original post by davidx9The dot product of orthogonal, or parallel vectors, will be 0. You must watch out for this.

That's not the problem. Its not that it suddenly jumps back to a rotation of 0 when the vectors are parallel. Its that for about 20 degrees surrounding straight up and straight down the angle stops getting larger or smaller. It just seems to lock.

Matt Hughson

##### Share on other sites
The Dot Product is not your angle - it's the cosine of your angle. That's why it only handles 180 degrees before wrapping. If you take the arcosine and take sidedness into account you can get the angle. If that's working, then look at your rotation code.

Hope that helps.

Tom

##### Share on other sites
There is another way where you have a castian (?) cordinate ( x, y, z ) and you can simply convert that into sphereical cordinates and maybe make a quaternion from that.

##### Share on other sites
Quote:
 Original post by AxiverseThere is another way where you have a castian (?) cordinate ( x, y, z ) and you can simply convert that into sphereical cordinates and maybe make a quaternion from that.

cartesian coordinate

##### Share on other sites
Quote:
 Original post by ParadigmShiftThe Dot Product is not your angle - it's the cosine of your angle. That's why it only handles 180 degrees before wrapping. If you take the arcosine and take sidedness into account you can get the angle. If that's working, then look at your rotation code.Hope that helps.Tom

Awesome dude! Thanks for the help dude. I'll give that a try in the morning.

Matt Hughson

1. 1
Rutin
21
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 13
• 38
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631715
• Total Posts
3001868
×