Reputation: 1254
I'm trying to rotate a vertex using the lwjgl Matrix4f class. I tried this code:
float rotY = (float) Math.toRadians(180);
Matrix4f transMat = new Matrix4f();
transMat.rotate(rotY, new Vector3f(0.0f, 1.0f, 0.0f));
transMat.translate(new Vector3f(1.0f, 0.0f, 0.0f));
Vector4f vecPosMod = new Vector4f(1.0f, 0.0f, 0.0f, 1.0f);
Matrix4f.transform(transMat, vecPosMod, vecPosMod);
It should rotate a Vector3f(1.0f, 0.0f, 0.0f)
by 180 degrees but unfortunately after all calculations vecPosMod is (-2.0, 0.0, 1.7484555E-7, 1.0)
. I want it to be (-1.0, 0.0, 0.0, 1.0)
. How?
Upvotes: 1
Views: 1350
Reputation: 5980
You are applying a translation along the X axis to your matrix after the rotation. This is translating the position one unit along the X axis after the rotation is performed resulting in -2.0 instead of -1.0.
Try this:
float rotY = (float) Math.toRadians(180);
Matrix4f transMat = new Matrix4f();
transMat.rotate(rotY, new Vector3f(0.0f, 1.0f, 0.0f));
Vector4f vecPosMod = new Vector4f(1.0f, 0.0f, 0.0f, 1.0f);
Matrix4f.transform(transMat, vecPosMod, vecPosMod);
It gives the following result:
(-1.0 0.0 8.742278E-8 1.0)
The 8.742278E-8 is probably as close to 0.0 as you can get after applying a rotational transform with floats in java.
Upvotes: 1