How can I create realistic gravity code for my video game?

AI Thread Summary
To create realistic gravity code for a video game, implement the kinematic equation y = y0 + vy0t + 0.5ayt², where y represents the position over time, y0 is the initial position, vy0 is the initial velocity, and ay is the acceleration due to gravity, typically -9.81 m/s². Define variables for height, width, and y-position, and update the y-position in a function that runs every 0.01 seconds. Ensure to use consistent units such as meters and seconds, and be mindful that the downward direction is often treated as positive in many coordinate systems. Consider the shape of objects for accurate collision modeling. This foundational understanding of gravity and motion will enhance the realism of the game.
AlbertRenshaw
Messages
14
Reaction score
0
Hello! I am a programmer and I am working on a video game, up until now I have used "gravity" to make objects fall just based off simple positioning code. I want to create realistic gravity code though!

Here is how the code works...
You simply create a variable and assign it a value..
ex. If I wanted to create a gravity constant I would say:

gravity = 5.485024534535

Now whenever I want to use this constant in my code / equations I will just type "gravity" instead of typing "5.485024534535"

You can set as many variables as you need..

I already have, height (the height of the object in the game), width (width in game), y (the y position (x-y cartesian coordinates) of the object, and a few other un-important things..

To set the y value of the ball you can say something like:

y = -50;
And the ball will move on the y-axis to -50 (coordinates of the top left corner of the screen = (0,0)

Dont worry about setting x-coordinate values in this code, it is falling in a straight path.

To define variables simply say:

variableName = variableNumber

I also have a function that is called every 0.01 seconds..

So inside this function if I were to say:

number1 = number1 + 5

and number1 starts out equal to 1, after 0.01 seconds it equals 1+5, another 0.01 seconds pass and it equals (1+5)+5, and so on.

Code should be in the following format:

{
Define variables here (each variable on a new line)
}

{{
stuff that happens every 0.01 seconds here
}}

Anyone up for the challenge?

Or even just want to help me understand how gravity works in moving things?
 
Physics news on Phys.org
Have you had any exposure to kinematics? If not, it sounds like you are ready to run with something without the fundamentals under your belt.
 
All I need is a falling code, everything else will "fall" into play! (pun intended)
 
AlbertRenshaw said:
Or even just want to help me understand how gravity works in moving things

Not sure I can help you understand "how gravity works in moving things", but, however it works, I will say its consistent enough to be described by the following formula which you will need to implement in your code:

y=y0 + vy0t + 0.5ayt2

y : y position as a function of time, t
y0 : y postion when time = 0
vy0 : initial velocity in the y direction when time = 0
ay : acceleration constant in the y direction (on Earth it is -9.81 m/s2)

Use proper units: meters (m), seconds (s).

This should get you started. Have fun!
 
Since the downward direction is positive in most GUI coordinate systems, the value of g in MKS will be ~9.81 m/s2 (positive), unless you manually implement another coordinate system.

The kinematics equations describe motion about the center of mass, so you'll need to also take the shape of the object into account if you want to model collisions.
 
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...
Back
Top