How to find a new vector which lies 60 degrees away from an existing one?
I would like to find a point which lies 60 degrees away from the forward facing vector and is at some distance in front of a character.
Are the following steps correct?
1) Find the normalized forward facing vector of the character
2) For the x and z components, multiply each of them by cos 60
3) Multiply the new vector by a value and then add the vector to the position of
the character to find the new point which lies 60 degrees away and is at a distance in front of the character
Also, how do I specify if the point lies in a clockwise or anticlockwise direction away from the character's forward facing vector?
Multiplying by cos 60 is not enough, that will simply change the magnitude of the vector and not the direction. Instead you need to do the following:
Depending on your coordinate system and transformation matrix, you might want to switch places for the + and -. Usually, increasing a positive angle gives a counter-clockwise movement.
To move in the other direction, simply set angle to -60 instead of +60.
Your other steps are correct.
newx = x * cos(angle) - z * sin(angle)newz = z * cos(angle) + x * sin(angle)
Depending on your coordinate system and transformation matrix, you might want to switch places for the + and -. Usually, increasing a positive angle gives a counter-clockwise movement.
To move in the other direction, simply set angle to -60 instead of +60.
Your other steps are correct.
I personally find it more practical to express that kind of transformation with more high-level tools, such as quaternions or matrices.
Doing such operations directly is prone to error.
Doing such operations directly is prone to error.
I used the following to find the new vector which lies 60 degrees away from the current normalized forward facing vector.
newx = x * cos(60) - z * sin(60)
newz = z * cos(60) + x * sin(60)
However, it looks like the new vector's orientation is not correct. Sometimes, the new vector points behind the character, even though the angle is less than 180.
Whether I used x and z as the character position's x and z components or as the character forward facing vector's components, the problem is still the same.
What could be wrong with the method?
Or is there a better method like using matrices?
I have checked that the angle is correctly converted to radians in the code and also tried swapping the - and + signs.
newx = x * cos(60) - z * sin(60)
newz = z * cos(60) + x * sin(60)
However, it looks like the new vector's orientation is not correct. Sometimes, the new vector points behind the character, even though the angle is less than 180.
Whether I used x and z as the character position's x and z components or as the character forward facing vector's components, the problem is still the same.
What could be wrong with the method?
Or is there a better method like using matrices?
I have checked that the angle is correctly converted to radians in the code and also tried swapping the - and + signs.
I don't understand what you're trying to get.
Express it with actual mathematical terms (60 degrees away from a vector doesn't mean anything to me) and its relation with the first vector in terms of vector operatins, quaternions and/or matrices will come naturally.
Express it with actual mathematical terms (60 degrees away from a vector doesn't mean anything to me) and its relation with the first vector in terms of vector operatins, quaternions and/or matrices will come naturally.
Provided you have the following information:
and want to find a target point pt, at some distance d from p, in a direction a radians from the forward facing direction, as in the following image:
You can achieve this the following way:
p = position of characterv = forward facing direction of character, normalized
and want to find a target point pt, at some distance d from p, in a direction a radians from the forward facing direction, as in the following image:
You can achieve this the following way:
dir.x = v.x * cos(a) - v.y * sin(a);dir.y = v.y * cos(a) + v.x * sin(a);pt = p + dir * d;
Wouldn't it be better if you did the following instead:-
1. Get the transformation matrix.(I think we need inverse in this case - not sure though)
2. Multiply it by a 60 degree rotation matrix(-60 for cw).
3. Get the forward direction
4. Normalize the forward direction.
5. Then do NewPosition = currentPosition + (fowardDirection * someFactor)
This way you don't have to worry about what kind of coordinate system you are using i.e. if UP is y or z etc.
1. Get the transformation matrix.(I think we need inverse in this case - not sure though)
2. Multiply it by a 60 degree rotation matrix(-60 for cw).
3. Get the forward direction
4. Normalize the forward direction.
5. Then do NewPosition = currentPosition + (fowardDirection * someFactor)
This way you don't have to worry about what kind of coordinate system you are using i.e. if UP is y or z etc.
rotate v 60 degrees and multiply it by your distance.
There you go, you've got the vector that goes from p to pt.
You just need to code the function that takes a vector and an angle and returns the new rotated vector.
There you go, you've got the vector that goes from p to pt.
You just need to code the function that takes a vector and an angle and returns the new rotated vector.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement