<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Openlayer bitmap collision</title>
		<link>http://www.allegro.cc/forums/view/599634</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 21 Mar 2009 15:38:54 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>How to make a collision between polygon and bitmap so that the polgon would either bounce back or just stop? I found some example code but i dont know either what to do after collision has happened or how to use those functions. Pretty poorly intorduced in manual.</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="c">//lets assume the polygon moves like this</span>
<span class="number">  2</span>px <span class="k3">+</span><span class="k3">=</span> speed <span class="k3">*</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>direction<span class="k2">)</span><span class="k2">;</span> 
<span class="number">  3</span>py <span class="k3">+</span><span class="k3">=</span> speed <span class="k3">*</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>direction<span class="k2">)</span><span class="k2">;</span> 
<span class="number">  4</span>
<span class="number">  5</span>
<span class="number">  6</span>Collision collision <span class="k3">=</span> myPolygon.GetCollision<span class="k2">(</span> <span class="k3">*</span>myBitmap.GetCollisionPoly<span class="k2">(</span><span class="k2">)</span>,
<span class="number">  7</span> myPolygon.GetPlacement<span class="k2">(</span><span class="k2">)</span>, Placement<span class="k2">(</span> Vec2D<span class="k2">(</span> imgx, imgy <span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span>
<span class="number">  9</span><span class="c">// Let's see if a collision has happened</span>
<span class="number"> 10</span><span class="k1">if</span><span class="k2">(</span> collision <span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 11</span>  <span class="c">// Get the exact point of the collision</span>
<span class="number"> 12</span>  Vec2D collisionPoint <span class="k3">=</span> collision.GetPoint<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span>  
<span class="number"> 14</span>  <span class="c">// Get the normal of the polygon at the collision point</span>
<span class="number"> 15</span>  Vec2D polyNormal <span class="k3">=</span> collision.GetNormal<span class="k2">(</span> OBJ_A <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>  
<span class="number"> 17</span>  <span class="c">// Get the normal of the edges of the Bitmap at the collision point</span>
<span class="number"> 18</span>  Vec2D bitmapNormal <span class="k3">=</span> collision.GetNormal<span class="k2">(</span> OBJ_B <span class="k2">)</span><span class="k2">;</span>
<span class="number"> 19</span><span class="k2">}</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (peccos)</author>
		<pubDate>Thu, 19 Mar 2009 16:41:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If all you need is detecting collision between two &quot;objects&quot; (by object I mean bitmap, polygon, whatever is drawn on the screen and you know it&#39;s size and position) you can simply use the <a href="http://wiki.allegro.cc/index.php?title=Bounding_Box">bounding box</a> method. Works preety good in most of the cases. </p><p>If you want a &quot;pixel perfect&quot; collision detection just use the getpixel() function to check when two pixels that aren&#39;t invisible (in allegro that would be R 255  G 0  B255) collide with each other.  The cons of this method is that it&#39;s slower, esspecially when checking a lot of objects. </p><p>After you figure out the collision detection method just change the position of the objects accordingly (also the speed or whatever other variables you have to modify the movement of the objects). </p><p>Start of with to rectangles, it&#39;s easier.
</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="c">// Check collision. Make rect color red if collided, blue otherwise</span>
<span class="number">  2</span><span class="k1">if</span> <span class="k2">(</span><span class="k2">(</span><span class="k2">(</span>x<span class="k3">+</span><span class="n">10</span> <span class="k3">&gt;</span> x2<span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k2">(</span>y<span class="k3">+</span><span class="n">10</span> <span class="k3">&gt;</span> y2<span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k2">(</span>x<span class="k3">+</span><span class="n">10</span> <span class="k3">&lt;</span> x2<span class="k3">+</span><span class="n">10</span><span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k2">(</span>y<span class="k3">+</span><span class="n">10</span> <span class="k3">&lt;</span> y2<span class="k3">+</span><span class="n">10</span><span class="k2">)</span><span class="k2">)</span>
<span class="number">  3</span>     <span class="k3">|</span><span class="k3">|</span> <span class="k2">(</span><span class="k2">(</span>x2<span class="k3">+</span><span class="n">10</span> <span class="k3">&gt;</span> x<span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k2">(</span>y2<span class="k3">+</span><span class="n">10</span> <span class="k3">&gt;</span> y<span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k2">(</span>x2<span class="k3">+</span><span class="n">10</span> <span class="k3">&lt;</span> x<span class="k3">+</span><span class="n">10</span><span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k2">(</span>y2<span class="k3">+</span><span class="n">10</span> <span class="k3">&lt;</span> y<span class="k3">+</span><span class="n">10</span><span class="k2">)</span><span class="k2">)</span><span class="k2">)</span>
<span class="number">  4</span>     theColor <span class="k3">=</span> <a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a> <span class="k2">(</span><span class="n">255</span>, <span class="n">0</span> , <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span><span class="k1">else</span> theColor <span class="k3">=</span> <a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a> <span class="k2">(</span><span class="n">0</span>, <span class="n">0</span>, <span class="n">255</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span>
<span class="number">  7</span><span class="c">// Your objects</span>
<span class="number">  8</span><a href="http://www.allegro.cc/manual/rect" target="_blank"><span class="a">rect</span></a><span class="k2">(</span>buffer, x, y , x<span class="k3">+</span><span class="n">10</span>, y<span class="k3">+</span><span class="n">10</span>, theColor<span class="k2">)</span><span class="k2">;</span>
<span class="number">  9</span><a href="http://www.allegro.cc/manual/rect" target="_blank"><span class="a">rect</span></a><span class="k2">(</span>buffer, x2, y2, x2<span class="k3">+</span><span class="n">10</span>, y2<span class="k3">+</span><span class="n">10</span>, theColor<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span>
<span class="number"> 11</span><span class="c">// Blit to the screen</span>
<span class="number"> 12</span><a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>buffer, <a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <a href="http://www.allegro.cc/manual/SCREEN_W" target="_blank"><span class="a">SCREEN_W</span></a>, <a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span>
<span class="number"> 14</span><span class="c">// Use this so you can move your objects around</span>
<span class="number"> 15</span><span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_RIGHT<span class="k2">]</span><span class="k2">)</span> x<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="number"> 16</span><span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_LEFT<span class="k2">]</span><span class="k2">)</span> x--<span class="k2">;</span>
<span class="number"> 17</span><span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_UP<span class="k2">]</span><span class="k2">)</span> y--<span class="k2">;</span>
<span class="number"> 18</span><span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_DOWN<span class="k2">]</span><span class="k2">)</span> y<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="number"> 19</span>
<span class="number"> 20</span><span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_D<span class="k2">]</span><span class="k2">)</span> x2<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="number"> 21</span><span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_A<span class="k2">]</span><span class="k2">)</span> x2--<span class="k2">;</span>
<span class="number"> 22</span><span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_W<span class="k2">]</span><span class="k2">)</span> y2--<span class="k2">;</span>
<span class="number"> 23</span><span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_S<span class="k2">]</span><span class="k2">)</span> y2<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
</div></div><p>

