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.

Dark GDK / Gravity!

Author
Message
SliZe
14
Years of Service
User Offline
Joined: 21st Oct 2010
Location:
Posted: 2nd Nov 2010 22:50
Hay everyone!

Im working on a little game and im wondering how i can get the gravity to work. What is the simplest way to make gravity, WHITOUT downloading a new library?

-SliZe
Cuddle Bunniezzz 12
15
Years of Service
User Offline
Joined: 14th Jan 2009
Location: Buffalo, NY
Posted: 3rd Nov 2010 02:54
It would really help if you could be a bit more descriptive on what kind of game you are developing (2D or 3D), but the most simpliest way would be to create a variable and then in your game loop, subtract that value from all of your Y values. Just make sure you have some sort of ground for your objects to collide with so they just don't keep on falling.

Ex:


Understand?

http://ref.darkgdk.us/ <- Online DarkGDK Refernece. More content coming soon.
Fatal Berserker
14
Years of Service
User Offline
Joined: 2nd Jul 2010
Location:
Posted: 3rd Nov 2010 05:44 Edited at: 3rd Nov 2010 05:44


If you want the equation.

Quote: "yPos -= GRAVITY;"


You should make it speed up as time goes on, rather than constantly the same.

Earths gravity, is roughly 9.8.

Smoke me a kipper, ill be back for breakfast.

MMORPG -- Many Men Online Role Playing as Girls

G.I.R.L -- Guy In Real Life
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 3rd Nov 2010 19:56
I helped someone recently to implement gravity, here is the thread:

http://forum.thegamecreators.com/?m=forum_view&t=176441&b=22

If you don't want to read the whole thing, the most relevant part is this:

"the usual method of implementing gravity is: there is a vertical speed for the jump, which is initially high, then it is decreased every loop by a constant gravity value. The speed is then added to the vertical position. Thus, the vertical position is changing by different amounts. You can play with the initial vertical speed and the gravity value to make the jump higher or smaller."
SliZe
14
Years of Service
User Offline
Joined: 21st Oct 2010
Location:
Posted: 4th Nov 2010 19:19 Edited at: 4th Nov 2010 20:11
Thanks!

I'll test this
Its 2D BTW.




EDIT: Im having problems whit geting the player down. Im trying to get the player move down again on a sertan height, but i can't.

help?


Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 4th Nov 2010 21:25
You didn't really implement vertical speed because you change the Y coordinate directly and test the height for "falling down". It's possible but in this case the jump will be always at the same speed which is not realistic. Nevertheless, I give you a solution for this.

At the beginning of the program, we need more variables:



I introduced STARTY because we need to check when the player is back on ground to stop the jump and it's better to check a name then a hardcoded pixel value. And the "Jumping" flag is needed otherwise the sprite would jump only as long as you hold the space key down, which is not realistic either. Instead it should move as long as it is in jumping state. Here is the movement code. I changed the height to 50 because 10 was too small to test with.



But this is not really gravity, it is just changing the Y position with a constant amount every loop. With gravity, the vertical speed of the jump is changing: first it's high, then decreases, then becomes negative (start falling) and then falling faster. Here is the implementation with vertical speed. In this, you don't test the height, instead you can change the starting vertical speed to adjust how high you can jump.

Start with the variables again, notice the new ySpeed:



and the movement:

SliZe
14
Years of Service
User Offline
Joined: 21st Oct 2010
Location:
Posted: 4th Nov 2010 21:48
Seriusly! THANKS SOOOOOOOOOOOOO MUCH!!!

Login to post a reply

Server time is: 2024-11-19 16:29:29
Your offset time is: 2024-11-19 16:29:29