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

Click For Summary

Homework Help Overview

The discussion revolves around creating realistic gravity code for a video game, focusing on the implementation of gravity in a programming context. The original poster seeks to enhance their current simplistic approach to gravity by incorporating more realistic physics principles.

Discussion Character

  • Exploratory, Conceptual clarification, Mathematical reasoning

Approaches and Questions Raised

  • Participants discuss the fundamentals of kinematics and how they relate to the implementation of gravity in code. There are inquiries about the original poster's understanding of the underlying physics and suggestions for incorporating specific formulas related to motion.

Discussion Status

Some participants have offered guidance on the physics of gravity and its mathematical representation, while others question the original poster's foundational knowledge in kinematics. Multiple interpretations of how to approach the coding of gravity are being explored, but no consensus has been reached.

Contextual Notes

There is a mention of the coordinate system used in the game, which may affect the implementation of gravity, particularly regarding the direction of acceleration due to gravity.

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.
 

Similar threads

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