• Advertisement
Sign in to follow this  

Rotation About An Axis

This topic is 565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a circular shape object, which I want to rotate like a fan along it's own axis.

I can change the rotation in any direction i.e. dx, dy, dz using my transformation matrix.

The following it's the code:

Matrix4f matrix = new Matrix4f();
matrix.setIdentity();
Matrix4f.translate(translation, matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1,0,0), matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0,1,0), matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0,0,1), matrix, matrix);
Matrix4f.scale(new Vector3f(scale,scale,scale), matrix, matrix);
My vertex code:
vec4 worldPosition = transformationMatrix * vec4(position,1.0);
vec4 positionRelativeToCam = viewMatrix*worldPosition;
gl_Position = projectionMatrix *positionRelativeToCam;


Main Game Loop:


 

Object.increaseRotation(0,0,1f);

knBlT.gif

But, it's not rotating along it's own axis. What am I missing here? I want something like this. Please Help

 

Full Code Here

Share this post


Link to post
Share on other sites
Advertisement

You need to first translate it so the origin is at its centre, then rotate, that translate it back to its position.

I tried that also i.e set the translation to vector3f(0,0,0) but no success  it is revolving and rotating , can you suggest if any code changes are required i not getting the desried result 

Edited by Marvels

Share this post


Link to post
Share on other sites
I tried that also i.e set the translation to vector3f(0,0,0) but no success  it is revolving and rotating , can you suggest if any code changes are required i not getting the desried result

Are you sure, maybe double check this. because translating a point by (0,0,0) is equal to not translating at all.

 

Also I'm not familiar with the API that you are using, but are you sure that you are using it correctly and applying the transformations in correct order.

Edited by imoogiBG

Share this post


Link to post
Share on other sites

If you first translate your matrix and then rotate it you rotate arround the translation axis so maybe you translate to |0,1,0| you are rotating arround the Y axis with a +1 offset. What you should do is either create your fan that its center is at its origin or translate it by its size first so your matrix should look like

Matrix4f.translate(-size * 0.5f, matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1,0,0), matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0,1,0), matrix, matrix);
Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0,0,1), matrix, matrix);
Matrix4f.translate(size * 0.5f, matrix, matrix);
Matrix4f.translate(translation, matrix, matrix);
Matrix4f.scale(new Vector3f(scale,scale,scale), matrix, matrix);

This way we assume your fans top edge is located at |0,0,0| so we translate it by half its size then rotate it arround the axis angles before translating it back to the previous origin going ahead with the other transforms as normal.

Share this post


Link to post
Share on other sites

@[member='Shaarigan'],  What's  this code means 

Matrix4f.translate(-size * 0.5f, matrix, matrix);

translate the object size*0.5 in x,y and z direction ? size here ?

Share this post


Link to post
Share on other sites

By Object is rotating and revolving... when i change Z axis position , i checked using printing position vector it's  remain same but object is rotating and revolving.

Share this post


Link to post
Share on other sites

We dont know anything about your object. What coord system do you use when rendering it?

Where is its orign?

 

I assumed it is flat and located at |0,0,0| for the top left edge so you need tot ranslate it by - half its size to have the z-Axis cross its center.

Then rotate it arround ist center and translate it back to where it was

Share this post


Link to post
Share on other sites

The easiest thing to do when we are lost, is simply not to put the object at its final position. Do it at the origin. If it does not rotate around the point and axis you want, simply translate it back first, just like the other posters told you. Then you'll have to find the axis where you want to rotate around.

 

Having such issues could also mean that the model has been wrongly designed, or not designed for the purpose we want to use it for (ie, was designed for another game, or wasn't designed to be rotated). Then, few changes of the model could help a lot.

It could also mean that the programming is not aligned with the design. For example, if all the design is aligned so that the bottom left corner is the origin of each models, then the development should be aware of it at the beginning and the code should do all the necessary manipulation to overcome this "design issue".

 

So it all depends on what you have, what you want and what you've done so far.

Share this post


Link to post
Share on other sites
mat4 wrld;
wrld.Translate(object->pos);
wrld = object->ROTATION_MAT * wrld;


mat4 MVP = (wrld * VIEW_MATRIX) * PROJECTION_MATRIX;

before using that you go through all vertices and add them together then divide that by objects vertice amount

you have your objects center point

go again through all verts and subtract them by center point ( v = v - center_point ); now you can do anything with your object you can translate or rotate vertices

by using mat4 'implementation':P ah and you need to know how to multiple matrices. ;]

