开发者

Modifying The Angle of a Unit Vector

开发者 https://www.devze.com 2023-03-06 12:06 出处:网络
I have an angle in the form of a unit vector. I need to be able to change this angle. Say the angle of the vector V = (x,y) is currently A. How would I go 开发者_Python百科about changing the angle to

I have an angle in the form of a unit vector. I need to be able to change this angle. Say the angle of the vector V = (x,y) is currently A. How would I go 开发者_Python百科about changing the angle to A+0.2 (that is just an example, it could be any value I'm adding) without converting the vector to an angle, modifying it, then calculating the appropriate vector again?


Using the following formulae to rotate your vector counter-clockwise:

Modifying The Angle of a Unit Vector

therefore to rotate the vector V = (x, y) pi an angle A you would do the following:

V' = (x * cos(A) - y * sin(A), x * sin(A) + y * cos(A))

or with code:

vec2 rotateVec2(const vec2 &vec, float angle)
{
    float newX = vec.x * cos(angle) - vec.y * sin(angle);
    float newY = vec.y * sin(angle) + vec.y * cos(angle);
    return vec2(newX, newY);
}


x2 = x * cos(angle) - y * sin(angle);
y2 = y * cos(angle) + x * sin(angle);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号