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.
 
Thread 'Voltmeter readings for this circuit with switches'
TL;DR Summary: I would like to know the voltmeter readings on the two resistors separately in the picture in the following cases , When one of the keys is closed When both of them are opened (Knowing that the battery has negligible internal resistance) My thoughts for the first case , one of them must be 12 volt while the other is 0 The second case we'll I think both voltmeter readings should be 12 volt since they are both parallel to the battery and they involve the key within what the...
Thread 'Correct statement about a reservoir with an outlet pipe'
The answer to this question is statements (ii) and (iv) are correct. (i) This is FALSE because the speed of water in the tap is greater than speed at the water surface (ii) I don't even understand this statement. What does the "seal" part have to do with water flowing out? Won't the water still flow out through the tap until the tank is empty whether the reservoir is sealed or not? (iii) In my opinion, this statement would be correct. Increasing the gravitational potential energy of the...
Back
Top