# Help Implementing Camera Sway When Sprinting

So far, my code is pretty much just https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_collision, and I’m trying to make the camera rock back and forth.

I tried rotating the camera inside simpleUpdate with the angle being based on the sin of tpf, but it didn’t have the effect I wanted. It just kept spinning, and although I could change the speed, it always rotated in the same direction and rotating with the mouse messed things up.

Then I did the following in simpleUpdate:

[java]

//only if they’re moving fowards, not left, right, or back

if (up) {

//a field, starts at 0

ctr += 1;

//so value goes up and down again and again

float percent = FastMath.sin(ctr/25.0F);

//rotate a little each time

Quaternion sway = (new Quaternion().fromAngleAxis(0.005F * percent, walk_dir));

//combine it with the camera’s current rotation so it doesn’t reset it

Quaternion foo = cam.getRotation().multLocal(sway);

//apply

cam.setRotation(foo);

}

[/java]

walk_dir is just the walkDirection in the collision tutorial.

This worked much better than my last attempt and it’s almost what I want. It still messes up the left to right sway when looking on other directions though.

How can I implement camera rocking that works no matter what direction the player is facing?

Most camera rocking is just translation and not rotation. I notice some games add a roll in with the normal motion but it almost always makes me queasy after a while.

Movement is a little easier but you have to keep track of the natural location of the camera separate from the camera (you’d have to do the same thing with rotation, too). You want to be able to add a frame-based offset to some “stable” location to make the camera location. Then it’s just a matter of applying sin/cos to the left vector and up vector at a period that you like.

But be aware that (as already mentioned) you have to be very careful with this sort of thing as it gives a lot of people motion sickness. Even if you test it with 5 people fine the 6th may find it unplayable.

Test with a lot of people and be prepared to adjust the settings or even give an option to turn it off completely.

don’t do this [java]cam.getRotation.multLocal()[/java] you should never call Local methods on stuff like this, use mult();

Also try [java]sway.multLocal(cam.getRotation); //multLocal is fine here for instance[/java]as Quaternion multiplication is not commutative ab != ba, I have no idea if it will help, but it is worth trying. I haven’t looked at the rest of your logic though, so it may be fundamentally flawed i don’t know.

Thank you all for your replies. I looked closer at games that I ‘recall’ having this feature, and I noticed that the majority of the effect was not actually camera sway but rather movement of arms/weapons in the foreground. I just wanted to have something like this because it seemed very strange gliding across the ground… it almost seemed as if the player was stationary and the ground was moving.

Anyway, thanks!

@zcbvcbds said:
Thank you all for your replies. I looked closer at games that I 'recall' having this feature, and I noticed that the majority of the effect was not actually camera sway but rather movement of arms/weapons in the foreground. I just wanted to have something like this because it seemed very strange gliding across the ground... it almost seemed as if the player was stationary and the ground was moving.

Anyway, thanks!

_moving_ the head is ok... keep it subtle. _rotating_ the head will make people sick, I think. Imagine looking at some point on the horizon... then imagine even a fraction of a degree head turn and how far that point would move. :) I'm getting a little queasy just thinking about it. :)
@pspeed said:
_moving_ the head is ok... keep it subtle. _rotating_ the head will make people sick, I think. Imagine looking at some point on the horizon... then imagine even a fraction of a degree head turn and how far that point would move. :) I'm getting a little queasy just thinking about it. :)

Um... that's my point on the horizon... I dropped it there this morning when I was getting out of my car. Go get your own point on the horizon ;)
@t0neg0d said:
Um... that's my point on the horizon... I dropped it there this morning when I was getting out of my car. Go get your own point on the horizon ;)

You missed my point. I'd point to it but I think I'd lose points. ;) And what was your car doing on my horizon anyway? That's trespassing.
@pspeed said:
You missed my point. I'd point to it but I think I'd lose points. ;) And what was your car doing on my horizon anyway? That's trespassing.

It's only trespassing if you don't have a parking permit. Which reminds me... for those of you who don't, the horizon does offer longer term parking for extended stays and there is also metered parking just left of the horizon.