Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How to implement gravity

Author
Message
Game designer extraordinaire
15
Years of Service
User Offline
Joined: 11th Oct 2010
Location: Mars
Posted: 27th Oct 2010 03:27
Hello, once again it's me Game designer extraordinaire and right now I'm having a silly problem in the game I'm designing I cant seam to get gravity to work I have several ideas on how to do it but i cant get any of them to work correctly. Thank you in advance for the answer to my problem.

Yes...it is I
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 27th Oct 2010 16:23
Quote: "Please help me"

Is extremely unhelpful as a title for a thread. Anyone who is looking for a solution to the same problem as you will not find it if it is titled in this way.

A simple example of why proper titling is vital to these boards can be found by typing the word "Gravity" into the search bar at the bottom of the page...

gbark
20
Years of Service
User Offline
Joined: 14th Oct 2005
Location: US - Virginia
Posted: 27th Oct 2010 21:57
How are you handling all your other physics? There are many ways to implement gravity, some easier than others.

As a general implementation, your game objects will have positions, velocities, and accelerations. Your acceleration gets added to your velocity every game cycle, and your velocity gets added to your position every game cycle. Gravity is an example of a constant acceleration, and when you apply a constant acceleration in this manner, your object's position will exponentially increase (like real gravity).

This also has the added benefit of automatically adding "arcs" to your jump - That is, when you jump, you simply apply a strong upwards y acceleration for one game cycle, and then your code will automatically handle the object moving up, slowing down, and falling back at an increasing speed.

So, a simple system (in 2D) would be something like:

Game designer extraordinaire
15
Years of Service
User Offline
Joined: 11th Oct 2010
Location: Mars
Posted: 28th Oct 2010 04:57
thank you, baxslash, for your rude comment. However I do appreciate gbark's comment and help. if you are going to post on this thread then please make it useful and friendly. Oh and gbark-I haddent thaught of that, thanks.

Yes...it is I
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 28th Oct 2010 10:04 Edited at: 28th Oct 2010 10:06
Game Designer, you may think Baxslash is being rude, but he is only iterating one of the rules of the forum which I'm sure you read when signing up(!)...

Quote: "4) Please be verbose when asking your questions! Please dont post with a thread title "HELP ME!!!!" instead, try and be more informative, for instance "Please help me with functions." It is also a good idea to post some of your problem code and your system specifications so we can have a better idea of what is going on."


Also, when posting a question you should consider this...

Quote: "6) Also, when you need help debugging one of your progs, please, PLEASE include a snippet with your post. It is almost impossible to help you solve any problems if we dont know what the program looks like."


You'll find everything you need to know to make your experience a pleasurable one right here.


I've changed your thread title to make it more user friendly. You'll find that you get more responses with a good title

baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Oct 2010 11:11
Quote: "thank you, baxslash, for your rude comment. However I do appreciate gbark's comment and help. if you are going to post on this thread then please make it useful and friendly. Oh and gbark-I haddent thaught of that, thanks."

I'm sorry if you thought I was being rude, perhaps I was a little and I see this was only your third thread so perhaps I should have been a little softer in my approach.

Gravity is a subject that has been covered about a thousand times on these forums. I guess the question is how realistic do you want it to be? If you are just doing a simple platform game then realism is far from the most important thing you need to consider. If you are planning to use a physics system (such as ODE or DarkPhysics) then gravity is mostly taken care of for you.

gbark's example is a pretty simple one and very easy to impliment in your first game. I would worry about using more complex systems later and use this simple example to give you something to get started with.

I hope we haven't got off on the wrong foot, I really do try to help where I can, as I'm sure you'll find out. You will find there are people who get annoyed at posts that don't conform to the 'rules' and some who will be much more rude about it than me!

This is a friendly and helpful community for the most part though and quite unique in the industry for its community spirit. You are most welcome to it!

sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 28th Oct 2010 19:51 Edited at: 28th Oct 2010 19:54
Quote: "// Apply gravity
yAccel# = gravity#

