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

## Recommended Posts

Hi,
I create a RTS game. I got a problem of the character heading. I can calculate the angle of rotate by using dot product, but I can not control the direction of the rotation. For example, turn to right or left when I click the location on the left hand side of the character. The character may turn to right hand side 90 degree and not left hand side 90 degree. Any one got the solution ?

##### Share on other sites
Just a thought: Have you done the appropriate conversions between radians/degrees (if any)?

##### Share on other sites
Post your code for determining the angle and the direction of rotation. The dot product of 2 vectors will give you the angle between them, but won't tell you which vector is on the "left" and which is on the "right."

##### Share on other sites
The code is in Dark gdk
float ar = 0, br = -1, cr, dr; // First heading (ar, br)and second heading (cr, dr)float bb = dbMouseX();float ba = dbMouseY();double pickVector = dbPickObject ( bb, ba, 1, 10 ) ;	float xHit = dbGetPickVectorX() + dbCameraPositionX();//Pick the x and z location on 3d terrain of mouse location	float yHit = dbGetPickVectorY() + dbCameraPositionY();	float zHit = dbGetPickVectorZ() + dbCameraPositionZ();cr = xHit;dr = zHit;kcr = xHit;kdr = zHit;dbMakeVector2 ( 1 );dbMakeVector2 ( 2 );dbSetVector2 (1, ar, br);dbNormalizeVector2 ( 2, 1 );float vX1 = dbXVector2 (2);float vY1 = dbYVector2 (2);dbMakeVector2 ( 3 );dbMakeVector2 ( 4 );dbSetVector2 (3, cr, dr);dbNormalizeVector2 ( 4, 3 );float vX2 = dbXVector2 (4);float vY2 = dbYVector2 (4);float vka = vX1 * vX2 + vY1 * vY2;r2 = dbACOS ( vka );dbYRotateObject ( 10, r2);ar = kcr;br = kdr;

##### Share on other sites
You can use atan2(y,x) to obtain a signed angle. From there you can probably figure out how to do what you want, although some naive angle computation might be off by 360 degrees, and the code to handle this might end up being a bit messy.

Alternatively, you could not use angles, and rotate by using either 2x2 matrices or -my personal favorite- complex numbers. But I don't know how much of a Math enthusiast you are, and you may find the first solution easier.

##### Share on other sites
You can use atan2(y,x) to obtain a signed angle. From there you can probably figure out how to do what you want, although some naive angle computation might be off by 360 degrees, and the code to handle this might end up being a bit messy.

Alternatively, you could not use angles, and rotate by using either 2x2 matrices or -my personal favorite- complex numbers. But I don't know how much of a Math enthusiast you are, and you may find the first solution easier.

• 9
• 9
• 13
• 41
• 15