L
Luc The Perverse
I just wrote this piece of code for moving an object given an intial
position, velocity and acceleration.
protected float X=0.0f;
protected float Y=0.0f;
protected float velX=0.0f;
protected float velY=0.0f;
protected float accX=0.0f;
protected float accY=100.0f; //gravity
protected float DesiredVelocityX=0.0f;
protected float ApplyDiscreteMovement(float Pos, float Vel, float Acc,
float Time){
return Pos + Time * Vel + Acc * Time * Time /2.0f;
}
protected float AdjustVelocity(float Vel, float Acc, float Time){
return Vel + Time * Acc;
}
public void MoveMe(float ElapsedTime){
if(accX>0){
velX = AdjustVelocity(velX, accX, ElapsedTime);
if(velX>=DesiredVelocityX){ //reached desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
} else if(accX<0){
velX = AdjustVelocity(velX, accX, ElapsedTime);
if(velX<=DesiredVelocityX){ //reached desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
}
velY = AdjustVelocity(velY, accY, ElapsedTime);
X = ApplyDiscreteMovement(X, velX, accX, ElapsedTime);
Y = ApplyDiscreteMovement(Y, velY, accY, ElapsedTime);
}
The redundancy that I am concerned about is in reaching the desired
velocity. I am using a nested if statement. First I check which way they
are accelerating to know whether to use a greater or lesser than in the
comparison to see if it has been reached or passed.
I was wondering if there were a better way to do this?
I don't want to multiple both velocities by the acceleration, this seems
like an unneccesary amount of overheard.
I did come up with this, but I think it further obfuscates the intent.
if(accX!=0.0f){
velX = AdjustVelocity(velX, accX, ElapsedTime);
if((accX<0) == (velX<DesiredVelocityX)){ //passed desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
}
I realize that this is not identical since it isn't testing for velX ==
DesiredVelocityX, but as this is the only places where accX is even looked
at, it has the same effect.
Is there a better way to do this?
position, velocity and acceleration.
protected float X=0.0f;
protected float Y=0.0f;
protected float velX=0.0f;
protected float velY=0.0f;
protected float accX=0.0f;
protected float accY=100.0f; //gravity
protected float DesiredVelocityX=0.0f;
protected float ApplyDiscreteMovement(float Pos, float Vel, float Acc,
float Time){
return Pos + Time * Vel + Acc * Time * Time /2.0f;
}
protected float AdjustVelocity(float Vel, float Acc, float Time){
return Vel + Time * Acc;
}
public void MoveMe(float ElapsedTime){
if(accX>0){
velX = AdjustVelocity(velX, accX, ElapsedTime);
if(velX>=DesiredVelocityX){ //reached desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
} else if(accX<0){
velX = AdjustVelocity(velX, accX, ElapsedTime);
if(velX<=DesiredVelocityX){ //reached desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
}
velY = AdjustVelocity(velY, accY, ElapsedTime);
X = ApplyDiscreteMovement(X, velX, accX, ElapsedTime);
Y = ApplyDiscreteMovement(Y, velY, accY, ElapsedTime);
}
The redundancy that I am concerned about is in reaching the desired
velocity. I am using a nested if statement. First I check which way they
are accelerating to know whether to use a greater or lesser than in the
comparison to see if it has been reached or passed.
I was wondering if there were a better way to do this?
I don't want to multiple both velocities by the acceleration, this seems
like an unneccesary amount of overheard.
I did come up with this, but I think it further obfuscates the intent.
if(accX!=0.0f){
velX = AdjustVelocity(velX, accX, ElapsedTime);
if((accX<0) == (velX<DesiredVelocityX)){ //passed desired velocity
accX = 0.0f;
velX=DesiredVelocityX;
}
}
I realize that this is not identical since it isn't testing for velX ==
DesiredVelocityX, but as this is the only places where accX is even looked
at, it has the same effect.
Is there a better way to do this?