<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Collision Detection and responding to it</title>
		<link>http://www.allegro.cc/forums/view/592712</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 12 Aug 2007 19:38:35 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey guys, I&#39;m working on an engine for a 2D action RPG using C++ and the Allegro library. I just discovered this forum so I decided to put forward a problem I have...</p><p>I have the code written to find out if one sprite is colliding with another sprite. But if one sprite goes inside something it shouldn&#39;t, like the ground, I want it to stop moving.</p><p>The problem: the sprites x and y positions are updated based on its velocity x and velocity y valuables. The code to stop the sprite going through the ground looks like this a the moment:</p><p>    if (onSurface())<br />    {<br />            vY = 0;<br />    }</p><p>However if the sprite is traveling too fast (for example after jumping or falling off a ledge) it can end up being stuck inside the ground. <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /> Any ideas? I have thought about using an old x and old y value and if the sprite collides, set the x and y values back to the old ones. However I don&#39;t think this would work because of the sprite traveling in incrementations of velocity x and y.</p><p>I would be really interested to hear how other people solve this and appreciate some help <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>Thanks,<br />Nicky
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (NickyAtfat)</author>
		<pubDate>Sun, 12 Aug 2007 18:45:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The easiest would probably be to check for the <b>future</b> location. That is x+vX, not just x, and then backtrack (back away until they no longer overlap, then stop).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Sun, 12 Aug 2007 18:49:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for you response!</p><p>Hmm I&#39;ve been trying to implement that and I&#39;m having trouble with the &quot;backtracking&quot; part... I&#39;m not sure I understand what you mean... Maybe you have an example?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (NickyAtfat)</author>
		<pubDate>Sun, 12 Aug 2007 19:24:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Not really. But a non-optimal solution could be something like this (pseudo code)<br />(This is when colliding with non-moving objects, moving objects is a bit harder.)
</p><div class="source-code snippet"><div class="inner"><pre>Is movingObject.position<span class="k3">+</span>movingObject<span class="k3">+</span>velocity colliding with staticObject.position?
<span class="k2">(</span>
  Do <span class="k1">for</span> each point in a <a href="http://www.allegro.cc/manual/line" target="_blank"><span class="a">line</span></a> going from movingObject.position to movingObject.position<span class="k3">+</span>velocity
    <span class="k2">(</span>
       Is currentPosition colliding with staticobject.position?
       <span class="k2">(</span>
          <span class="c">//This is the final position of the moving object. Set speed to 0 etc</span>
       <span class="k2">)</span>
    <span class="k2">)</span>
<span class="k2">)</span>
</pre></div></div><p>
You may want to do this on a per-axis basis oslt, not sure
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Sun, 12 Aug 2007 19:34:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Aha I was forgetting to set the velocity to 0, works fine now.</p><p>Thanks very much! <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (NickyAtfat)</author>
		<pubDate>Sun, 12 Aug 2007 19:38:35 +0000</pubDate>
	</item>
</rss>