// Apply acceleration to velocity
xVelocity# = xVelocity# + xAccel#
yVelocity# = yVelocity# + yAccel#

// Apply velocity to position
xPos# = xPos# + xVelocity#
yPos# = yPos# + yVelocity#"


Hmmm... this defies basic things I knew about gravity! (Not to be rude or anything).

Gravity is, or at least always should be, a positive value. This is because mass and distance are always positive too, so therefore nothing would ever make it negative.

Also, in order to find acceleration, velocity, and your eventual position you will need time (game cycles might not be the best thing to go off of), so I take it that your code example assumes moving in the same time intervals?

This should be a more general approach to gravity:


If at first you dont succeed, LOWER YOUR STANDARDS.
Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 28th Oct 2010 21:53
Quote: "Gravity is, or at least always should be, a positive value."


Does it really matter? It depends on how you implement it that matters.

gbark
20
Years of Service
User Offline
Joined: 14th Oct 2005
Location: US - Virginia
Posted: 28th Oct 2010 22:04
Quote: "Gravity is, or at least always should be, a positive value. This is because mass and distance are always positive too, so therefore nothing would ever make it negative."

That's simply a matter of choice, and it depends on your implementation. It also depends on how your axes are aligned (for example, in 2D games, the positive Y axis points down, while in the 3D world, the positive Y axis points up.... And in 3D math, it's actually the Z axis that points up!)

Quote: "Also, in order to find acceleration, velocity, and your eventual position you will need time (game cycles might not be the best thing to go off of), so I take it that your code example assumes moving in the same time intervals?"


Again, that depends on how you're scaling your values. If you were using Timer Based Movement, however, you would be right - You'd need to multiply by some interval to account for the difference in time.

However, what you have here
is not correct. Gravity is generally considered to be a constant force, and by extension, a constant acceleration. If you're increasing the magnitude of your acceleration every game cycle to account for gravity, you will not get correct results. You want to increase the velocity every cycle to account for gravity, which is what the original code snippet does.
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 28th Oct 2010 22:13
It's amazing how everyone always argues over the 'best' way to do gravity. There are only two ways to do it.

1-Use Newtons laws for gravity
2-Use an approximation

I almost always use an approximation and you can never tell the difference. Frankly, if your game is so boring people are thinking "that guy isn't jumping quite in accordance with the laws of gravity" then you have bigger problems!

BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Oct 2010 00:11
Approximation is good, all hail approximation!

If you're looking to shave a few milliseconds off your logic, you need to cut down on precision calculations, they're for scientists and propellor heads.

For example, trigonometry is so much faster with a lookup table for angles 1 to 180. If you need more precision, take tenths between 2 values, it's pretty damn close and so much faster than SIN, COS and TAN.

sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 30th Oct 2010 05:13
Quote: "Gravity is generally considered to be a constant force, and by extension, a constant acceleration."


You're right... silly mistake on my part forgive me?

Quote: "It's amazing how everyone always argues over the 'best' way to do gravity."


I'm sorry... I'm a physics major focusing in gravitation... forgive me too?

If at first you dont succeed, LOWER YOUR STANDARDS.
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 30th Oct 2010 09:15
*cough cough almost everything on a computer is an approximation expecially for floats cough cough*

it's just a matter of how accurate you want it to be b][/b])

gbark
20
Years of Service
User Offline
Joined: 14th Oct 2005
Location: US - Virginia
Posted: 30th Oct 2010 16:08
Quote: "Frankly, if your game is so boring people are thinking "that guy isn't jumping quite in accordance with the laws of gravity" then you have bigger problems!"


Nonsense. If you can find a way to bring players' attention to the intricate workings of your gravity system - That's a good thing!

But really though, I don't think game-gravity itself is really that complicated. Now an entire physics engine can get really complex, obviously, and for those you need to choose between basing your system on actual Newtonian laws of physics, or just "winging it".

