I'm using jMonkeyEngine to develop a game. I'm good at scripting, but math usually goes over my head. I tried the Wiki page for Quaternion, but I was far too confused to figure this out myself :P
My goal
I have a sphere with physics enabled, and I want to have it rotate based on mouse axis movement. I've got listeners set up properly so that in this method:
void roll(int axis, float tpf) {
Quaternion rot = this.getPhysicsRotation();
switch(axis) {
case MouseInput.AXIS_X:
// Rotate here
break;
case MouseInput.AXIS_Y:
// Rotate here
break;
}
}
an axis and a value (+/-) will be provided. Once I figure out how to modify Quaternions, I'll just use RigidBodyControl.setPhysicsRotation(Quaternion)
to update the rotation.
My Problem
I'm clueless about Quaternions.. I don't even know what method I'd be using. I assume Quaternion.add()
or .mult()
will be necessary.
Someone please help! Here's a link to jme's Quaternion class for reference.
Solution
From Zoltan's answer:
void roll(int axis, float tpf) {
tpf *= 10;
System.out.println("roll("+axis+","+tpf+")");
switch(axis) {
case MouseInput.AXIS_Y:
Quaternion quaternion = new Quaternion();
this.setPhysicsRotation(getPhysicsRotation().mult(quaternion.fromAngleAxis(tpf, Vector3f.UNIT_X)));
break;
case MouseInput.AXIS_X:
Quaternion q = new Quaternion();
this.setPhysicsRotation(getPhysicsRotation().mult(q.fromAngleAxis(tpf, Vector3f.UNIT_Y)));
break;
}
}
I'm not quite sure why case:MouseInput
had to be swapped, but it works this way so.. shrug