Sign in to follow this  
ScottC

How would I rotate around a center point?

Recommended Posts

Say I want to rotate the other points around the center point, and I don't have opengl to push, rotate, and pop... how can I do it? [Edited by - ScottC on August 2, 2005 4:52:12 AM]

Share this post


Link to post
Share on other sites
You could use matrices like the APIs but just code it all yourself, it's fairly easy. Just search google for 2D Rotation Matrices or just 3D ones if thats what you need and also google for matrix maths.

EDIT:



^^Linkified^^

ace

Share this post


Link to post
Share on other sites
Quote:
Original post by ace_lovegrove
You could use matrices like the APIs but just code it all yourself, it's fairly easy. Just search google for 2D Rotation Matrices or just 3D ones if thats what you need and also google for matrix maths.

EDIT:



^^Linkified^^

ace


the sites I find are confusing.

Share this post


Link to post
Share on other sites
You need to grasp a few concepts before this will work:
- matrix multiplication (click)
- matrix/vector multiplication
- transformation matrices (click here)

Let us assume a 2D rotation. Furthermore simplify by rotating around the origin first so C = (0,0). We shall rotate Phi angles in radians. Clicky2 gives the rotation matrix for this:

cos Phi sin Phi
-sin Phi cos Phi

Just fill in the values and you have a simple rotation matrix.

Transformation matrices are applied to a coordinate by multiplying the coordinates vector with the matrix. If the previous matrix is R and we want to rotate the point P we compute P' = P R. See Clicky1.

Now transformation can, besides rotations, also be translations. We could build a matrix that transforms some point P 5 units up along the Y axis. Thus, we could also construct a transformation matrix that would move the origin (0,0) to the desired center point C = (Cx,Cy). Now the last step is that we can combine two transformation matrices using multiplication.

We had a rotation matrix R and we just introduced a translation matrix T. If you want to rotate around C you can simply compute the transform matrix M = T R and transform your points with this.

This will probably sound complicated but I suggest you read out the given links and try some examples on paper. Feel free to ask if you are stuck.

Illco

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It is not difficult at all.
You can do that on tree steps, all convened into a single step
Say the origin is p(x0, y0)

The first thing you do is to translate everything to the origin

x’ = x – x0
y’ = y – y0

in matrix form this is equivalent to

[x’ y’ 1] = [x y 1] * [1 0 0]
[0 1 0]
[-x0 -y0 1]


now you rotate each point by the angle you want


x’’ = x’ cos(a) - y’ sin (a)
y’’ = x’ sin(a) + y’ cos (a)

in matrix from this is

[x’’ y’’ 1] = [x’ y’ 1] * [cos(a) sin(a) 0]
[-sin(a) cos(a) 0]
[0 0 1]

finally you translate everything back to where it was


x’’’ = x’’ + x0
y’’’ = y’’ + y0

and in matrix form this is:


[x’’’ y’’’ 1] = [x’’ y’’ 1] * [1 0 0]
[0 1 0]
[x0 y0 1]

Now calling the tree matrices inv(T), R, T, and concatenating all tree equation you get

P’’’ = P * inv(T) * R * T

The expression :

inv(T) * R * T

Is very common in linear algebra and it is called similar transformation. You can develop the algebra by expanding the expression and save few operations here and there, by I recommendation you do the matrix multiplies in some matrix class and it will guaranty correct result not matter what the situation is.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
It is not difficult at all.

ironically this is the only thing I could understand...

Share this post


Link to post
Share on other sites
Basically rotation math is always done around the origin. (0, 0) So if you want to rotate something around this point, you make sure it's coordinates are relative to this, probably with a translation if it's not in local space. Do your rotate, then translate it to the point you want it.

Share this post


Link to post
Share on other sites
Assume View means the point/target you want to rotate around.

// This rotates the object's position around the view depending on the values passed in.
void c3dObject::RotateAroundView(float angle, float x, float y, float z)
{
CVector3 vNewView;

// Get the view vector (The direction we are facing)
CVector3 vView = m_vPosition - m_vView;

// Calculate the sine and cosine of the angle once
float cosTheta = (float)cos(angle);
float sinTheta = (float)sin(angle);

// Find the new x position for the new rotated point
vNewView.X = (cosTheta + (1 - cosTheta) * x * x) * vView.X;
vNewView.X += ((1 - cosTheta) * x * y - z * sinTheta) * vView.Y;
vNewView.X += ((1 - cosTheta) * x * z + y * sinTheta) * vView.Z;

// Find the new y position for the new rotated point
vNewView.Y = ((1 - cosTheta) * x * y + z * sinTheta) * vView.X;
vNewView.Y += (cosTheta + (1 - cosTheta) * y * y) * vView.Y;
vNewView.Y += ((1 - cosTheta) * y * z - x * sinTheta) * vView.Z;

// Find the new z position for the new rotated point
vNewView.Z = ((1 - cosTheta) * x * z - y * sinTheta) * vView.X;
vNewView.Z += ((1 - cosTheta) * y * z + x * sinTheta) * vView.Y;
vNewView.Z += (cosTheta + (1 - cosTheta) * z * z) * vView.Z;

// Now we just add the newly rotated vector to our position to set
// our new rotated view of our camera.
m_vPosition = m_vView + vNewView;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well I am sorry I confused you. What GodBeastX is exactly what I said.
If you know a least the concept of matriices and matrix multiplication it should be clear. If you do not know that but you have a matrix class this is what you do

Matrix RoationAboutAPoint ( Point P, Angle A)
{
Matrix invT = TranlationMatrix (P.Scale(-1));
Matrix R = RotationMatrix (A);
Matrix T = ranlationMatrix (P);

Matrix mat = invT * R * T;
Return mat
}

then you multiply each point by the matrix mat.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this