moooni moon
moooni moon

Reputation: 363

Rotation of a point around a fixed point at a particular radius in Directx?

I am trying to rotate a point say (20,6,30) around a point (10,6,10) at a radius of 2 and i have failed so far trying to do it. I know that to rotate a point around origin you just multiply rotation matrix with world matrix and to rotate a point around itself is translating the point to origin ,then rotating and translating back, but not sure how to approach this problem.

Upvotes: 0

Views: 2877

Answers (2)

ichster
ichster

Reputation: 23

Using a radius doesn't make sense if you are rotating an object coordinate around an arbitrary origin coordinate because the radius is going to be constant during the rotation. To use a desired radius during rotation make sure that the object coordinate and origin coordinate are the desired radius apart. Also note that you need a rotation axis to rotate around a point while using euler angles (vs. spherical or quaternions) because otherwise its undefined in which direction(s) you will be rotating.

If you are willing to do matrix math here is how I do it (using pseudo code because I only know OGL):

vec3 translate_around_point
(
    vec3 object_pos,
    vec3 origin_pos,
    vec3 rotation_axis,
    float rotation
)
{
    // get difference
    vec3 difference = object_pos - origin_pos;
    //rotate difference on rotation axis
    mat4 model = rotate(mat4 model(1.0), rotation, rotation_axis);
    vec3 trans = model * vec4(difference, 1.0);
    //add add translation to origin pos
    return origin_pos + trans;
}

Your code using this function would look like this (radius is 22.360679626464844):

vec3 object_pos(20.0, 6.0, 30.0); 
vec3 orig_pos(10.0, 6.0, 10.0);
vec3 axis(0.0, 1.0, 0.0); // rotation axis is 'up' in world space
float angle = 90.0;
object_pos = translate_around_point(object_pos, orig_pos, axis, angle);

Also if you were to run it with the object position having a greater height or a tilted axis of rotation then the origin position may not appear to be at the center of the rotation anymore. This is ok, because if the rotation doesn't rotate on part of the axis (x,y, or z), then it won't effect the translation on that part.

Upvotes: 0

Code Monkey2
Code Monkey2

Reputation: 161

I could slap together some C++ code if you like (stray away from D3DX as it is deprecated), but I think figuring things out for yourself is a big part of programming. Here is the math behind rotating 3d point v2 around 3d point v1. Hope it helps:

1.) Compute difference vector by subractring v2 from v1. Store in v3.

2.) Convert v3 to spherical coordinates, a notation of defined by radius, yaw, and pitch.

3.) Change values of theta (yaw) and phi (pitch) as required.

4.) Convert v3 back into Cartesian (x, y, z) coordinates and add the coordinates of v1. That's where v2's new position should be.

Note 1 - In physics, the meaning of theta and phi are swapped, so theta is pitch and phi is yaw. In mathematics, theta is yaw and phi is pitch.

Note 2 - yaw, pitch and roll are described as:

Graphical definition of yaw, pitch, and roll

Note 3 - Wikipedia on D3DX: "In 2012, Microsoft announced that D3DX would be deprecated in the Windows 8 SDK, along with other development frameworks such as XNA. The mathematical constructs of D3DX, like vectors and matrices, would be consolidated with XNAMath into a new library: DirectXMath."

Upvotes: 3

Related Questions