<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Collision Detection again..</title>
		<link>http://www.allegro.cc/forums/view/610019</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 15 Apr 2012 23:10:40 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello, I&#39;m using the function I got off the wiki for bounding boxes but I&#39;m not able to handle the collision like I want to.</p><p>Right now I&#39;ve got one box that can move and jump, but the problem is I can&#39;t make it so it just drops down or stops if I hit the sides or under the box and let&#39;s me walk on top of the box. I&#39;ve tried so many times now, but everytime I fix one bug, another one pops up and ruins it.</p><p>I have tried to do setPosX(getPosX - getVelX) then set the velX to 0 for the sides, and setPosY(getPosY - getVelX) and set velY to 0 if under the box, and for the top of the box I do the same thing but disable gravity so the player isn&#39;t pushed into the box.</p><p>This is what I&#39;m using to figure out which side the player hit the box.</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span>   <span class="k1">if</span> <span class="k2">(</span><span class="k2">(</span>b1_x <span class="k3">&gt;</span> b2_x <span class="k3">+</span> b2_w <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span><span class="k2">)</span> side <span class="k3">=</span> <span class="n">1</span><span class="k2">;</span> <span class="c">// player right Right off</span>
<span class="number"> 2</span>  <span class="k1">if</span> <span class="k2">(</span><span class="k2">(</span>b1_y <span class="k3">&gt;</span> b2_y <span class="k3">+</span> b2_h <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span><span class="k2">)</span> side <span class="k3">=</span> <span class="n">2</span><span class="k2">;</span> <span class="c">// player at bottom off</span>
<span class="number"> 3</span>  <span class="k1">if</span> <span class="k2">(</span><span class="k2">(</span>b2_x <span class="k3">&gt;</span> b1_x <span class="k3">+</span> b1_w <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span><span class="k2">)</span> side <span class="k3">=</span> <span class="n">3</span><span class="k2">;</span> <span class="c">// player left off</span>
<span class="number"> 4</span>  <span class="k1">if</span> <span class="k2">(</span><span class="k2">(</span>b2_y <span class="k3">&gt;</span> b1_y <span class="k3">+</span> b1_h <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span><span class="k2">)</span> side <span class="k3">=</span> <span class="n">4</span><span class="k2">;</span> <span class="c">// player on top</span>
</div></div><p>

I&#39;d really appreciate some help with this, basically I&#39;m just trying to make a platform the player can stand on.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnamra)</author>
		<pubDate>Sun, 15 Apr 2012 00:58:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Although it isn&#39;t perfect this code will work it does have abug still :/... Maybe you can find the mistake in it I just tossed it together to test collisions.</p><p>It uses allegro 4.2</p><p>You can convert to 4.4 if you like.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (David Sopala)</author>
		<pubDate>Sun, 15 Apr 2012 07:15:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I know it works, but every time I kept finding bugs I made the code more and more complicated to fix the bugs I found, so I decided to rewrite it again for the 4th or 5th time. This time with help of this awesome community <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>Edit: Oh I thought you meant the code I posted before I edited the whole post, and I didn&#39;t notice the attached file.</p><p>Edit 2: Hmm, I gotta convert it to Allegro 5 and I haven&#39;t used allegro 4 before, but it helps me a little. Thanks <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Perhaps I&#39;ve overlooked something, but I can&#39;t find the code for
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span>
<span class="number"> 2</span><span class="k1">int</span> Get_Y_Correction<span class="k2">(</span>C_Rect <span class="k3">*</span>, C_Rect <span class="k3">*</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 3</span><span class="k1">int</span> Get_X_Correction<span class="k2">(</span>C_Rect <span class="k3">*</span>, C_Rect <span class="k3">*</span><span class="k2">)</span><span class="k2">;</span>
</div></div><p>

in the Collision_Handler.cpp file, these two interest me the most cause I&#39;ll only be using rectangles in my program.</p><p>Also, I have no idea what this is</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>  <span class="k1">if</span><span class="k2">(</span>u <span class="k3">&lt;</span> <span class="n">0</span><span class="k2">)</span>
<span class="number">  2</span>    <span class="k2">{</span>
<span class="number">  3</span>        u <span class="k3">&amp;</span><span class="k3">=</span> <span class="n">0x007F</span><span class="k2">;</span>    <span class="c">//save amount needed</span>
<span class="number">  4</span>        u <span class="k3">=</span> u <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="n">16</span><span class="k2">;</span>        <span class="c">//shift 16 bits into first 2 bytes</span>
<span class="number">  5</span>        u <span class="k3">+</span><span class="k3">=</span> <span class="n">0x8000</span><span class="k2">;</span>     <span class="c">//sign preservation</span>
<span class="number">  6</span>    <span class="k2">}</span><span class="k1">else</span>
<span class="number">  7</span>    <span class="k2">{</span>
<span class="number">  8</span>        u <span class="k3">&amp;</span><span class="k3">=</span> <span class="n">0x007F</span><span class="k2">;</span>    <span class="c">//save amount needed</span>
<span class="number">  9</span>        u <span class="k3">=</span> u <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="n">16</span><span class="k2">;</span>        <span class="c">//shift 16 bits into first 2 bytes</span>
<span class="number"> 10</span>    <span class="k2">}</span>
<span class="number"> 11</span>
<span class="number"> 12</span>    <span class="k1">if</span><span class="k2">(</span>l <span class="k3">&lt;</span> <span class="n">0</span><span class="k2">)</span>
<span class="number"> 13</span>    <span class="k2">{</span>
<span class="number"> 14</span>        l <span class="k3">&amp;</span><span class="k3">=</span> <span class="n">0x007F</span><span class="k2">;</span>    <span class="c">//save amount needed</span>
<span class="number"> 15</span>        l <span class="k3">+</span><span class="k3">=</span> <span class="n">0x80</span><span class="k2">;</span>     <span class="c">//sign preservation</span>
<span class="number"> 16</span>    <span class="k2">}</span><span class="k1">else</span>
<span class="number"> 17</span>    <span class="k2">{</span>
<span class="number"> 18</span>        l <span class="k3">&amp;</span><span class="k3">=</span> <span class="n">0x007F</span><span class="k2">;</span>    <span class="c">//save amount needed</span>
<span class="number"> 19</span>    <span class="k2">}</span>
<span class="number"> 20</span>    <span class="k1">return</span> u <span class="k3">+</span> l<span class="k2">;</span>
</div></div><p>
What is that called?<br />I haven&#39;t yet learnt what that is, but I have seen it before.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnamra)</author>
		<pubDate>Sun, 15 Apr 2012 07:18:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>and operators and bit shifting?</p><p>I did the correction factors by taking the overlaps and going from there....<br />That was an abandoned project of mine, even if it did work mostly.</p><p>Ah I guess I never wrote those functions....</p><p>The thought process was 15 bits of data = 2^15 = 32768 more than enough to correct for a collision.</p><p>Then have a sign as well to go with it.</p><p>Rect/Rect would be something like
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>
<span class="number">  2</span><span class="k1">int</span> Get_X_Correction<span class="k2">(</span>C_Rect <span class="k3">*</span>a, C_Rect <span class="k3">*</span>b<span class="k2">)</span>
<span class="number">  3</span><span class="k2">{</span>
<span class="number">  4</span>    <span class="k1">if</span><span class="k2">(</span><span class="k2">(</span>a-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">&lt;</span> b-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> b-&gt;Get_SX<span class="k2">(</span><span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> a-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">&gt;</span> b-&gt;Get_X<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span> <span class="k3">|</span><span class="k3">|</span> <span class="k2">(</span>a-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> a-&gt;Get_SX<span class="k2">(</span><span class="k2">)</span> <span class="k3">&lt;</span> b-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> b-&gt;Get_SX<span class="k2">(</span><span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> a-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> a-&gt;Get_SX<span class="k2">(</span><span class="k2">)</span> <span class="k3">&gt;</span> b-&gt;Get_X<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="c">//basicly if a starts somewhere in b OR if a ends somewhere in b</span>
<span class="number">  5</span>    <span class="k2">{</span>
<span class="number">  6</span>        <span class="c">//We have overlap in the x dimension</span>
<span class="number">  7</span>        <span class="c">//determine amount of overlap</span>
<span class="number">  8</span>        <span class="k1">if</span><span class="k2">(</span>a-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">&lt;</span> b-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> b-&gt;Get_SX<span class="k2">(</span><span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> a-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">&gt;</span> b-&gt;Get_X<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">)</span>   <span class="c">//if we are overlapping and needing to move to the RIGHT to get out of collision.</span>
<span class="number">  9</span>        <span class="k2">{</span>
<span class="number"> 10</span>            <span class="k1">return</span> <span class="k2">(</span>b-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> b-&gt;Get_SX<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span> <span class="k3">-</span> a-&gt;Get_X<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>     <span class="c">//b's right side minus a's left side gives the overlap</span>
<span class="number"> 11</span>        <span class="k2">}</span><span class="k1">else</span> <span class="c">//we must need to move to the LEFT if not the right.</span>
<span class="number"> 12</span>        <span class="k2">{</span>
<span class="number"> 13</span>            <span class="k1">return</span> b-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">-</span> <span class="k2">(</span>a-&gt;Get_X<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> a-&gt;Get_SX<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>     <span class="c">//b's left side minus a's right side gives us the overlap(even the correct sign)</span>
<span class="number"> 14</span>        <span class="k2">}</span>
<span class="number"> 15</span>        
<span class="number"> 16</span>    <span class="k2">}</span>
<span class="number"> 17</span>    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 18</span><span class="k2">}</span>
</div></div><p>

Change the Xs to Ys and you have the y correction function.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (David Sopala)</author>
		<pubDate>Sun, 15 Apr 2012 19:08:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Think in 1D for a moment.<br />Suppose you have two intervals on an axis: (min1, max1) and (min2, max2). They are overlapping and you want to separate them. The first one is stationary, but you can move the second one along the axis either in the negative or the positive direction, until they stop overlapping. Take a piece of paper, draw the diagram and work out the formulas for how much to move in either direction to separate the intervals. Now, in a game, you&#39;ll want to move an object as little as possible while still pushing it out of the tile. You have two axes, giving you 4 directions you could move along. Calculate the minimal amount of motion along the X axis required to push the object out of the tile, then do the same for the Y axis, then just choose the one with the minimal amount of motion. Try to work it out on your own.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Stas B.)</author>
		<pubDate>Sun, 15 Apr 2012 20:34:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/610019/952559#target">Stas B.</a> said:</div><div class="quote"><p>Think in 1D for a moment.<br />Suppose you have two intervals on an axis: (min1, max1) and (min2, max2). They are overlapping and you want to separate them. The first one is stationary, but you can move the second one along the axis either in the negative or the positive direction, until they stop overlapping. Take a piece of paper, draw the diagram and work out the formulas for how much to move in either direction to separate the intervals. Now, in a game, you&#39;ll want to move an object as little as possible while still pushing it out of the tile. You have two axes, giving you 4 directions you could move along. Calculate the minimal amount of motion along the X axis required to push the object out of the tile, then do the same for the Y axis, then just choose the one with the minimal amount of motion. Try to work it out on your own.</p></div></div><p>

if abs(Bx2 - Px1) &gt; abs(Bx1 - Px2) where x1 is always closest to 0, then go towards left else right, then use the same for y and check both x and y? Wouldn&#39;t that work?</p><p>It always gave me the correct result when I tried it on paper, and I tried it with different lengths, if the player somehow swallowed the box and some other stuff.</p><div class="quote_container"><div class="title">David Sopala said:</div><div class="quote"><p>int Get_X_Correction(C_Rect *a, C_Rect *b) code </p></div></div><p>

I&#39;ll definitely try this code.</p><p>Thanks for the help, both of you!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnamra)</author>
		<pubDate>Sun, 15 Apr 2012 23:10:40 +0000</pubDate>
	</item>
</rss>
