# first person camera with Acceleration/Decceleration(Inertia)

This topic is 2561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If I want to make a simple first person camera I can do this:
[font="Arial"]
 case KEY_S: float xrotrad, yrotrad; yrotrad = (yrot / 180 * 3.141592654f); xrotrad = (xrot / 180 * 3.141592654f); pos.x -= float(sin(yrotrad)); pos.z += float(cos(yrotrad)); pos.y += float(sin(xrotrad)); break; [/font][font="Arial, Helvetica, sans-serif"][/font]

Now if wanted to add Acceleration/Decceleration(Inertia) I would have thought to do this:

[font="Arial"] case KEY_S: float xrotrad, yrotrad; yrotrad = (yrot / 180 * 3.141592654f); xrotrad = (xrot / 180 * 3.141592654f); vel.x -= float(sin(yrotrad)); vel.z += float(cos(yrotrad)); vel.y += float(sin(xrotrad)); break; [/font]
and update "pos"

 pos += vel * timestep; //decelelerate camera if(vel.x > 0.0) vel.x -= 1; if(vel.x < 0.0) vel.x += 1; if(vel.z > 0.0) vel.z -= 1; if(vel.z < 0.0) vel.z += 1; 

but for some reason this does not work.

how can I add Acceleration/Decceleration properly?

##### Share on other sites
I think your friction code (subtracting 1 from the velocity each update) is acting too strongly. Each frame, your key handling is changing the velocity from -1 to +1 (the range of the sin/cos functions), and then your friction is immediately removing all of it.

For friction, try multiplying your velocity by 0.9 instead of subtracting 1. That will scale friction with velocity and look more realistic. It'll also mean you won't have to separate the less-than-zero or greater-than-zero cases.

##### Share on other sites

I think your friction code (subtracting 1 from the velocity each update) is acting too strongly. Each frame, your key handling is changing the velocity from -1 to +1 (the range of the sin/cos functions), and then your friction is immediately removing all of it.

For friction, try multiplying your velocity by 0.9 instead of subtracting 1. That will scale friction with velocity and look more realistic. It'll also mean you won't have to separate the less-than-zero or greater-than-zero cases.

Sorry about that my actual code uses 0.0055 but I used 1 when I the posted pseudocode here;

I tried multiplying it like you said and although it looks better it still doesn't work properly

Thanks anyway

##### Share on other sites
Can you describe what kind of problem you're having, and what else you've tried? The code in general looks fine to me; perhaps the problem is caused elsewhere.

##### Share on other sites
It's ok I fixed it.
I made each kepress call a function walk(); with a different direction, for example if I pressed "a" I would call walk(1.0, 0.0, 0.0);

here is my walk(); function:

 void walk(float x, float y, float z) { if(!jumping) { float yrotrad = (ry / 180 * PI); float xrotrad = (rx / 180 * PI); if(x != 0) { vel.x += (float(cos(yrotrad)) * speed) / x; vel.z += (float(sin(yrotrad)) * speed) / x; } if(z != 0) { vel.x -= (float(sin(yrotrad)) * speed) / z; vel.z += (float(cos(yrotrad)) * speed) / z; } } } 

If I'm doing anything wrong please let me know

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633705
• Total Posts
3013462
×