<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Bouncing ball</title>
		<link>http://www.allegro.cc/forums/view/547610</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 26 Nov 2005 01:31:02 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>hi, everyone, I am trying to simulate a bouncing ball in Allegro but don&#39;t quite know the physics, please help me please. Now I just want it to have velocity and gravity, no friction. What I see some people do is that they do something like this:</p><div class="source-code snippet"><div class="inner"><pre>vel_y <span class="k3">+</span><span class="k3">=</span> gravity
...
ball.Y <span class="k3">+</span><span class="k3">=</span> vel_y
...
<span class="k1">if</span><span class="k2">(</span>ball.Y <span class="k3">&gt;</span> <a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a> <span class="k3">-</span> ball.height<span class="k2">)</span>
<span class="k2">{</span>
  vel_y <span class="k3">=</span> <span class="k3">-</span>vel_y
<span class="k2">}</span>
</pre></div></div><p>

That doesn&#39;t work very well because the ball bounce higher and higher, what I want is to have the ball keep bounce up to the same level, and when I add friction I want it to bounce lower and lower.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miica neo)</author>
		<pubDate>Fri, 25 Nov 2005 09:28:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, instead of doing it the way you did, you should have gravity change. When the ball should bounce, make gravity a negative value. Each &quot;step&quot; (logic loop):<br />if (gravity &lt; max_gravity) gravity += 0.01;<br />myobj.Y += gravity;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elverion)</author>
		<pubDate>Fri, 25 Nov 2005 10:11:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
when I add friction I want it to bounce lower and lower.
</p></div></div><p>It isn&#39;t friction which makes the ball bounce lower.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Well, instead of doing it the way you did, you should have gravity change.
</p></div></div><p>Not if he wants a realistic physical simulation.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
if(ball.Y &gt; SCREEN_H - ball.height)<br />{<br />  vel_y = -vel_y<br />}
</p></div></div><p>This is the part that should change when you want the ball to bounce lower; for example by changinge &quot;vel_y = -vel_y&quot; to &quot;vel_y = -vel_y * elasticity&quot; where elasticity is between 0 and 1, a value of 0 corresponding to &#39;no bounce&#39; and a value of 1 corresponding to &#39;full bounce&#39;.</p><p>As to the ball bouncing higher and higher, it&#39;s probably because you&#39;re using a numerical method which gives inexact results, Euler integration.  (edit: Although from the code you posted I think it would go lower and lower if anything ...)<br />are you sure you don&#39;t have these lines:
</p><div class="source-code snippet"><div class="inner"><pre>vel_y <span class="k3">+</span><span class="k3">=</span> gravity
...
ball.Y <span class="k3">+</span><span class="k3">=</span> vel_y
</pre></div></div><p>reversed?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Zaphos)</author>
		<pubDate>Fri, 25 Nov 2005 11:04:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Zaphos is right. Listen to Zaphos and you will get a realistic bouncing ball.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Karadoc ~~)</author>
		<pubDate>Fri, 25 Nov 2005 13:17:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t quite understand your method Elverion, is there no velocity if I use your method? and I just change the gravity value to achieve the result?</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
are you sure you don&#39;t have these lines:</p><p>vel_y += gravity<br />...<br />ball.Y += vel_y<br />reversed?
</p></div></div><p>

yes, I checked the code again and I am sure of it. I don&#39;t know why the ball bouncing higher and higher, that seems a very odd physics.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
It isn&#39;t friction which makes the ball bounce lower.
</p></div></div><p>
Is it the gravity then?</p><p>I tried vel_y = -vel_y * elasticity, I set elasticity to 0.9 and the ball starts to bounce higher after some times, when I set elasticity to 0.8 the ball bounce lower for a while then bounce no lower, it bounce at the same level. I don&#39;t get it, I still need help <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miica neo)</author>
		<pubDate>Fri, 25 Nov 2005 18:38:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Do you think it is because you are using integer value variables? If so, change them to floating point variables. When using the variable as an int later, just use &quot;(int)variable&quot;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Skalle)</author>
		<pubDate>Fri, 25 Nov 2005 19:16:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No, I am using float.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miica neo)</author>
		<pubDate>Fri, 25 Nov 2005 22:08:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Is it the gravity then?
</p></div></div><p>No, it&#39;s a property of the ball and the ground; some materials conserve kinetic energy better than others.  A collision which conserves kinetic energy is an elastic collision. Collisions in the &#39;real world&#39; are not fully elastic, as there is always some energy dissipation on impact.<br />To some extent it is also an effect of air drag, but it is not caused by ground friction or gravity.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I don&#39;t get it, I still need help <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" />
</p></div></div><p>I feel it might help to see more of the code; without seeing it all I can say is maybe you should start worrying about the impact point more precisely, perhaps with something like:
</p><div class="source-code snippet"><div class="inner"><pre>vel_y <span class="k3">+</span><span class="k3">=</span> gravity
...
oldY <span class="k3">=</span> ball.Y
ball.Y <span class="k3">+</span><span class="k3">=</span> vel_y
...
<span class="k1">if</span><span class="k2">(</span>ball.Y <span class="k3">&gt;</span> <a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a> <span class="k3">-</span> ball.height<span class="k2">)</span>
<span class="k2">{</span>
  fraction <span class="k3">=</span> <span class="k2">(</span>ball.Y <span class="k3">-</span> <a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a> <span class="k3">-</span> ball.height<span class="k2">)</span> <span class="k3">/</span> <span class="k2">(</span>ball.Y <span class="k3">-</span> oldY<span class="k2">)</span><span class="k2">;</span>
  vel_y <span class="k3">-</span><span class="k3">=</span> gravity <span class="k3">*</span> fraction
  vel_y <span class="k3">=</span> <span class="k3">-</span>vel_y <span class="k3">*</span> elasticity
  <span class="k1">if</span> <span class="k2">(</span>vel_y <span class="k3">*</span> gravity <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span>
     vel_y <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
  ball.Y <span class="k3">=</span> <a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a> <span class="k3">-</span> ball.height <span class="k3">+</span> vel_y <span class="k3">*</span> fraction<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

edit:
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
is there no velocity if I use your method? and I just change the gravity value to achieve the result?
</p></div></div><p>I would say it is more that he is conflating gravity and velocity, so his gravity variable corresponds to your velocity variable, his hardcoded .01 corresponds to your gravity variable, and then he limits velocity ... which I suppose can be seen as a very simple approximation of air drag, although of course it will have no effect unless you&#39;re bouncing high enough to reach the maximum velocity.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Zaphos)</author>
		<pubDate>Sat, 26 Nov 2005 01:18:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If I were doing a bouncing ball, I&#39;d have gravity as a negative constant.  The velocity moving across the floor in x &amp; y would remain constant without friction (air, energy absorption by ball deformation etc).  Gravity always increases the balls vertical fall (where &quot;going up&quot; is simply a sign change).  If the ball was dropped at some given height above the floor, it&#39;s vertical motion would be zero at the exact instant of release, then the vertical motion would increase as the gravity was added in each logic loop.  When the ball hits the floor, simply change the sign of the vertical velocity, then continue to add the gravity constant.  The ball would then slow down relative to the floor, and eventually approach the floor again.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Sat, 26 Nov 2005 01:23:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Arthur: That&#39;s what miica neo was already doing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Zaphos)</author>
		<pubDate>Sat, 26 Nov 2005 01:31:02 +0000</pubDate>
	</item>
</rss>
