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

Click For 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.
 
The book claims the answer is that all the magnitudes are the same because "the gravitational force on the penguin is the same". I'm having trouble understanding this. I thought the buoyant force was equal to the weight of the fluid displaced. Weight depends on mass which depends on density. Therefore, due to the differing densities the buoyant force will be different in each case? Is this incorrect?

Similar threads

  • · Replies 27 ·
Replies
27
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
7
Views
2K
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 0 ·
Replies
0
Views
656
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
1
Views
2K
Replies
5
Views
3K