C# velocity and rotation problem

Recommended Posts

Hello, I am using C# XNA and the problem is quite hard to explain. I created a tank that has a moving turret on top. This turret is rotated around the midpoint based on where the mouse is pointed. The problem I come to is when I try and move it around 180 degrees (North is 0) or rather point it downwards, there will be times when I go counter clockwise that the turret will jump backward and the same thing when I go counter clockwise. This only happens at the very end and I thought it was because my rotation was going up to -186 degrees but that should still make it appear at the right place. (If I where to rotate it -186 degrees it would go 186 degrees and not sure where my problem is. The best code I can give is just my velocity normalization code, or if anyone is willing to look at the code closer I can send the project.
public void normVelocity(Vector2 target)
{
int speed = 1;

Vector2 delta = new Vector2(target.X - position.X - 16, target.Y - position.Y - 16);
double dy = -1 - (float)((speed * delta.Y) / Math.Sqrt(delta.X * delta.X + (delta.Y * delta.Y)));
double dx = -(float)((speed * delta.X) / Math.Sqrt(delta.X * delta.X + (delta.Y * delta.Y)));
double val = Math.Atan(dy / dx) * .66;

rotation = (float)(val * Math.PI);
}

Target is the mouse x,y pos. Pos is defined as the right corner of my image on my map so I subtract 16 from both x and y to get the midpoint (its a 32x32 image). The rest is displayed. Any help would be greatly appreciated. EDIT: I apologize forgot to add at dy, we have a -1 at the front because that is the y start point and dx is actually a 0 - the rest.

Share on other sites
To try to understand: Why do you multiply the atan by .66? Why do you multiply the angle by PI?

Share on other sites
I actually found the problem.
The multiplication of .66 was for some unknown reason to me but probably because my 0 degree was actually north instead of the normal east that it is in geometry. The multiplication by pie is because I needed it to be in Radians. However I found that what mine did was went to about 3.25 or pie + .10. So I had to change all values by dividing all my values by the highest value I attained which was 3.25 and then multiplying it by Pie so that it was a value that could never become larger then pie.

I do have one question, I currently use rotation to make it appear as if my object is going diagonal (the main object can only go at 45 degree angles because there is no velocity or different thrust in each wheel). However if someone knows XNA is it more costly to do a SpriteBatch.draw with the rotation then just the normal draw one?

Create an account

Register a new account

• Forum Statistics

• Total Topics
627704
• Total Posts
2978716

• 21
• 14
• 12
• 10
• 12