At 7/1/12 05:18 PM, PandaexpressMan wrote:
Hi, so I was just wondering how I would be able to find the direction that my sprite is facing? The reason I would be trying to do this is so I can make him eventually move in the direction that he is facing. So if anyone can help me I would really appreciate it.
Assuming the game is 2d, you need to do the following things:
1. Find the angle at which your sprite is
2. Implement the following algorithm (I don't know the actual syntax for this in c#, but I'll explain it)
DistanceToMoveX = Speed*Cos(Angle)
DistanceToMoveY = Speed*Sin(Angle)
Sprite.x += DistanceToMoveX
Sprite.y += DistanceToMoveX
So, what this does, is takes a "speed" value (Which would be the overall velocity of your sprite) and multiplies is by Sin and Cosine of the sprite's angle. This means that (stored in a new variables) you have the actual amount that your sprite will move in both the x and y directions, so you add these values to the sprite's current position. (These values can be negative, hence why we always add).
And also, just as a side note, with anything to do with movement within a game, you should multiply the movement speed by "Deltatime" (The length of time between two frames). This would mean that regardless of what FPS your game runs at, a character would still move the same distance in a set period of time. You can implement this in the above algorithm as follows:
DistanceToMoveX = (Speed*DeltaTime)*Cos(Angle)
DistanceToMoveY = (Speed*DeltaTime)*Sin(Angle)
Im not sure if c# (Or whatever API, libraries etc) has a built in deltatime function, but if it does you should definitely use it. If not, do some research and make your own, its pretty vital :P
Hope this helps, if you need anything else drop me a line!