views:

1843

answers:

4

I'm making an XNA game and have run into a small problem figuring out a bit of vector math.

I have a class representing a 2D object with X and Y integer coordinates and a Rotation float. What I need is to have a Vector2 property for Position that gets and sets X and Y as a Vector2 that has been transformed using the Rotation float. This way I can just do something like;

Position += new Vector2((thumbstick.X * scrollSpeed), -(thumbstick.Y * scrollSpeed));

and the object will move in it's own upward direction, rather than the View's upward direction.

So far this is what I have...I think the set is right, but for += changes it needs a get as well and the answer just isn't coming to me right now... >.>

public Vector2 Position
{
    get
    {
        // What goes here? :S
    }
    set
    {
        X = value.X * (int)Math.Cos(this.Rotation);
        Y = value.Y * (int)Math.Cos(this.Rotation);
    }
}
A: 

Hi

Maybe:

get{
    return new Vector2((int)X/Math.Cos(this.Rotation), (int)Y/Math.Cos(this.Rotation));
}

?

Emiswelt
+4  A: 

No, both are incorrect.

A 2D vector transforms like this:

x' =  x*cos(angle) - y*sin(angle)
y' =  x*sin(angle) + y*cos(angle)

where the angle is measured in radians, zero angle is along the positive x-axis, and increases in the counterclockwise direction as you rotate around the z-axis out of plane. The center of rotation is at the end of the vector being transformed, so imagine the vector with origin at (0,0), end at (x,y) rotation through an angle until it becomes a vector with origin at (0,0) and end at (x', y').

duffymo
It does not matter in what units angle is measured as long it is the same unit in both equations :-)
macias
Every implementation of trig functions that I know of uses radians, so in that sense it certainly does matter. Think radians if you're using a computer to solve this problem.
duffymo
+1  A: 

I think this is a bad idea. Keep all of your objects' X and Y co-ordinates in the same planes instead of each having their own axes. By all means have a Position and Heading properties and consider having a Move method which takes your input vector and does the maths to update position and heading.

Matt Howells
That's why I want to make it a property. You can either reference the X and Y coordinates directly, or you can use the property to get or set them as if they are in a rotated plane. It makes it much easier to calculate the movement controls of an object that changes direction based on where it's rotation points it.
Stephen Belanger
Not sure I agree - I think this will be confusing.
Matt Howells
I don't see why it would be confusing; it's entirely optional. It's just a little helper for moving an object where "forward" should always correspond to a certain edge of the sprite, rather than the top of the viewport.
Stephen Belanger
A: 

You can also use the Matrix helper methods to create a Z rotation matrix then multiply your vector by this to rotate it. Something like this:

Vector v1;
Matrix rot = Matrix.CreateRotationZ(angle);
Vector v2 = v1 * rot;
Bill Reiss