Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am reading an iphone game dev book and I have a question about velocity.

mainly it is this line

playerVelocity = ccp(playerVelocity.x * dec + acceleration.x * sens, 0);

Why do you mutiple the playerVelocity.x by the deceleration. the book says it works by reducing the current velocity so it is easier to change direction and then add the ( acceleration.x * sens. )

this piece of code is using the accelerometer.

float dec = 0.4f; //lower = quicker to change direction;
float sens = 6.0f; //higher more sensitive;
float maxVel = 100;

playerVelocity = ccp(playerVelocity.x * dec + acceleration.x * sens, 0);
NSLog(@"%@", NSStringFromCGPoint(playerVelocity));

if(playerVelocity.x > maxVel)
    playerVelocity.x = maxVel;
else if(playerVelocity.x < - maxVel)
    playerVelocity.x = - maxVel;
share|improve this question
Without the deceleration, your player-entity will never get slower without any further input. – bummzack Jul 23 '11 at 14:01
up vote 1 down vote accepted

by acceleration I think you are referring to the value IPhone accecelometer returns.

dec value may be used for two reasons :

  1. to simulate friction.
  2. to make velocity changes more smooth.

dec value also creates some upper and lower bounds for player velocity. note that if -1 < acceleration.x < 1 we can assume velocity should be somewhere between -10 and 10.

for more information I need to know the code other than that lines.

share|improve this answer
modified my question for you to include some more info. the playerVelocity.x < - maxVel line i also dont understand, is that less than negative maxVel? surely it should be playerVelocity < maxVel. – Mintuz Jul 23 '11 at 17:45
that part doesn't really make any difference, that's just a more strick bounds for velocity. – Ali.S Jul 23 '11 at 20:26
@Mintuz You are confusing "velocity" with "speed". Velocity has a direction, speed does not. maxVel means the max speed in one direction, probably to the right, and -maxVel means the max speed in the opposite direction. In other words, the maximum velocity you can go to the left is the same as the maximum velocity you can go to the right. – Matthew Read Jul 23 '11 at 21:34
@matthew: check his code velocity.y is always 0, so there is no difference between speed and velocity here (assuming speed is a signed value). – Ali.S Jul 23 '11 at 21:52
Speed is never a signed value. Speed begins at 0 (no motion) and increases (with motion). Negative speed doesn't make sense, since you can only move, not "anti-move". Velocity, however, has a direction. Negative just means opposite when used with a direction, so negative velocity is possible. Negative velocity rightward is the same as positive velocity leftward. The lack of velocity up or down doesn't matter. – Matthew Read Jul 23 '11 at 22:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.