# Quaternian Questions

Me and my friend are making a game together.

We are not really great at game making or anything but we have figured out quite a bit. in fact we have found almost all are questions asked and answered by other people, all except for one: What is a quaternion and how the heck do you use it?!! and why do you need to have one for rotation?!! i understand there is something called Gimbel locking, although i have never had that problem with other engines that use Vector3's for rotation.

More importantly what i wan't to be able to do is make the main character (and others) to move forward in the direction they are facing, as well as backward, left and right. currently we are using the NodeHandler class.

It seems that no matter what i do, the quaternion is always doing weird stuff in the background. if i try to alter a quaternion everything goes out of control.

Another question i have is: How do you make a bullet move forward in the direction it is facing?

As you have probably noticed every single one of these questions has to do with rotation!!.

Well it's either that or using matrices. You only face gimbal lock if you use euler angles, and quaternions are not "necessarily" easier to use. Although they're not that bad when you use jmonkey's implementation and don't really have to worry about the underlying math all that much.

As for what they are, they're nothing more than way to define a rotation (hence the use, why the use, no idea…maybe the compactness? But that's not always true, see: http://www.gamedev.net/reference/articles/article1199.asp). The quaternion identity has an object "facing" the +z axis, so that might be the weirdness you encountered. (See the thread below)

Have you looked at this thread by any chance?

http://www.jmonkeyengine.com/forum/index.php?topic=11858.msg88545#msg88545

There's probably other examples, but that was a nice exercise in doing a rotation.

The problem you're facing isn't that hard, as long as you set things up nicely before hand. E.g. I have all my object's default "forward" looking down +Z in my modeling tool. Then your translations are simple:

Strafing/left right, where right = -1 if strafing right, or +1 if strafing left, and up is your up vector (0,1,0) for me.

```   public void strafe(int right, float time){       AttributeProperty strafeSpeed = ship.getProperty("strafespeed");       if(strafeSpeed != null){          Vector3f dir = ship.getLocalRotation().getRotationColumn(2);          Vector3f rightDir = new Vector3f(dir).cross(up).multLocal(right).normalizeLocal();          ship.getLocalTranslation().addLocal(rightDir.multLocal(strafeSpeed.getValue() * time));       }    }```

Moving forward/backward, where the currentspeed is + (forward), or - (backward). Current speed is calculated by an accceleration/deacceleration computation.

```      if(currentSpeed != null){          ship.getLocalTranslation().addLocal(ship.getDirection().multLocal(currentSpeed.getValue() * time));       }```

These are snippets from my own code, obviously your character would be my ship. But it's nothing more than getting the direction of the rotation (heading), multiplying by how fast you want to travel, and adding it to your character's translation.

I DID IT!!!

Thank you very much for helping. i was able to implement these methods as well as some other related ones into the game Object class. the main cheracture can now move in any direction. now all i need to figure out is how to make him

turn when you move the mouse.

clwillingham said:

I DID IT!!!  :D

Thank you very much for helping. i was able to implement these methods as well as some other related ones into the game Object class. the main cheracture can now move in any direction. now all i need to figure out is how to make him
turn when you move the mouse.

That too is fairly easy (in fact, the flagrush tutorial has a good starting point for such stuff).

```   public void rotate(int lean, float time){       AttributeProperty turnSpeed = ship.getProperty("turnspeed");       if(turnSpeed != null){          rot.fromAngleNormalAxis(lean * turnSpeed.getValue() * time, up);          ship.getLocalRotation().multLocal(rot);       }    }```

Rot is a variable defined elsewhere (a temporary one, to cut down on creating objects every update, come to think of it i should do that for the strafe bit too hehe) that I use to calculate the rotation for that frame (this particular rotation is around the y axis, this code is from a 2Dish game where the playing field is on the XZ plane). The lean variable is +/- depending on which key is pressed (probably can do something similar depending on where your mouse is), and the turn speed should be obvious.