But, discounting the need to handle things like bounces or realistic collisions with objects of different masses, gravity (for our purposes) really is just an acceleration. I assume the OP was asking about the question in terms of something like a platformer or simple FPS, in which case a simple solution of applying a constant y-accel is perfectly realistic enough.
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 30th Oct 2010 20:00
Quote: "Nonsense. If you can find a way to bring players' attention to the intricate workings of your gravity system - That's a good thing!"

Yet again this comes down to what your game is about. If your game has a lot of realistic elements and requires a realistic physics system it's unlikely you'll be programming the physics system from scratch anyway as there are so many cheap/free proprietry systems out there... if however your game is "Mr.bouncy frog goes to town" or something a lot less realistic then a 'realistic' gravity system is far from important. Not "nonsense", just common sense.

Nobody complains when Mario jumps ten times his own height with an un-realistic sumersault half way through, why? Because it doesn't impact on the playability of the game. In fact the gameplay can rely on unrealistic gravity.

I hope you can see that was my point.

sneaky smith12
20
Years of Service
User Offline
Joined: 30th Apr 2006
Location: Like id tell you, oh wait i just did
Posted: 30th Oct 2010 20:39
Quote: "Nobody complains when Mario jumps ten times his own height with an un-realistic sumersault half way through, why? Because it doesn't impact on the playability of the game. In fact the gameplay can rely on unrealistic gravity."


That, and it would be impossible to get to over half the ledges in the game

If at first you dont succeed, LOWER YOUR STANDARDS.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 30th Oct 2010 20:48
What's the point in making a 100% realistic game when you've got real life for that

[b]
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 31st Oct 2010 10:36 Edited at: 31st Oct 2010 10:40
I'd like to point out that this kind of approximation is used even in scientific research. There are of course better approximation methods, like Runge-Kutta which requires more multiplications, but the base idea is still the same (Runge-Kutta basically uses this method twice on half the interval). The method you guys keep proposing is a variation of the so called Euler method for approximation on differential equations. It didn't get a name for nothing. It is often used due to its speed and relatively good approximations if the pass is small (the pass is the increment in time).

The only way to get an exact trajectory, is by using symbolic computation, and that is definitely not fast enough to be used in games once you get to a few more forces working on your free body (drag forces or spring forces). I guess it would be possible to use different hand-solved equations each time gravity kicks in, but still... I'd choose Euler over this anytime.

Cheers!
Sven B

Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 1st Nov 2010 06:53
Just handle the movement with vectors (not the built in kind, but i'm sure those would probably work too). I usually use one for horizontal movement and one for vertical movement.

What you do is make a Variable for VerticalSpeed#. If its positive then player is going up, if its negative then player is going down. Every cycle subtract a value for the gravity. This will cause player to start accelerating downward. If player jumps add a value (just once) and player will accelerate upward.

As player accelerates upward with a single jump value, gravity which is a smaller value is always being subtracted will eventually win out and pull player back down.

When player hits the ground (or lower), override it all by seting player equal to the ground and making the VerticalSpeed# 0.


For Horizontal movement Store the Angle# and Speed#. When player turns Angle# changes (or maybe only if player is on the ground) and if player walks or jumps have Speed# be added or overriden to a single value. And have Speed# be reset if player isnt moving and is on the ground.

Next all you do is add VerticalSpeed# to the Y value of the player object. And use the Move Object command for horizontal movement. Rotate Object to Angle# Then "Move Object" at a value of Speed#, then rotate the Object back to what it was.

This is probably the simplest way to have a full range of realistic motion. But theres a few things that need to be changed if you start getting into more complicated Player Actions,collisions, and etc.

TL;DR
Keep track of Horizontal Angle, Horizontal Speed, Vertical Speed. Constantly subtract a value from Vertical Speed for gravity.

Game designer extraordinaire
15
Years of Service
User Offline
Joined: 11th Oct 2010
Location: Mars
Posted: 2nd Nov 2010 02:08
Thanks for everything! I received allot of informative information *funny how information works that way* and am glad to say that I finaly managed to get it working.

Yes...it is I

Login to post a reply

Server time is: 2026-07-22 04:37:15
Your offset time is: 2026-07-22 04:37:15