Edit
Just for clarification, my sprite's 'movement' isn't the problem. If I set my Time variable to 4 seconds, then it will cross the screen in exactly 4 seconds regardless of logic updates rate per second, rendering rate per second or screen resolution. So I am pretty sure I'm scaling the sprite's movement correctly. What I'm pretty sure I'm not doing correctly, is scaling acceleration.
Original Question
I'm trying to implement gravity in my 2d platformer and am having a few problems understanding how to keep it consistent when I change my updates per second.
Here's what I have.
My Gameloop overview
Currently, my gameloop renders at the maximum rate allowed by device and the updates are 'clamped' to an upper limit (At the moment, 60 per second). I am working on the assumption that most of the time, my game will have no problem hitting this, even if the actual rendering dips. Thus I am doing all of my calculations based Delta Time derived from this fixed 'ticksPerSecond' value.
I don't know for sure that this will remain at 60, I may decide at some point during the development to lower this upper limit.
My Gravity - variable declaration and initial values
At the moment, I have:
float spriteYTime=7f; //This is the (initial) amount in seconds that this sprite will take to move from the top of the screen to the bottom.
float fallAccel=.5; //This is the value that will subtracted from he sprite's fall speed (To make it fall ever faster)
float terminalVelocity=1.5f; //Cap speed at this rate (1.5 seconds)
my sprite's position is worked out on dt as follows:
//Delta time
float ticksPerSecond = 60;
float dt = 1f/ticksPerSecond;
//Velocity
spriteYVel = 1/spriteYTime;
//Update position
spriteYReal = spriteYReal + (spriteYVel * dt);
//Convert to screen coordinates (will be drawn at this Y coordinate)
spriteyScreen=(int) (furmanYReal*height);
My Gravity - code
//If my sprite's state is 'f' (meaning falling) then apply gravity
if(sprite.getState('f')==true){
//Calculate new position
spriteYReal = spriteYReal + (spriteYVel * dt);
//Convert to screen coordinates
sprite.yScreen=(int) (spriteYReal*r.height);
//Reduce time by fallAccel amount & update velocity value based on new time value (So sprite falls slightly faster this frame compared to the last
spriteYTime-=fallAccel;
spriteYVel = 1/spriteYTime;
//Check that speed isn't faster than terminal velocity
if (spriteYTime<spriteVelocity)
spriteYTime=spriteVelocity;
}
The problem
Now, this does work, but if I change my ticksPerSecond value, it goes wrong (it falls at different rates).
I know that 'Earth Normal' gravity is approximately 9.8 meters per second per second, but this is measured 'per second' whereas I (think) I need to work with 'per frame'. This is where I'm getting confused.
So, take this example:
If set my initial Time value to 4 seconds, then if it remained constant at this speed, it would take 4 seconds to reach the bottom, if I changed my ticksPerSecond, because the time value is worked out using delta, it would still take 4 seconds but if I apply 'fallAccel" to the Time value (i.e. subract it), it goes wrong when I change the ticksPerSecond value - why?
How can I get this to fall at the same rate regardless of the value of 'ticksPerSecond' - any help in understanding this would be appreciated.