|
|
| Formula for gravity for a game |
|
blargmob
Member #8,356
February 2007
|
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
Member #4,798
July 2004
|
Code? --- |
|
blargmob
Member #8,356
February 2007
|
This code is for a gamespace of 100 ft by 50 ft
--- |
|
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) --- Kris Asick (Gemini) |
|
ImLeftFooted
Member #3,935
October 2003
|
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
|
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. And use floating point. It's not 1995 anymore. -- |
|
blargmob
Member #8,356
February 2007
|
If I used smaller speed increments, then the gravity would not be to scale. --- |
|
Krzysztof Kluczek
Member #4,191
January 2004
|
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
|
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
Member #8,356
February 2007
|
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
Member #3,935
October 2003
|
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
|
So it has realistic effects on the game. (This is if I'm making a game that is "real", like basketball or something). --- |
|
ImLeftFooted
Member #3,935
October 2003
|
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
|
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
Member #8,356
February 2007
|
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
Member #7,080
April 2006
|
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 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
Member #8,356
February 2007
|
That doesn't help my problem...... --- |
|
CursedTyrant
Member #7,080
April 2006
|
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
Member #5,313
December 2004
|
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) --- Kris Asick (Gemini) |
|
blargmob
Member #8,356
February 2007
|
So should I just "play around" with the gravity vector until seems suitible? --- |
|
CursedTyrant
Member #7,080
April 2006
|
Short: Yes. --------- |
|
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) --- Kris Asick (Gemini) |
|
James Stanley
Member #7,275
May 2006
|
That's what everybody else does... |
|
blargmob
Member #8,356
February 2007
|
O.K. That helps. Thanks guys. --- |
|
|