Formula for gravity for a game
blargmob

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?
?????????

Kauhiz

Code?

blargmob

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}

Kris Asick

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

ImLeftFooted

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
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.

blargmob

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

Krzysztof Kluczek
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

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.

blargmob

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)

ImLeftFooted
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

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

ImLeftFooted
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
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.

blargmob

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!?

CursedTyrant
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.

blargmob

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

CursedTyrant

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.

LennyLen
Quote:

What's wrong with it!?

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

Kris Asick
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

blargmob

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

CursedTyrant

Short: Yes.

Kris Asick
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

James Stanley

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

O.K. That helps. Thanks guys.

Thread #590370. Printed from Allegro.cc