Public Group

# [XNA] Calculating Direction

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

## Recommended Posts

I seem to have more problems than everyone else...but the journey continues, I guess. My question is how do I calculate the direction an object, such as a player, is facing? I've tried looking it up, but everything I've found deals with 3D, whereas I'm working in 2D. Are there any resources specific to 2D? Not asking for you to do it for me...just need to know where to go to figure it out!

##### Share on other sites
Are you speaking top-down, or side scrolling?

If you're speaking top-down, you can do it in radians or degrees as an angle. Basically, far right is 0, up is 90, left is 180, etc...As your character rotates, you either increase or decrease the angle.

If you're talking side scrolling, right could be 1, while left is -1, or you could continue to do angles with right = 0, and left = 180.

Cheers!

##### Share on other sites
Haha. Wow. Sorry. I totally forgot about telling you what perspective it's in. :)

Sorry!

It's top-down, and I'm not really familiar with any kind of trig...took Algebra III in high school instead of Trig...wasn't aware then that I wanted to program. :)

Are there any resources out there for me to read on this kind of thing?

I think it should be mentioned, though, that I didn't actually write the code for it to rotate. Apparently that's part of the XNA framework. All I do is increase or decrease the rotation variable depending on which direction the object rotates and XNA does the rest. So...still at a loss.

Okay...so I just read the MSDN doc on SpriteBatch.Draw regarding Rotation, and it's expressed in radians. However, I still don't understand how this helps me figure out which direction my object faces. I need to know this so I can tell the game where to draw a bullet and where it needs to go. Thanks for any help!
[/edit]

[Edited by - violentcrayon on August 14, 2007 7:32:29 AM]

##### Share on other sites
If you pass the y offset and x offset between your position and the thing you're looking at into Math.Atan2() it will return the angle (in radians).

To convert back you can use distance * Math.Sin(angle) for the x-component and and distance * Math.Cos(angle) for the y-component (this assumes zero and the y axis point North).

Not sure if that's of any help or not.

##### Share on other sites
Thanks, Benryves. I had to use Sin on the Y axis, though, because the farther up from Zero Y goes, the lower the number (ie, negative numbers). I'm guessing that's the proper solution since that's what worked. :)

However, my issue now is how do I know which object my player is looking at? =/

##### Share on other sites
Okay, so now I calculate the direction I'm facing independently of any kind of object by pass the rotation variable to the Sin and Cos of the Math function. I'm not even using Atan2. I found this after spending a large amount of time reading some MSDN and help file stuff. Took way too long to figure out. Now, I'm just curious.

Here's the code.

if (totalTime >= keyPressDelay)            {                if (kState.IsKeyDown(Keys.D))                {                    //rotate right                    tanks[0].rotation += (float)(gameTime.ElapsedGameTime.TotalSeconds * 2);                }                if (kState.IsKeyDown(Keys.A))                {                    //rorate left                    tanks[0].rotation -= (float)(gameTime.ElapsedGameTime.TotalSeconds * 2);                }                mapAdd.X = (float)Math.Sin(tanks[0].rotation);                mapAdd.Y = -(float)Math.Cos(tanks[0].rotation);                if (kState.IsKeyDown(Keys.W))                {                    TileMap.mapDrawPosition -= mapAdd;                    for (int i = 1; i < tankCount; i++)                    {                        tanks.position.Y += 1.0f * (float)TileMap.tileHeight;                    }                    totalTime = 0.0f;                }            }

Could anyone explain how that actually works to tell me the direction it's facing? I'm guessing it's because rotation is expressed in radians relative to the overall grid or something along those lines? Again, I know zero about trig and my math is a tad rusty until school starts in October...

1. 1
2. 2
3. 3
frob
20
4. 4
Rutin
18
5. 5

• 33
• 13
• 10
• 9
• 18
• ### Forum Statistics

• Total Topics
632555
• Total Posts
3007062

×