bobbing FPS camera

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
arnir
Competition winner
Posts: 154
Joined: Sat Jan 20, 2007 4:36 pm
Location: Czech Republic

bobbing FPS camera

Post by arnir »

code for walk effect.
open
CCameraFPSSceneNode.cpp

find line

Code: Select all

const f32 MAX_VERTICAL_ANGLE = 88.0f;
past this line add

Code: Select all

int frame = 1;
int walk = 1;
f32 LastBobbingTime = os::Timer::getTime();
find line

Code: Select all

if (CursorKeys[0])
pos += movedir * (f32)timeDiff * MoveSpeed;
and replace it for

Code: Select all

if (CursorKeys[0]){
pos += movedir * (f32)timeDiff * MoveSpeed;
if(os::Timer::getTime()-LastBobbingTime >=10) //every 10 ms
{
if (walk == 1) {
RelativeRotation.X += 0.14f;  //change rotation
RelativeRotation.Y += 0.05f;
++frame;
if(frame >= 20) {
walk = 2;
}
}
if(walk == 2){
RelativeRotation.X -=0.14f;
RelativeRotation.Y += 0.05f;
++frame;
if(frame >= 40) {
walk = 3;
}
}
if(walk == 3){
RelativeRotation.X +=0.14f;
RelativeRotation.Y -= 0.05f;
++frame;
if(frame >= 60) {
walk = 4;
}
}
if(walk == 4){
RelativeRotation.X -=0.14f;
RelativeRotation.Y -= 0.05f;
++frame;
if(frame >= 80) {  //animation frame loop
walk = 1;
frame = 1;
}
}
LastBobbingTime = os::Timer::getTime();
}
}
for faster animation change 10ms to smaller number
for more choppiness animation change frame numbers (20,40,6...) to smaller.
and if you would like strafing or backward move with bobbing, add this lines there too
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

I'd prefer to use sine, as it's smoother.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Not seen this in action but it's something that's used quite a lot in FPS games now.

Christian Clavet has made a completely new FPS camera which has some good sin-wave bobbing and head titling for looking round corners and also whilst strafing which gives a really cool effect.
Image Image Image
arnir
Competition winner
Posts: 154
Joined: Sat Jan 20, 2007 4:36 pm
Location: Czech Republic

Post by arnir »

my bobbing is not up and down.
test it and see
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Post by christianclavet »

Yes, Arnir. It's not up and down and it's pretty simple to use. Your camera is doing like a "head bobbing" from a HULA DANCER character we put in vehicle dashboard. It pivot front and back.
Image
Mine is more complicated, but it mimic the motion a real body walking or running. And my code create a new camera. (You don't have to modify the source to use it)

When you walk or run, your body "fall" and get up in each leg movement while your head try to stay on balance. (My camera rig target is looking far away). So when the body is going down, the camera will try look at the horizon and not look down. (as if it were trying to keep balance). Also my camera rig is based on velocity. (You'll never start running at the first frame, you'll accelerate. When you stop, it's the same thing, you decelerate). So I've put acceleration vectors, on the "up/down" movement.

I've done that camera to have a more looking cinematic view, as I like cinematic gameplay. Still it's not perfect (The concept is right, the code is horrible.. :oops: ). I wish I would have more time to tweak it. Right now, my main objective is to work on a new version of my editor and to put efforts in my studies at the school (Studying as a Level Designer at Ubisoft Campus in Canada)

One thing that could be added, is the disabling of the up/down movement, when the character is not moving (because of collision with the wall for example). It will work correctly if you stop moving and there is no collision with any wall. But adding this will make the thing even more realistic. I would also like to try to change the camera target further away and change the FOV when sprinting. (Will give an impression of speed even more). I would like to adjust the camera target distance based on the relative speed (I think it should be more realistic, right now the distance is far away and fixed)
Post Reply