Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate a Vector2?

Tags:

c#

.net

xna


I'm trying to rotate a Vector2 but nothing work.
I've tried the following -> didn't work:

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

I've tried using a rotation matrix -> didn't work

What am I doing wrong ? :/

angle = MathHelper.Pi;
direction.X = (int)((direction.X) * Math.Cos(angle) - direction.Y * Math.Sin(angle));
direction.Y = (int)((direction.X) * Math.Sin(angle) + direction.Y * Math.Cos(angle));

float angle = MathHelper.PiOver2;
Vector2 dir = new Vector2(direction.X, direction.Y);
Vector2.Transform(dir, Matrix.CreateRotationX(angle));
direction = new Point((int)dir.X, (int)dir.Y);
like image 575
darky89 Avatar asked Apr 01 '11 07:04

darky89


People also ask

How do you rotate a vector 180 degrees?

180 Degree Rotation When rotating a point 180 degrees counterclockwise about the origin our point A(x,y) becomes A'(-x,-y). So all we do is make both x and y negative.


2 Answers

Vector2.Transform() returns a result rather than applying the changes in-place.

var transformed = Vector2.Transform(dir, Matrix.CreateRotationX(angle));
direction = new Point((int) dir.X, (int) dir.Y);
like image 110
Paul Ruane Avatar answered Oct 09 '22 13:10

Paul Ruane


The first method you have written should work, as it's showed here also: http://www.oocities.org/davidvwilliamson/rotpoint.jpg

Remember to store the original values, and use those to determine the new values, and do not use the new x value to calculate y. Or store the products in separate variables.

like image 37
Attila Avatar answered Oct 09 '22 14:10

Attila