Rotating a Vector3f by a Vector3f in LWJGL

Alright, So I got this code where I punch in these values:

    rot      :1.0416666	0.0	-0.0
    pos      :-71.25	67.5	91.875

And get these values back:

    rotPos   :-71.25	67.500015	91.874985

Now I’m clearly trying to rotate a Vector3f by another Vector3f, but the following code isn’t rotating anything. Can someone tell me what I’m doing wrong?

    public Vector3f rotV3fByV3f(Vector3f npos, Vector3f nrot){
    		 	Vector3f pos = new Vector3f(0,0,0);
    	        Vector3f rot = new Vector3f(0,0,0);
    	        Matrix4f  matrix = new Matrix4f();
    	        matrix.setIdentity();
    	        
    	        pos = new Vector3f(npos);
    	        
    	        matrix.m03 = pos.x;
    	        matrix.m13 = pos.y;
    	        matrix.m23 = pos.z;
    	        
    	        rot = new Vector3f(nrot);
    	        Matrix4f.rotate((float) Math.toRadians(360), rot, matrix, matrix);
    
    	        return new Vector3f(matrix.m03, matrix.m13, matrix.m23);
    	}

I’m trying to wrap my head around how you can even rotate a vector by another vector…how does that make any sense?

Now do you want to rotate your vector around another vector by a certain angle?

I’m assuming one represents the axis of rotation…you additionally need an angle. The code example doesn’t make any sense. You’re shoving what I’m assuming to be the axis of rotation into a translation slot…assuming that’s the convention you’ve chosen.

Oh okey… seems like, you’ve been really tired at the time you wrote this… ? :smiley:

First, you create the Vector3f’s “pos” and “rot” 2 times. In lines 2 and 3, and in lines 7 and 13 again.

Also, that code seems to work just as it should.
But wtf???
You rotate the matrix by 360 ° ?!? Of course you get the same vector again :smiley:

Matrix4f.rotate((float) Math.toRadians(360), rot, matrix, matrix);

These guys above me are right, you can only rotate a vector by another vector and an angle (or just by another Quaternion).
So I’d suggest:

public Vector3f rotV3fByV3f(Vector3f npos, Vector3f nrot, float rotation) {
	Matrix4f matrix = new Matrix4f();

	Vector3f pos = new Vector3f(npos);

	matrix.m03 = pos.x;
	matrix.m13 = pos.y;
	matrix.m23 = pos.z;

	Vector3f rot = new Vector3f(nrot);

	Matrix4f.rotate((float) Math.toRadians(rotation), rot, matrix, matrix);

	return new Vector3f(matrix.m03, matrix.m13, matrix.m23);
}

EDIT: Removed “matrix.setIdentity()”. The matrix is already an identity matrix, if you create it :slight_smile:

Still not a rotation matrix. A quick glance at the API yields:
public Matrix4f rotate(float angle, Vector3f axis) public Matrix4f rotate(float angle, Vector3f axis, Matrix4f dest)
Which I’d assume create rotation matrices from an axis and an angle.

I was wondering what the correct way to rotate Matrix4fs by 3 axises is.

This is what I got so far:

Matrix4f x =new Matrix4f(mat_transform.rotate(rotationVector.x , new Vector3f(1,0,0)));
	           Matrix4f y =new Matrix4f(mat_transform.rotate(rotationVector.y , new Vector3f(0,1,0)));
	           Matrix4f z =new Matrix4f(mat_transform.rotate(rotationVector.z , new Vector3f(0,0,1)));
	           
	           mat_transform = Matrix4f.mul(Matrix4f.mul(x,y,null), z, null);

If you 're wondering how to correctly use rotation to move in a 3D world,then there is not just one way to rotate matrix in a 3D space. You always have to choose in which order you’re going to apply your rotation (first x, then y and the z, or another way) and each order implies different results.
In conclusion, you have to choose the correct order that matches to your expectation.

If your talking about the implementation, here is what I do for a FPS:


Matrix4f camera = new Matrix4f();
camera.translate(new Vector3f(X, Y, Z));
camera.rotate(Yangle,new Vector3f(0,1,0));
camera.rotate(Xangle,new Vector3f(1,0,0));

Hope it helps :smiley: