# How to increase the acceleration of an object on the Y axis? [duplicate]

So far I am able to make the character fall at a constant rate but I am unsure how to make the character accelerate while falling.

When the character is falling I have a line of code as such:

position.y -= 7f;


How can I change my implementation such that the character falls faster over time?

position.y -= 7f;


change 7f to a variable

var fallspeed = 7.0f;
position.y -= fallspeed;


increase the fallspeed every update

var fallspeed = 7.0f;
var gravity = 5.0f;
position.y -= fallspeed;
fallspeed += gravity;


This creates a linear acceleration. Gravity on the other hand isnt linear, so you might wanna try something like this

var fallspeed = 7.0f;
var gravity = 2.0f;
position.y -= fallspeed;
fallspeed *= gravity; // *= instead of +=


values are totally of, you will have to try around a bit.

• While you are technically correct that gravity isn't a linear acceleration, this is irrelevant unless you're dealing with mechanics at an orbital level, in which case gravity will have a 2D or 3D direction instead of just being straight down. – Icy Defiance Aug 21 '14 at 20:02
• Do I need to make a new field when I change 7f to a variable? I am getting an error that says "var cannot be resolved to a type." – Filo Doxia Aug 22 '14 at 1:29
• I changed the variables to floats and the fallspeed *= gravity to position.y *= gravity and got it to work. – Filo Doxia Aug 22 '14 at 18:38
• @FiloDoxia var is just a short-hand notation used in some languages like C# to declare a variable without explicitly stating the type. – ashes999 Aug 24 '14 at 15:09

Change your thought process from "Moving 7 units" to "Applying a velocity of 7 units" and you can come up with something like:

velocity.Y = -7;
position.Y += velocity.Y;


Now if we take this and we want to apply acceleration every frame we can simply update the velocity every frame.

accelerationRate = 2; //arbitrarily picked - no significance
velocity.Y = velocity.Y + accelerationRate;
position.Y += velocity.Y;


A more appropriate and less naive approach would take elapsed time into account. This results in consistent distance traversed over time. To implement this we are required to somehow track the amount of time elapsed since the last update. Some libraries will include this functionality but otherwise using time keeping measures built into your language of choice are likely sufficient. This value will be stored in deltaTime in the sample below and will expressed in seconds.

accelerationRate = 2;
velocity.Y = velocity.Y + (accelerationRate * deltaTime); //We should only increase our accelration based on the elapsed time
position.Y += velocity.Y * deltaTime; //Only apply velocity as much as we should given the amount of elapsed time


You could take this and easily apply it to moving in either direction on the Y axis, up or down. Your velocity should just be positive or negative to behave appropriately as needed.