Uprising
Member
I cant seem to get Acceleration or deceleration of a unit to move the same regardless of the frame rate.
If a key is pressed then a constant amount of force is added to xForce (or yForce) and then a force in the opposite direction is added to xForce based on the velocity (xVel/6).
The xForce is added to the units xVelocity which in turn is added to its x position proportional to the time since last update (t->x += t->xVel * moveAmount.
This is the main code that iam using to move a unit.
CODE
int unit_move(struct unit *t)
{
int x;
struct unit *unitTemp;
struct unit *unitCurrent;
struct tile *tempTile = NULL;
float moveAmount;
int xdeceleration = 0;
int ydeceleration = 0;
moveAmount = (float)((gameTimer - t->speedTimer)/1000.0); // Get the amount of seconds past from previous movement
if(t->xVel != 0)
xdeceleration -= t->xVel/6;
if(t->yVel != 0)
ydeceleration -= t->yVel/6;
t->xForce += xdeceleration;
t->yForce += ydeceleration;
if((t->xVel > 0 || t->xVel < 0 ) && (xdeceleration) == 0)
{
t->xVel = 0;
}
if((t->yVel > 0 || t->yVel < 0 ) && (ydeceleration) == 0)
{
t->yVel = 0;
}
t->xVel += t->xForce;
t->yVel += t->yForce;
t->speedTimer = gameTimer;
t->xForce = 0;
t->yForce = 0;
t->x += t->xVel * moveAmount;
t->box.x = (int)t->x;
t->y += t->yVel * moveAmount;
t->box.y = (int)t->y;
return 0;
}
The unit has the same max velocity regardless of frame rate but its acceleration is still dependant on it.
I was wondering if anyone can see whats wrong or if i need to add anything?
If a key is pressed then a constant amount of force is added to xForce (or yForce) and then a force in the opposite direction is added to xForce based on the velocity (xVel/6).
The xForce is added to the units xVelocity which in turn is added to its x position proportional to the time since last update (t->x += t->xVel * moveAmount.
This is the main code that iam using to move a unit.
CODE
int unit_move(struct unit *t)
{
int x;
struct unit *unitTemp;
struct unit *unitCurrent;
struct tile *tempTile = NULL;
float moveAmount;
int xdeceleration = 0;
int ydeceleration = 0;
moveAmount = (float)((gameTimer - t->speedTimer)/1000.0); // Get the amount of seconds past from previous movement
if(t->xVel != 0)
xdeceleration -= t->xVel/6;
if(t->yVel != 0)
ydeceleration -= t->yVel/6;
t->xForce += xdeceleration;
t->yForce += ydeceleration;
if((t->xVel > 0 || t->xVel < 0 ) && (xdeceleration) == 0)
{
t->xVel = 0;
}
if((t->yVel > 0 || t->yVel < 0 ) && (ydeceleration) == 0)
{
t->yVel = 0;
}
t->xVel += t->xForce;
t->yVel += t->yForce;
t->speedTimer = gameTimer;
t->xForce = 0;
t->yForce = 0;
t->x += t->xVel * moveAmount;
t->box.x = (int)t->x;
t->y += t->yVel * moveAmount;
t->box.y = (int)t->y;
return 0;
}
The unit has the same max velocity regardless of frame rate but its acceleration is still dependant on it.
I was wondering if anyone can see whats wrong or if i need to add anything?