<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Formula for gravity for a game</title>
		<link>http://www.allegro.cc/forums/view/590370</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 04 Mar 2007 05:10:38 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Howdy,</p><p>     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&#39;t seem right. It looks like the balls move way to fast. Any suggestions?<br /><img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /><img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /><img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (blargmob)</author>
		<pubDate>Sun, 04 Mar 2007 01:01:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Code?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kauhiz)</author>
		<pubDate>Sun, 04 Mar 2007 01:21:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This code is for a gamespace of 100 ft by 50 ft<br />It&#39;s not so much the code that&#39;s wrong (I think) but the formula for gravity that I&#39;m using. Anyway, here ya are:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">int</span> gravity<span class="k3">=</span><span class="n">4</span><span class="k2">;</span><span class="c">//After I did the formula. (4 pixels every 10 milliseconds)</span></td></tr><tr><td class="number">2</td><td><span class="k1">int</span> ballx<span class="k3">=</span><span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">3</td><td><span class="k1">int</span> bally<span class="k3">=</span><span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td><span class="k1">int</span> ballxspeed<span class="k3">=</span><span class="n">8</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td><span class="k1">int</span> ballyspeed<span class="k3">=</span><span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td><span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span></td></tr><tr><td class="number">8</td><td><span class="k2">{</span></td></tr><tr><td class="number">9</td><td>   ballyspeed<span class="k3">+</span><span class="k3">=</span>gravity<span class="k2">;</span></td></tr><tr><td class="number">10</td><td>&#160;</td></tr><tr><td class="number">11</td><td>   ballx<span class="k3">+</span><span class="k3">=</span>ballxspeed<span class="k2">;</span></td></tr><tr><td class="number">12</td><td>   bally<span class="k3">+</span><span class="k3">=</span>ballyspeed<span class="k2">;</span></td></tr><tr><td class="number">13</td><td>&#160;</td></tr><tr><td class="number">14</td><td>   <span class="k1">if</span><span class="k2">(</span>bally&gt;<span class="k3">=</span><span class="c">//The screen hieght. I.E. 600</span></td></tr><tr><td class="number">15</td><td>      ballyspeed<span class="k3">*</span><span class="k3">=</span><span class="k3">-</span><span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>   <span class="k1">if</span><span class="k2">(</span>ballx&gt;<span class="k3">=</span><span class="c">//The screen width. I.E. 800</span></td></tr><tr><td class="number">17</td><td>      ballxspeed<span class="k3">*</span><span class="k3">=</span><span class="k3">-</span><span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>&#160;</td></tr><tr><td class="number">19</td><td>   <span class="c">//Render the ball</span></td></tr><tr><td class="number">20</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (blargmob)</author>
		<pubDate>Sun, 04 Mar 2007 01:49:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Your formula&#39;s perfect. Gravity is nothing more than linear acceleration, and that&#39;s what your code is doing.</p><p>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&#39;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.</p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Sun, 04 Mar 2007 02:04:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>4 pixels every 10 ms is far too fast.  Games aren&#39;t realistic, just fudge some numbers.</p><p>Maybe something closer to 10 pixels a second would be good.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sun, 04 Mar 2007 02:07:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">blargmob said:</div><div class="quote"><p>
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&#39;t seem right.
</p></div></div><p>
If you&#39;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&#39;s world space.</p><div class="quote_container"><div class="title">blargmob said:</div><div class="quote"><p>
This code is for a gamespace of 100 ft by 50 ft
</p></div></div><p>
Related to the previous point: if you&#39;re going to use real units anywhere, at least use logical ones. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>And use floating point. It&#39;s not 1995 anymore.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Sun, 04 Mar 2007 02:09:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If I used smaller speed increments, then the gravity would not be to scale.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (blargmob)</author>
		<pubDate>Sun, 04 Mar 2007 02:10:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I am using the gamespace to screenspace (Vise-Versa) formulas to find the gravity in pixels per second
</p></div></div><p>
Gravity is acceleration, so its unit will be pixel/second^2. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>EDIT: Also I strongly suggest using floats or fixed point fractional numbers. If you do calculations every 10 miliseconds you&#39;ll surely need subpixel precision. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Krzysztof Kluczek)</author>
		<pubDate>Sun, 04 Mar 2007 02:15:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>As I have once read, &quot;When it comes to games, if it looks right, it is right.&quot; Basically meaning that you shouldn&#39;t worry too much about about how true to life it is, but more that it &quot;feels&quot; 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&#39;ve just got to play with it a bit.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elverion)</author>
		<pubDate>Sun, 04 Mar 2007 02:17:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><b>How does pixels per seconds^2 affect my code?</b></p><p>EDIT: I don&#39;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)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (blargmob)</author>
		<pubDate>Sun, 04 Mar 2007 02:18:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I don&#39;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)