[spoiler]

vec3 operator*(const vec4 p) const
{
vec4 vertexPos;
vertexPos.x = p.x;
vertexPos.y = p.y;
vertexPos.z = p.z;
vertexPos.w = p.w;

vec3 vertexClip;
vec4 matrow;

matrow.x = m[0]; matrow.y = m[1]; matrow.z = m[2]; matrow.w = m[3];
vertexClip.x = dp4(matrow, vertexPos);

matrow.x = m[4]; matrow.y = m[5]; matrow.z = m[6]; matrow.w = m[7];
vertexClip.y = dp4(matrow, vertexPos);

matrow.x = m[8]; matrow.y = m[9]; matrow.z = m[10]; matrow.w = m[11];
vertexClip.z = dp4(matrow, vertexPos);

return vertexClip;
} 

Matrix44<T> operator *(Matrix44<T> mat)
{
	return Matrix44<T>(

(m[0]*mat.m[0])+(m[4]*mat.m[1])+(m[8]*mat.m[2])+(m[12]*mat.m[3]),
(m[1]*mat.m[0])+(m[5]*mat.m[1])+(m[9]*mat.m[2])+(m[13]*mat.m[3]),
(m[2]*mat.m[0])+(m[6]*mat.m[1])+(m[10]*mat.m[2])+(m[14]*mat.m[3]),
(m[3]*mat.m[0])+(m[7]*mat.m[1])+(m[11]*mat.m[2])+(m[15]*mat.m[3]),

(m[0]*mat.m[4])+(m[4]*mat.m[5])+(m[8]*mat.m[6])+(m[12]*mat.m[7]),
(m[1]*mat.m[4])+(m[5]*mat.m[5])+(m[9]*mat.m[6])+(m[13]*mat.m[7]),
(m[2]*mat.m[4])+(m[6]*mat.m[5])+(m[10]*mat.m[6])+(m[14]*mat.m[7]),
(m[3]*mat.m[4])+(m[7]*mat.m[5])+(m[11]*mat.m[6])+(m[15]*mat.m[7]),

(m[0]*mat.m[8])+(m[4]*mat.m[9])+(m[8]*mat.m[10])+(m[12]*mat.m[11]),
(m[1]*mat.m[8])+(m[5]*mat.m[9])+(m[9]*mat.m[10])+(m[13]*mat.m[11]),
(m[2]*mat.m[8])+(m[6]*mat.m[9])+(m[10]*mat.m[10])+(m[14]*mat.m[11]),
(m[3]*mat.m[8])+(m[7]*mat.m[9])+(m[11]*mat.m[10])+(m[15]*mat.m[11]),

(m[0]*mat.m[12])+(m[4]*mat.m[13])+(m[8]*mat.m[14])+(m[12]*mat.m[15]),
(m[1]*mat.m[12])+(m[5]*mat.m[13])+(m[9]*mat.m[14])+(m[13]*mat.m[15]),
(m[2]*mat.m[12])+(m[6]*mat.m[13])+(m[10]*mat.m[14])+(m[14]*mat.m[15]),
(m[3]*mat.m[12])+(m[7]*mat.m[13])+(m[11]*mat.m[14])+(m[15]*mat.m[15]));
}
	
template <class type> type  dp4(t4dpoint<type> vVector1, t4dpoint<type>  vVector2)
{
return ( (vVector1.x * vVector2.x) + (vVector1.y * vVector2.y) + (vVector1.z * vVector2.z) + (vVector1.w * vVector2.w) );
}
 

[/spoiler]

apply that to graphics buffer, this is your actual model, i suggest you decrease Object.increaseRotation(0,0,1f); to something like Object.increaseRotation(0,0,0.01f); but i dont know your game loop

Edited by WiredCat

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement