Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Formula for gravity for a game

This thread is locked; no one can reply to it. rss feed Print
Formula for gravity for a game
blargmob
Member #8,356
February 2007
avatar

Howdy,

I want to make a game the includes a gravity vector to some balls that bounce around the screen. I will be adding the gravity vector to the balls speed vector every loop with a 10 millisecond rest. I am using the gamespace to screenspace (Vise-Versa) formulas to find the gravity in pixels per second, than dividing that by 100 to get the gravity in pixels per 10 milliseconds, but when I run this in my program, I doesn't seem right. It looks like the balls move way to fast. Any suggestions?
?????????

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

Kauhiz
Member #4,798
July 2004

Code?

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

blargmob
Member #8,356
February 2007
avatar

This code is for a gamespace of 100 ft by 50 ft
It's not so much the code that's wrong (I think) but the formula for gravity that I'm using. Anyway, here ya are:

1int gravity=4;//After I did the formula. (4 pixels every 10 milliseconds)
2int ballx=0;
3int bally=0;
4int ballxspeed=8;
5int ballyspeed=0;
6 
7while(!key[KEY_ESC])
8{
9 ballyspeed+=gravity;
10 
11 ballx+=ballxspeed;
12 bally+=ballyspeed;
13 
14 if(bally>=//The screen hieght. I.E. 600
15 ballyspeed*=-1;
16 if(ballx>=//The screen width. I.E. 800
17 ballxspeed*=-1;
18 
19 //Render the ball
20}

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

Kris Asick
Member #1,424
July 2001

Your formula's perfect. Gravity is nothing more than linear acceleration, and that's what your code is doing.

The reason it goes super fast is because consider that by then end of 100 frames, or 1 second, if there was no floor your ball's y-speed would be 400 units every 10 ms. You need to either switch to floating point values and use much smaller speed increments, reduce the game logic to process at a slower rate, (such as 20 ms instead of 10 ms), or both.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

ImLeftFooted
Member #3,935
October 2003
avatar

4 pixels every 10 ms is far too fast. Games aren't realistic, just fudge some numbers.

Maybe something closer to 10 pixels a second would be good.

gnolam
Member #2,030
March 2002
avatar

blargmob said:

I am using the gamespace to screenspace (Vise-Versa) formulas to find the gravity in pixels per second, than dividing that by 100 to get the gravity in pixels per 10 milliseconds, but when I run this in my program, I doesn't seem right.

If you're trying to model any kind of real-world physics, use real-world units in all the calculations and only scale when rendering. If not, forget about the real world completely and think only in terms of your game's world space.

blargmob said:

This code is for a gamespace of 100 ft by 50 ft

Related to the previous point: if you're going to use real units anywhere, at least use logical ones. :P

And use floating point. It's not 1995 anymore.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

blargmob
Member #8,356
February 2007
avatar

If I used smaller speed increments, then the gravity would not be to scale.

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

Krzysztof Kluczek
Member #4,191
January 2004
avatar

Quote:

I am using the gamespace to screenspace (Vise-Versa) formulas to find the gravity in pixels per second

Gravity is acceleration, so its unit will be pixel/second^2. :)

EDIT: Also I strongly suggest using floats or fixed point fractional numbers. If you do calculations every 10 miliseconds you'll surely need subpixel precision. :)

Elverion
Member #6,239
September 2005
avatar

As I have once read, "When it comes to games, if it looks right, it is right." Basically meaning that you shouldn't worry too much about about how true to life it is, but more that it "feels" right. I usually have the gravity variable of an object accelerate at about 0.05 to 0.1 per frame (target FPS is usually about 60) with a max of somewhere between 5 and 10. You've just got to play with it a bit.

--
SolarStrike Software - MicroMacro home - Automation software.

blargmob
Member #8,356
February 2007
avatar

How does pixels per seconds^2 affect my code?

EDIT: I don't want to play with, I just want to set the variable to what it would look like in real ife. (Depending on gamespace and screenspace)

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

I don't want to play with, I just want to set the variable to what it would look like in real ife. (Depending on gamespace and screenspace)

Why?

blargmob
Member #8,356
February 2007
avatar

So it has realistic effects on the game. (This is if I'm making a game that is "real", like basketball or something).

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

realistic

You don't need to get exact numbers for gravity to be realistic. Being realistic has nothing to do with that.

It sounds like what you're really saying is you want your game to be real which is not possible in real time. So if you have to fake large portions of the 'reality' anyway, whats one more thing?

gnolam
Member #2,030
March 2002
avatar

Quote:

EDIT: I don't want to play with, I just want to set the variable to what it would look like in real ife. (Depending on gamespace and screenspace)

Then re-read the thread. Or read it, as it may be.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

blargmob
Member #8,356
February 2007
avatar

O.K. All I want to do is convert the acceleration of gravity on earth (9.8 m/s^2) into the accelaration of gravity in my game using gamespace to screenspace. I have done this, and the acceleration of the balls on the screen look TO FAST! What's wrong with it!?

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

CursedTyrant
Member #7,080
April 2006
avatar

Quote:

if(bally>=//The screen hieght. I.E. 600
      ballyspeed*=-1;
if(ballx>=//The screen width. I.E. 800
      ballxspeed*=-1;

It's off-topic, but... wouldn't this

if(bally>=SCREEN_H)
      ballyspeed*=-1;
if(ballx>=SCREEN_W)
      ballxspeed*=-1;

be better?

In case you have already implemented that in your code and just put a comment there for some unfathomable reason, ignore this post.

---------
Signature.
----
[My Website] | [My YouTube Channel]

blargmob
Member #8,356
February 2007
avatar

That doesn't help my problem......

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

CursedTyrant
Member #7,080
April 2006
avatar

Helping your problem was not the point of my post. Pointing out stuff that might be of some use in the future, was. I just thought I'd point it out in case you didn't know about it.

---------
Signature.
----
[My Website] | [My YouTube Channel]

LennyLen
Member #5,313
December 2004
avatar

Quote:

What's wrong with it!?

You've already been told the answer to that question.

Kris Asick
Member #1,424
July 2001

Quote:

O.K. All I want to do is convert the acceleration of gravity on earth (9.8 m/s^2) into the accelaration of gravity in my game using gamespace to screenspace. I have done this, and the acceleration of the balls on the screen look TO FAST! What's wrong with it!?

You're trying to model real world physics with too few of the components.

If you want to implement real gravity you must also create systems for units of measurement, object mass, volume, friction, drag, kinetic transfer of energy, time...

The only solution to your problem is to design all that. Then and only then will your gravity be realistic. Anything beforehand will be arbitrary, and 99% of game developers settle for arbitrary because as you can see, modeling real world physics is a major chore and not to be attempted by anyone but the most dedicated and experienced in both programming and physics.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

blargmob
Member #8,356
February 2007
avatar

So should I just "play around" with the gravity vector until seems suitible?

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

CursedTyrant
Member #7,080
April 2006
avatar

Short: Yes.

---------
Signature.
----
[My Website] | [My YouTube Channel]

Kris Asick
Member #1,424
July 2001

Quote:

So should I just "play around" with the gravity vector until seems suitible?

Exactly.

And it wouldn't hurt to switch everything to use floating point variables so you can do smaller increments than 1.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

James Stanley
Member #7,275
May 2006
avatar

That's what everybody else does...
To be honest, a gamer doesn't care how realistic it is, they care how realistic it looks.

blargmob
Member #8,356
February 2007
avatar

O.K. That helps. Thanks guys.

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

Go to: