Wanting sprite always pointing at the center
by Matthew Shapiro · in Torque X 2D · 09/15/2007 (6:23 am) · 13 replies
So I"m trying to get my object to always point at the center of the screen. I figured the easiest way to do this was basic trigonometry (tax x = opp/adj). So here's the code I came up with:
Unfortunatly, this doesn't work. It seems to be pointed at a completely "random" direction depending on where it is on the x axis (only 1 axis movement). However, it is consistently random, meaning that when I go to the same place on the x axis it is pointing the same direction.
I though this would be an easy feature to have but apparently I messed something up. Any ideas?
if (move != null)
{
// set our test object's Velocity based on stick/keyboard input
SceneObject.Physics.VelocityX = move.Sticks[0].X * 20.0f;
// Correct rotation so ship is always facing the center.
Vector2 center = new Vector2(0, 0);
Vector2 tempVector = SceneObject.Position - center;
// Try to prevent divide by 0 problems
if (tempVector.X == 0)
tempVector.X = (float)0.001;
double angle = Math.Tan((tempVector.Y) / (tempVector.X));
SceneObject.Rotation = (float)angle;
}Unfortunatly, this doesn't work. It seems to be pointed at a completely "random" direction depending on where it is on the x axis (only 1 axis movement). However, it is consistently random, meaning that when I go to the same place on the x axis it is pointing the same direction.
I though this would be an easy feature to have but apparently I messed something up. Any ideas?
#2
09/15/2007 (10:31 am)
I'm pretty sure James is correct on both counts--I first looked at this post without coffee or in fact being awake at all, and identified both of those issues, but couldn't function well enough to actually post solutions--and he covered both the solutions!
#3
09/15/2007 (1:52 pm)
Horray for me '-)
#4
Now the sprite doesn't rotate at all, not even "randomly." For reference, my player is being spawned at (0,30) and is only moving along the X axis.
When I have the ship on the total right of the screen angle is 0.556694, tempVector is -48.20404,-30 and the ship's posiition is 48.20404,30.
And it actually does rotate a tiny bit. It looks like it is slightly rotated to the right when the ship's X position is positive an dslightly rotated to the left when the ships' X position is negative.
09/15/2007 (5:17 pm)
Ah good find, I forgot about ATan (been a while since trig ;)). Here's the latest code:Vector2 center = new Vector2(0, 0);
Vector2 tempVector = center - SceneObject.Position;
// Try to prevent divide by 0 problems
if (tempVector.X == 0)
tempVector.X = (float)0.001;
double angle = Math.Atan((tempVector.Y) / (tempVector.X));
SceneObject.Rotation = (float)angle;Now the sprite doesn't rotate at all, not even "randomly." For reference, my player is being spawned at (0,30) and is only moving along the X axis.
When I have the ship on the total right of the screen angle is 0.556694, tempVector is -48.20404,-30 and the ship's posiition is 48.20404,30.
And it actually does rotate a tiny bit. It looks like it is slightly rotated to the right when the ship's X position is positive an dslightly rotated to the left when the ships' X position is negative.
#5
Just convert it to degrees and you should be good to go.
09/15/2007 (7:13 pm)
You're getting the result in radians? (I'm guessing.... haven't had enough coffee either =).Just convert it to degrees and you should be good to go.
#6
So now I am converting the radians to degrees via: angle = angle * (180 / Math.PI);
Now I am closer, but it actually looks like the side of the ship is always facing the center, so I'm wondering if my logic is right. When my ship first spawns it spawns at 0,30, tempVector is 0.001, -30 (not 0 to prevent divide by zero probs). The angle is then -89.999 which sets the rotation to 270.0019. This causes the ship to be facing the side of my screen instead of straight up (or close to straight). Is my logic behind using the right triangle correct?
Sorry for the nubishness.
09/15/2007 (7:53 pm)
Hrm ok I didn't realize that would get me radians, that would explain a few things :). So now I am converting the radians to degrees via: angle = angle * (180 / Math.PI);
Now I am closer, but it actually looks like the side of the ship is always facing the center, so I'm wondering if my logic is right. When my ship first spawns it spawns at 0,30, tempVector is 0.001, -30 (not 0 to prevent divide by zero probs). The angle is then -89.999 which sets the rotation to 270.0019. This causes the ship to be facing the side of my screen instead of straight up (or close to straight). Is my logic behind using the right triangle correct?
Sorry for the nubishness.
#7
09/15/2007 (7:59 pm)
Just add (or remove? still not enough coffee.... working on it though) 90 degrees to the angle...
#8
So now I am converting the radians to degrees via: angle = angle * (180 / Math.PI);
Now I am closer, but it actually looks like the side of the ship is always facing the center, so I'm wondering if my logic is right. When my ship first spawns it spawns at 0,30, tempVector is 0.001, -30 (not 0 to prevent divide by zero probs). The angle is then -89.999 which sets the rotation to 270.0019. This causes the ship to be facing the side of my screen instead of straight up (or close to straight). Is my logic behind using the right triangle correct?
Sorry for the nubishness.
09/15/2007 (8:04 pm)
Hrm ok I didn't realize that would get me radians, that would explain a few things :). So now I am converting the radians to degrees via: angle = angle * (180 / Math.PI);
Now I am closer, but it actually looks like the side of the ship is always facing the center, so I'm wondering if my logic is right. When my ship first spawns it spawns at 0,30, tempVector is 0.001, -30 (not 0 to prevent divide by zero probs). The angle is then -89.999 which sets the rotation to 270.0019. This causes the ship to be facing the side of my screen instead of straight up (or close to straight). Is my logic behind using the right triangle correct?
Sorry for the nubishness.
#9
Though just for curioisity's sake, if anyone wants to tell where my logic was wrong I'd be much appreciative!
09/16/2007 (12:52 am)
Hrm well I solved the issue by realizing there is a AngleFromTarget function in T2DVectorUtil. Handy ;).Though just for curioisity's sake, if anyone wants to tell where my logic was wrong I'd be much appreciative!
#10
09/17/2007 (10:33 am)
As Magnus said, it sounds like you just need a constant offset of either +/-90 degrees. Zero degrees for your image is pointing to the right, just keep that in mind, or change your image to point up.
#11
09/17/2007 (11:35 am)
And just in case you were wondering--that sounds like a "hack", but it's pretty standard given the nature of Cartesian coordinate systems and cyclic functions--especially when your signs are a bit different in stock TGB than a "normal" Cartesian coordinate system.
#12
09/17/2007 (4:20 pm)
For future reference, these two methods can be helpful:degrees = MathHelper.ToDegrees(radians); radians = MathHelper.ToRadians(degrees);
#13
09/17/2007 (11:56 pm)
Cool thanks guys :)
Associate James Ford
Sickhead Games
Also I think its ATan ( Y, X ) not Tan( Y, X);
tan(angle) = opposite/adjacent
...
Atan(tan(angle)) = ATan(opposite/adjacent)
...
angle = ATan(opposite/adjacent)
Tada!