# velocity question from book iPhone

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;
}

• Without the deceleration, your player-entity will never get slower without any further input. – bummzack Jul 23 '11 at 14:01

## 1 Answer

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.

• 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. – Ali1S232 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). – Ali1S232 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