1
\$\begingroup\$

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;
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Without the deceleration, your player-entity will never get slower without any further input. \$\endgroup\$
    – bummzack
    Jul 23, 2011 at 14:01

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$
    – Mintuz
    Jul 23, 2011 at 17:45
  • \$\begingroup\$ that part doesn't really make any difference, that's just a more strick bounds for velocity. \$\endgroup\$
    – Ali1S232
    Jul 23, 2011 at 20:26
  • \$\begingroup\$ @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. \$\endgroup\$ Jul 23, 2011 at 21:34
  • \$\begingroup\$ @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). \$\endgroup\$
    – Ali1S232
    Jul 23, 2011 at 21:52
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Jul 23, 2011 at 22:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .