Camera shake for recoil effect

ive gotten the gun animation that makes it shake while shooting, but i need the camera to also move a bit too when the player is shooting. how would i rotate the camera in really tiny increments?

Use the Camera.setRotation() method.

i tried to, but i couldnt figure out for the life of me, how to use it

You should read the tutorials on quaternions and rotations, they help alot.



If you are using jMonkeyPlatform you will (per default) see some quaternions on the right side.

You can drag those into your code and modify them a bit, and apply them to your camera. I dont know how fast you want this rotations to appear, so you could juggle around with timers and rotation amounts.

(for timing use tpf in your update thread; for rotation amounts modify the value of FastMath.PI/X, where a smaller value of X gives less rotation; for axis of rotation modify the 2nd parameter in fromAngleAxis, where the supplied vector serves as the axis for rotation)



Example

public static final Quaternion ROT_LEFT = new Quaternion().fromAngleAxis(FastMath.PI/32, new Vector3f(0,1,0));

public static final Quaternion ROT_RIGHT = new Quaternion().fromAngleAxis(-FastMath.PI/32, new Vector3f(0,1,0));

public static final Quaternion ROT_UP = new Quaternion().fromAngleAxis(FastMath.PI/32, new Vector3f(1,0,0));

public static final Quaternion ROT_DOWN = new Quaternion().fromAngleAxis(-FastMath.PI/32, new Vector3f(1,0,0));



Quaternion originalCamRotation=cam.getRotation();

//now you apply those rotations to your camera

ROT_LEFT.multLocal(cam.getRotation());



//done with rotations, set the camera to original location

cam.setRotation(originalCamRotation);

1 Like