This is written on the fly, hope it works well. Also it&#39;s supposed to work in Allegro, didn&#39;t use OpenLayer before. For bouncing and stuff you have to modify the part where I changed the color to red with changing position or something. That&#39;s more complex, involving speed, gravity, friction, depends on how real you want it to look. </p><p>Good luck, hope this helps.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GClaudiu)</author>
		<pubDate>Thu, 19 Mar 2009 17:42:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I believe i should use those OL:s own functions but i don&#39;t know how. I can detect collision but no idea how to use those GetPoint / GetNormal commands.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (peccos)</author>
		<pubDate>Thu, 19 Mar 2009 19:29:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>OL doesn&#39;t know what a collision means in your case. For starters, you could move the polygons away of each other until no collision happens. But there are a lot of articles in the net about 2D collision physics, and those algorithms use the provided functions (normal, point etc). OL is mostly just graphics library, not physics...
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/599634/800837#target">Claudiu Gilcescu-Ceia</a> said:</div><div class="quote"><p>This is written on the fly, hope it works well. Also it&#39;s supposed to work in Allegro, didn&#39;t use OpenLayer before.</p></div></div><p>
Using OL&#39;s collision methods is much easier than even a bounding box... and compared to pixel-perfect collisions, they support rotated and stretched bitmaps. The example code is pasted there above.
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>If all you need is detecting collision between two &quot;objects&quot;</p></div></div><p>
Nope, he wants to know what he should do after the collision is detected...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Fladimir da Gorf)</author>
		<pubDate>Fri, 20 Mar 2009 06:02:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Fladimir<br />Hehe, I told you I don&#39;t know OpenLayer <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>@peccos<br />If you figured out how to detect collision, could you give us an example of what exactly would you like to make? <br />For example: I want to have a ball bouncing on the screen. </p><p>You really have to know what you want to do in order to know what you have to do to achieve it (sound really funny, hope you understand). You won&#39;t be using the same code when collision occurs in a breakout clone as what you&#39;re using in a role playing game <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GClaudiu)</author>
		<pubDate>Fri, 20 Mar 2009 12:09:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay, the plan is to use a one big bitmap or either create a map from smaller bitmaps so i can accomplish a destructable terrain. if you have ever played Wings etc. you know how it&#39;s gonna be. If your player collides with the map it won&#39;t go through it from any angle <img src="http://www.allegro.cc/forums/smileys/wink.gif" /> I believe this pixel perfect method would work but am i supposed to use allegros commands or can this be done only with OL?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (peccos)</author>
		<pubDate>Fri, 20 Mar 2009 14:04:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Is this about the MC2 project? <img src="http://www.allegro.cc/forums/smileys/smiley.gif" /> Collision masks are the way to go, so for every frame of the animation of a worm you would have a black and white mask, with white meaning &quot;this is a pixel that&#39;s part of the worm&quot; and black &quot;this is invisible, doesn&#39;t collide&quot;. </p><p>You&#39;ll have the same collision mask for the map, that can be generated at runtime, once with the terrain. </p><p>Now say you have a grenade launched, that should also have a collision mask. When the grenade hits the terrain, render the explosion. The explosion should have something like a big circle attached, sort of like the collision mask. </p><p>So, when the explosion is rendered you just have to tell the program to replace every pixel on the terrain with an alpha pixel if that explosion circle overlaps it. </p><p>Like that you&#39;ll have a hole on the terrain once the grenade exploded. For a nicer effect, you could double the terrain layer, meaning that you&#39;ll have two identical terrains overlapping each-other. When the grenade explodes, replace the pixels on the <b>ON TOP</b> terrain layer with alpha pixels, and do the same with the layer behind as well, but make sure it&#39;s a circle with a smaller radius. Like that you&#39;ll give a sense of depth to the hole made on the terrain. </p><p>Use trigonometry functions if you want to make the grenade to bounce from the ground up at an angle, but that&#39;s another story. For your example all you have to do is change the grenade in mid air animation (maybe just a spinning grenade) with an exploding animation when it detects collision (make sure you stop moving the grenade when it hits the ground). </p><p>Oh, and do not use a big bitmap, not a good idea really (imagine a 3000*1000 sized map...). Maybe try tiles if you really want images, but you can also generate a random sloping terrain on run-time <img src="http://www.allegro.cc/forums/smileys/wink.gif" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GClaudiu)</author>
		<pubDate>Fri, 20 Mar 2009 14:47:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay i gave up with those OL functions for a while, always ended up giving wrong positions of collision argh. I tried OL:s GetPixel to check one pixels color from the player but this slowed down the game 90%. How am i supposed to use it? Lets say color white is considered transparent and black the terrain. Do i check if some pixel in player bitmap is black and if so then collision happens?</p><p>Edit: So i believe i have to deal with memory bitmaps to make it faster. I tried to convert OL bitmap into memorybitmap but it seems not to load it correctly. Same thing i tried to load an allegro bitmap but when drawing it only shows a black box instead. OL:s getpixel didn&#39;t work with memory bitmaps so should i be able to use allegros getpixel?  Or perhaps there is another way of doing all this?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (peccos)</author>
		<pubDate>Sat, 21 Mar 2009 15:38:54 +0000</pubDate>
	</item>
</rss>
