English 中文(简体)
XNA 2D vector angles - what s the correct way to calculate?
原标题:

what is in XNA in 2D the standard way vector angles work ?

0 degrees points right, 90 points up, 180 left, 270 down ?

What are the standard implementations of

float VectortoAngle(Vector2 vec)

and

Vector2 AngleToVector(float angle)

so that VectortoAngle(AngleToVector(PI)) == PI ?

问题回答

To answer your first question, 0 degrees points up, 90 degrees points right, 180 degrees points down, and 270 degrees points left. Here is a simple 2D XNA rotation tutorial to give you more information.

As for converting vectors to angles and back, I found a couple good implementations here:

Vector2 AngleToVector(float angle)
{
    return new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
}

float VectorToAngle(Vector2 vector)
{
    return (float)Math.Atan2(vector.Y, vector.X);
}

Also, if you re new to 2D programming, you may want to look into Torque X 2D, which provides a lot of this for you. If you ve payed to develop for XNA you get the engine binaries for free, and there is a utility class which converts from angles to vectors and back, as well as other useful functions like this.

Edit: As Ranieri pointed out in the comments, that function doesn t make sense when up is 0 degrees. Here s one that does (up is (0, -1), right is (1, 0), down is (0, 1), left is (-1, 0):

Vector2 AngleToVector(float angle)
{
    return new Vector2((float)Math.Sin(angle), -(float)Math.Cos(angle));
}

float VectorToAngle(Vector2 vector)
{
    return (float)Math.Atan2(vector.X, -vector.Y);
}

I would also like to note that I ve been using Torque for a while, and it uses 0 degrees for up, so that s where I got that part. Up meaning, in this case, draw the texture to the screen in the same way that it is in the file. So down would be drawing the texture upside down.

There is no convention for which direction a certain angle represents in XNA, so you can just define it however you like.

I m not sure when the last time I used angles in a game was. In almost every case it s easier to work directly with vectors, if a little less intuitive to begin with.





相关问题
c# wpf 2d graphics - Looking for tutorial / examples [closed]

Could you please point me to a good C# tutorial for drawing 2d graphics like Ellipse and Rectangle (that inherit from Shape) on a Canvas using WPF ? I m also interested later to click on shapes and ...

How to multiply two sprites in SpriteBatch Draw XNA (2D)

I am writing simple hex engine for action-rpg in XNA 3.1. I want to light ground near hero and torches just as they were lighted in Diablo II. I though the best way to do so was to calculate field-of-...

2D Ball Collisions with Corners

I m trying to write a 2D simulation of a ball that bounces off of fixed vertical and horizontal walls. Simulating collisions with the faces of the walls was pretty simple--just negate the X-velocity ...

2D Mathematics Geographical Directions

I am trying to check the location of a point(px, py) on 2D graph in relation to a line segment (lx1, ly1) (lx2, ly2), using logic of North South East West directions. The logic I have implemented is ...

Perpendicular on a line from a given point

How can I draw a perpendicular on a line segment from a given point? My line segment is defined as (x1, y1), (x2, y2), If I draw a perpendicular from a point (x3,y3) and it meets to line on point (x4,...

How to select a line

So I m trying to figure out how to implement a method of selecting lines or edges in a drawing area but my math is a bit lacking. This is what I got so far: A collection of lines, each line has two ...

How do I generate a contour graph?

How do I generate a contour graph like this: contour http://www.fz-juelich.de/vislab/software/gsharp/Gsharp/userguide/interpolate/ex6.gif It s easy enough if the points are on a regular grid, but ...

Mapping points from Euclician 2-space onto a Poincare disc

For some reason it seems that everyone writing webpages about Poincare discs is only concerned with how to represent lines and measure distances. I d like to morph a collection of 2D points (as ...

热门标签