# How to use Matrix3d to move/pan left and right

Hi,

I have following code to move player with WSAD. As the game is in a zero-gravity space, it works perfectly with default camera control but I’m wondering if I can replace the code in left/right block by Matrix3d to have a cleaner code:

``````	public void simpleUpdate(float tpf) {
Vector3f vel = new Vector3f(0f, 0f, 0f);
if (playerMovingForward) {
}
if (playerMovingBackward) {
vel = vel.subtract(cam.getDirection());
}
if (playerMovingLeft) {
Vector3f d = cam.getDirection();
float a = FastMath.atan2(d.z, d.x);
vel = vel.add(new Vector3f(FastMath.cos(a - FastMath.PI * 0.5f), 0f, FastMath.sin(a - FastMath.PI * 0.5f)));
}
if (playerMovingRight) {
Vector3f d = cam.getDirection();
float a = FastMath.atan2(d.z, d.x);
vel = vel.add(new Vector3f(FastMath.cos(a + FastMath.PI * 0.5f), 0f, FastMath.sin(a + FastMath.PI * 0.5f)));
}
playerRbc.setLinearVelocity(vel.normalizeLocal().mult(PLAYER_SPEED));
cam.setLocation(playerNode.getLocalTranslation());
}
``````

Thanks!

Where are you getting the Matrix3d class from?

Why do you think a matrix will clean up this already very straight forward code?

Haha, something like:

``````Matrix3d m = new Matrix3d();
m.fromAngleAxis(...); // not sure which methods I can use here...to me, it may look better than atan2/cos/sin
``````

Still don’t know where the Matrix3d class comes from but matrixes are a very poor way of keeping state and a lot of extra numbers just for calculating a rotation.

Probably you want a quaternion here.

Is your goals to “turn” the player by a certain number of degrees based on input?

If also looks like from your math that you intend up to always be “y=1” and never incur a roll through turning operations.

In that case, you may prefer to keep yaw and pitch and then just derive the rotations as needed to transform direction.

Note: there is already a utility class you can use that will wire input to object movement (of which the object can be a camera):

When player press A nd D. I want to keep the avater pan left and right according to where it faces.

My understanding is that it means I need to move avater along the axis which is +/-90 degree to the yaw angle. Since the W and S may also be pressed at the same time, I have to combine them into the velocity vector. For example, move 45 degree to the yaw angle. That’s why I was looking for an existing utility to do this directly.

See:
camera.getLeft()

Gosh, that’s the perfect answer I’m looking for!!!

``````	if (playerMovingLeft) {