</p></div></div><p>
Why?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sun, 04 Mar 2007 02:45:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So it has realistic effects on the game. (This is if I&#39;m making a game that is &quot;real&quot;, like basketball or something).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (blargmob)</author>
		<pubDate>Sun, 04 Mar 2007 02:47:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
realistic
</p></div></div><p>
You don&#39;t need to get exact numbers for gravity to be realistic.  Being realistic has nothing to do with that.</p><p>It sounds like what you&#39;re really saying is you want your game to be <b>real</b> which is not possible in real time.  So if you have to fake large portions of the &#39;reality&#39; anyway, whats one more thing?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sun, 04 Mar 2007 02:50:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
EDIT: I don&#39;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)
</p></div></div><p>
Then re-read the thread. Or read it, as it may be.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Sun, 04 Mar 2007 02:54:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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&#39;s wrong with it!?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (blargmob)</author>
		<pubDate>Sun, 04 Mar 2007 03:13:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span><span class="k2">(</span>bally&gt;<span class="k3">=</span><span class="c">//The screen hieght. I.E. 600</span>
      ballyspeed<span class="k3">*</span><span class="k3">=</span><span class="k3">-</span><span class="n">1</span><span class="k2">;</span>
<span class="k1">if</span><span class="k2">(</span>ballx&gt;<span class="k3">=</span><span class="c">//The screen width. I.E. 800</span>
      ballxspeed<span class="k3">*</span><span class="k3">=</span><span class="k3">-</span><span class="n">1</span><span class="k2">;</span>
</pre></div></div><p>
</p></div></div><p>

It&#39;s off-topic, but... wouldn&#39;t this
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span><span class="k2">(</span>bally&gt;<span class="k3">=</span><a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a><span class="k2">)</span>
      ballyspeed<span class="k3">*</span><span class="k3">=</span><span class="k3">-</span><span class="n">1</span><span class="k2">;</span>
<span class="k1">if</span><span class="k2">(</span>ballx&gt;<span class="k3">=</span><a href="http://www.allegro.cc/manual/SCREEN_W" target="_blank"><span class="a">SCREEN_W</span></a><span class="k2">)</span>
      ballxspeed<span class="k3">*</span><span class="k3">=</span><span class="k3">-</span><span class="n">1</span><span class="k2">;</span>
</pre></div></div><p>
be better?</p><p>In case you have already implemented that in your code and just put a comment there for some unfathomable reason, ignore this post.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CursedTyrant)</author>
		<pubDate>Sun, 04 Mar 2007 04:13:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That doesn&#39;t help my problem......
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (blargmob)</author>
		<pubDate>Sun, 04 Mar 2007 04:16:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Helping your problem was not the point of my post. Pointing out stuff that <b>might</b> be of some use in the future, was. I just thought I&#39;d point it out in case you didn&#39;t know about it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CursedTyrant)</author>
		<pubDate>Sun, 04 Mar 2007 04:22:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
What&#39;s wrong with it!?
</p></div></div><p>

You&#39;ve already been told the answer to that question.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Sun, 04 Mar 2007 04:31:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
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&#39;s wrong with it!?
</p></div></div><p>

You&#39;re trying to model real world physics with too few of the components.</p><p>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...</p><p>The only solution to your problem is to design all that. Then <i>and only then</i> 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 <i>and</i> physics.</p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Sun, 04 Mar 2007 05:00:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So should I just &quot;play around&quot; with the gravity vector until seems suitible?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (blargmob)</author>
		<pubDate>Sun, 04 Mar 2007 05:03:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Short: Yes.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CursedTyrant)</author>
		<pubDate>Sun, 04 Mar 2007 05:04:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
So should I just &quot;play around&quot; with the gravity vector until seems suitible?
</p></div></div><p>

Exactly.</p><p>And it wouldn&#39;t hurt to switch everything to use floating point variables so you can do smaller increments than 1.</p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Sun, 04 Mar 2007 05:07:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s what everybody else does...<br />To be honest, a gamer doesn&#39;t care how realistic it is, they care how realistic it looks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (James Stanley)</author>
		<pubDate>Sun, 04 Mar 2007 05:08:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>O.K. That helps. Thanks guys.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (blargmob)</author>
		<pubDate>Sun, 04 Mar 2007 05:10:38 +0000</pubDate>
	</item>
</rss>
