The motion of the vector relative to rotation

Let's say that I have a vector (x, y) and it is aligned to the beginning by rotation (for example, 30 degrees up).

If I want to transfer this vector by one relative to the direction (so, one block forward with an angle of 30 degrees up), how would I do it?

I am working to bring a simple third-person game together, and I would like to be able to rotate the character and move it forward, regardless of the rotation, and I’m kinda lost on how to do this.

EDIT:

After answering this question, I went further and realized how to get the opposite, left and right movement, so I decided that I would stick to this here for reference, if anyone needs help.

Given the Gunther2 code, To get the reverse movement, simply subtract the offset from the current position:

position -= displacement; 

To get left movement, change the offset variables to

 displacement.x = (float)Math.Sin(angle + PI/2); displacement.y = (float)Math.Cos(angle + PI/2); 

Replace PI / 2 with 90 if Sin / Cos takes degrees instead of radians.

Then subtract the offset:

 position -= displacement; 

To get the right movement, just follow the same steps, but add a new movement:

 position += displacement; 
+4
source share
1 answer

First you need a vector representing the angle:

 Vector2 displacement; displacement.x = (float)Math.Sin(angle); displacement.y = (float)Math.Cos(angle); 

Then, since the length of this vector (x, y) will always be 1, you multiply it by how far you travel. If you want to travel a distance of 1, then X and Y already have the size that they need.

 displacement *= speed; // Assuming your Vector2 *= operator supports a scalar 

Add the offset to the current position:

 position += displacement; 
+4
source

Source: https://habr.com/ru/post/1402686/


All Articles