Apple_Banana
Apple_Banana

Reputation: 131

Why is there a difference of 90 degrees between rotation and direction?

Firstly, this problem is not unique to the current project I am currently working on and has happened several times before. Here is the problem.

I have a Triangle struct. olc::vf2d is a vector class with x and y components.

struct Triangle
{
    olc::vf2d p1 = { 0.0f,   -10.0f };
    olc::vf2d p2 = { -5.0f,   5.0f };
    olc::vf2d p3 = { 5.0f,    5.0f };
};

I create a triangle along with position and angle for it.

Triangle triangle
olc::vf2d position = { 0.0f, 0.0f };
float angle = 0.0f;

Now, I rotate (and offset) the triangle as so:

    float x1 = triangle.p1.x * cosf(angle) - triangle.p1.y * sinf(-angle) + position.x;
    float y1 = triangle.p1.x * sinf(-angle) + triangle.p1.y * cosf(angle) + position.y;

    float x2 = triangle.p2.x * cosf(angle) - triangle.p2.y * sinf(-angle) + position.x;
    float y2 = triangle.p2.x * sinf(-angle) + triangle.p2.y * cosf(angle) + position.y;

    float x3 = triangle.p3.x * cosf(angle) - triangle.p3.y * sinf(-angle) + position.x;
    float y3 = triangle.p3.x * sinf(-angle) + triangle.p3.y * cosf(angle) + position.y;

When I increase the angle every frame and draw it, it rotates and works as expected. But now here is the problem. When I try to calculate the direction for it to move towards, like this:

position.x -= cosf(angle) * elapsedTime;
position.y -= sinf(-angle) * elapsedTime;

It moves, but is look 90 degrees off from the rotation. Example, it is facing directly up and is moving to the right.

Up until this point, I have always solved this problem by using different angles values, i.e taking away 3.14159f / 2.0f radians from angle used in direction calculation

position.x -= cosf(angle - (3.14159f / 2.0f));
position.y -= sinf(-angle - (3.14159f / 2.0f));

or vice-versa and this fixes the problem (now it moves in the direction it is facing).

But now I want to know exactly why this happens and a proper way to solve this problem, many thanks.

Upvotes: 0

Views: 195

Answers (1)

kch_PE_MSEE_BSCE
kch_PE_MSEE_BSCE

Reputation: 471

There are some missing items to be able to diagnose. You have to have some kind of coordinate system. Is it a right-handed coordinate system or a left-handed coordinate system. You determine this by taking your X/Y origin, and visualizing your hand over it with your thumb pointing towards you. When the X-axis rotates counter-clockwise, i.e. the way the fingers of your right hand curl when held as visualized, does the positive X-axis move towards the positive Y-axis (right-handed system) or does it move towards the negative Y-axis (left-handed system).

As an example, most algebra graphing is done with a right-handed system, but on the raw pixels of a monitor positive Y is down instead of up as typically seen in algebra.

Direction of motion should be independent of rotation angle -- unless you really want them coupled, which is not typically the case.

Using two different variables for direction-of-motion and angle-of-rotation will allow you to visually and mentally decouple the two and see better what is happening.

Now, typically -- think algebra -- angles for rotation are measured starting from "east" -- pointing to the right -- is zero degrees, "north" is 90 degrees -- pointing up -- and so on.

If you want to move "straight up" you are not moving in the zero-degree direction, but rather in the 90-degree direction. So if your rotation is zero degrees but movement is desired to be "straight up" like the triangle points, then you should be using a 90-degree offset for your movement versus your rotation.

If you decouple rotation and motion, this behavior is much easier to understand and observe.

Upvotes: 1

Related Questions