<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>coders &#187; math</title>
	<atom:link href="http://www.gehacktes.net/category/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gehacktes.net</link>
	<description>The Limits of My Language Mean the Limits of My World - Ludwig Wittgenstein</description>
	<lastBuildDate>Sat, 31 Jul 2010 12:33:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Programming iPhone Games with Cocos2D Part 4</title>
		<link>http://www.gehacktes.net/2010/03/programming-iphone-games-with-cocos2d-part-4/</link>
		<comments>http://www.gehacktes.net/2010/03/programming-iphone-games-with-cocos2d-part-4/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 15:15:18 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=693</guid>
		<description><![CDATA[Cocos2D gives you a huge amount of features and all that stuff you need and want in your game. But one thing is really missing &#8211; a simple collision detection between sprites. In this article I want to give you some hints how you could implement your own CCSprite collision detection routine without using box2d [...]]]></description>
			<content:encoded><![CDATA[<p>Cocos2D gives you a huge amount of features and all that stuff you need and want in your game. But one thing is really missing &#8211; a simple collision detection between sprites. In this article I want to give you some hints how you could implement your own CCSprite collision detection routine without using box2d or other physic libraries.<br />
<span id="more-693"></span></p>
<h1>Update Calls</h1>
<p>Ok, lets say you want to detect a collision between two objects A and B by checking one with the other. One way would be to enhance your CCSprite with an update routine which will detect its collisions by checking collision with all other children. The pseudo-code would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// assuming we are implementing an update routine </span>
<span style="color: #11740a; font-style: italic;">// for an extended CCSprite class, lets say: CCSpriteExt.m</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> onUpdate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ccTime<span style="color: #002200;">&#41;</span> delta
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>CCNode <span style="color: #002200;">*</span> you <span style="color: #a61390;">in</span> children<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> you <span style="color: #002200;">==</span> self<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">continue</span>;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>self isCollidingWithObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> 
		<span style="color: #002200;">&#123;</span>
			<span style="color: #11740a; font-style: italic;">// collision detected !</span>
			<span style="color: #002200;">&#91;</span>collisionDelegate performSelector<span style="color: #002200;">:</span>collisionSelector withObject<span style="color: #002200;">:</span>self withObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Lets say you have 5 Objects (A,B,C,D and E) and you use the above code to check the collisions, you will count 20 check calls where 10 calls are double :</p>
<pre>
AB, AC, AD, AE -> 4 checks, 0 double checks
BA, BC, BD, BE -> 4 checks, 1 double checks: AB
CA, CB, CD, CE -> 4 checks, 2 double checks: AC, BC
DA, DB, DC, DE -> 4 checks, 3 double checks: AD, BD, CD
EA, EB, EC, ED -> 4 checks, 4 double checks: AE, BE, CE, DE
20 total checks, 10 double checks
</pre>
<p>If you have many objects this really would take time. </p>
<p>Another solution would be one update call for all you objects. Like if you have a game scene or something which allready has an update call, you could place your collision detection there and write a more efficiant object loop:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// assuming we are implementing an udate routine in a CCScene or CCLayer class</span>
..
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> idxMe<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; idxMe &lt; <span style="color: #002200;">&#91;</span>children_ count<span style="color: #002200;">&#93;</span>; idxMe<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	CCNode <span style="color: #002200;">*</span> me <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCNode <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>idxMe<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> idxYou<span style="color: #002200;">=</span>idxMe<span style="color: #002200;">+</span><span style="color: #2400d9;">1</span>; idxYou &lt; <span style="color: #002200;">&#91;</span>children_ count<span style="color: #002200;">&#93;</span>; idxYou<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>		
		CCNode <span style="color: #002200;">*</span> you <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCNode <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>idxYou<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>self isCollidingObject<span style="color: #002200;">:</span>Me WithObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> 
		<span style="color: #002200;">&#123;</span>
			<span style="color: #11740a; font-style: italic;">// collision detected !</span>
			<span style="color: #002200;">&#91;</span>collisionDelegate performSelector<span style="color: #002200;">:</span>collisionSelector withObject<span style="color: #002200;">:</span>me withObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So now you would&#8217;nt have any double check calls:
</pre>
<pre>
AB, AC, AD, AE -> 4 checks, 0 double checks
BC, BD, BE -> 3 checks, 0 double checks
CD, CE -> 2 checks, 0 double checks
DE -> 1 checks, 0 double checks
10 total checks, 0 double checks
</pre>
<p>if you have objects in your scene which you dont want to test in your collision detection or even better if you want to have collision groups you have to advance your CCSprite with a collisionGroupID. To be sure the collision detection will be as fast as possible you should check the collisionGroupID <b>before</b> you check the collision it self :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// assuming we are implementing an udate routine in a CCScene or CCLayer class</span>
..
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> idxMe<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; idxMe &lt; <span style="color: #002200;">&#91;</span>children_ count<span style="color: #002200;">&#93;</span>; idxMe<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	CCNode <span style="color: #002200;">*</span> meNode <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>idxMe<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> <span style="color: #002200;">&#91;</span>meNode isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>CCSpriteExt class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">continue</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	CCSpriteExt <span style="color: #002200;">*</span> me <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> meNode;
&nbsp;
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> idxYou<span style="color: #002200;">=</span>idxMe<span style="color: #002200;">+</span><span style="color: #2400d9;">1</span>; idxYou &lt; <span style="color: #002200;">&#91;</span>children_ count<span style="color: #002200;">&#93;</span>; idxYou<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>		
		CCNode <span style="color: #002200;">*</span> youNode <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>children_ objectAtIndex<span style="color: #002200;">:</span>idxYou<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> <span style="color: #002200;">&#91;</span>youNode isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>CCSpriteExt class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">continue</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		CCSpriteExt <span style="color: #002200;">*</span> you <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> youNode;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>me.collisionID <span style="color: #002200;">==</span> you.collisionID<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;&amp;</span> 
		    <span style="color: #002200;">&#91;</span>self isCollidingObject<span style="color: #002200;">:</span>Me WithObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #11740a; font-style: italic;">// collision detected !</span>
			<span style="color: #002200;">&#91;</span>collisionDelegate performSelector<span style="color: #002200;">:</span>collisionSelector withObject<span style="color: #002200;">:</span>me withObject<span style="color: #002200;">:</span>you<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now take a deep breath before we take a closer look into the method <b>isCollidingObject:WithNode:</b>. To keep the performance and avoid lags you should know how many collision calls you could have and how time intensive they are - even if you optimize your collision detection method pretty much. </p>
<h1>Collision Detection Method</h1>
<p>You should start by testing a bounding box even if you want a pixel by pixel collision detection to save cpu time. May be its a good idea to setup a collision type for each CCSpriteExt and when detecting the collision you could choose the best algorithm for it. Lets say there are 4 collision body types: BOUNDING_BOX, BOUNDING_SPHERE, POLYGON and PIXEL. All of them starts with bounding box since this is a really easy thing for the cpu. If you are not rotating your sprites you could use CCSprite.contentSize and CCSprite.anchorPoint to calculate your bounding box. You could also use CCSprite.boundingBox which returns the current transformed bounding box.</p>
<p>To keep all colliding stuff in one method you should shift the code "(me.collisionID == you.collisionID)" into this function, too and remove it from the collision test loop. To make this example not to difficult I will just test collisions with the same collision body type and I will not implement a pixel by pixel test routine here.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isCollidingObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCSpriteExt<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>obj1 WithObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCSpriteExt<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj2
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> obj1.collisionID <span style="color: #002200;">==</span> obj1.collisionID <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> obj1.collisionBodyType <span style="color: #002200;">!=</span> obj2.collisionBodyType<span style="color: #002200;">&#41;</span> 
		<span style="color: #002200;">&#123;</span>
			<span style="color: #11740a; font-style: italic;">// not supported yet.</span>
			<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>obj1.collisionBodyType <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">case</span> BOUNDING_BOX <span style="color: #002200;">:</span> 
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isCollidingBox<span style="color: #002200;">:</span>obj1 WithBox<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span>; 
&nbsp;
			<span style="color: #a61390;">case</span> BOUNDING_SPHERE <span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isCollidingBox<span style="color: #002200;">:</span>obj1 WithBox<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #11740a; font-style: italic;">// first boxed test!</span>
			<span style="color: #002200;">&#91;</span>self isCollidingSphere<span style="color: #002200;">:</span>obj1 WithSphere<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #a61390;">case</span> POLYGON <span style="color: #002200;">:</span> 
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isCollidingBox<span style="color: #002200;">:</span>obj1 WithBox<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #11740a; font-style: italic;">// first boxed test!</span>
			<span style="color: #002200;">&#91;</span>self isCollidingPolygon<span style="color: #002200;">:</span>obj1 WithPolygon<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #a61390;">case</span> PIXEL <span style="color: #002200;">:</span> 
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isCollidingBox<span style="color: #002200;">:</span>obj1 WithBox<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #11740a; font-style: italic;">// first boxed test!</span>
			<span style="color: #002200;">&#91;</span>self isCollidingPixel<span style="color: #002200;">:</span>obj1 WithPixel<span style="color: #002200;">:</span>obj2<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
			<span style="color: #11740a; font-style: italic;">// not supported type</span>
			<span style="color: #a61390;">break</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Since all collision tests need a boundingbox test first lets take a look into its method. Basically you want to check if a bounding box intersects with another bounding box. If you dont have transformed bounding boxes you could check the intersection even more efficiant. Two sprites are colliding if the vertical distances between anchor points and edges of both sprites are smaller than the vertical distance between them and the horizontal distances between anchor points and edges of both sprites are smaller than the horizontal distance between them.<br />
<a href="http://www.gehacktes.net/wp-content/uploads/2010/03/picture.png"><img src="http://www.gehacktes.net/wp-content/uploads/2010/03/picture.png" alt="" title="picture" width="380" height="320" class="alignnone size-full wp-image-697" /></a></p>
<p>Non-transformed bounding boxes goes here :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isCollidingBox <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj1 WithBox<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj2 
<span style="color: #002200;">&#123;</span>	
	<span style="color: #a61390;">float</span> dx <span style="color: #002200;">=</span> <span style="color: #a61390;">fabs</span><span style="color: #002200;">&#40;</span>obj2.position.x <span style="color: #002200;">-</span> obj1.position.x<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">float</span> dy <span style="color: #002200;">=</span> <span style="color: #a61390;">fabs</span><span style="color: #002200;">&#40;</span>obj2.position.y <span style="color: #002200;">-</span> obj1.position.y<span style="color: #002200;">&#41;</span>;	
&nbsp;
	<span style="color: #a61390;">float</span> dx1;
	<span style="color: #a61390;">float</span> dx2;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>obj2.position.x &gt; obj1.position.x<span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		dx1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">-</span>obj1.collisionSprite.anchorPoint.x<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> obj1.collisionSprite.contentSize.width <span style="color: #002200;">*</span> obj1.collisionSprite.scaleX;
		dx2 <span style="color: #002200;">=</span> obj2.collisionSprite.anchorPoint.x <span style="color: #002200;">*</span> obj2.collisionSprite.contentSize.width <span style="color: #002200;">*</span> obj2.collisionSprite.scaleX;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> 
	<span style="color: #002200;">&#123;</span>
		dx1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>obj1.collisionSprite.anchorPoint.x<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span>  obj1.collisionSprite.contentSize.width <span style="color: #002200;">*</span> obj1.collisionSprite.scaleX;
		dx2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">-</span>obj2.collisionSprite.anchorPoint.x<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span>obj2.collisionSprite.contentSize.width <span style="color: #002200;">*</span> obj2.collisionSprite.scaleX;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">float</span> dy1;
	<span style="color: #a61390;">float</span> dy2;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>obj2.position.y &lt; obj1.position.y<span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		dy1 <span style="color: #002200;">=</span> obj1.collisionSprite.anchorPoint.y <span style="color: #002200;">*</span> obj1.collisionSprite.contentSize.height <span style="color: #002200;">*</span> obj1.collisionSprite.scaleY;
		dy2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">-</span> obj2.collisionSprite.anchorPoint.y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> obj2.collisionSprite.contentSize.height <span style="color: #002200;">*</span> you.collisionSprite.scaleY;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> 
	<span style="color: #002200;">&#123;</span>
		dy1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">-</span> obj1.collisionSprite.anchorPoint.y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> obj1.collisionSprite.contentSize.height <span style="color: #002200;">*</span> obj1.collisionSprite.scaleY;
		dy2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>obj2.collisionSprite.anchorPoint.y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> obj2.collisionSprite.contentSize.height <span style="color: #002200;">*</span> obj2.collisionSprite.scaleY;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>dx &gt; <span style="color: #002200;">&#40;</span>dx1<span style="color: #002200;">+</span>dx2<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> || <span style="color: #002200;">&#40;</span>dy &gt; <span style="color: #002200;">&#40;</span>dy1<span style="color: #002200;">+</span>dy2<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>But if you have transformed bounding boxes use cocos boundingbox function and CGRechtIntersection to test the collision - and even more it is human readable ;-) :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isCollidingBox <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj1 WithBox<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>CCSpriteExt <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj2 
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">!</span>CGRectIsNull<span style="color: #002200;">&#40;</span> CGRectIntersection<span style="color: #002200;">&#40;</span>obj1.boundingBox, obj2.boundingBox<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;	
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The spherical collision detection needs a radius to test the collision. A collision is detected if the distance between two objects is smaller than the sum of radius. If you dont want to enhance CCSprite with a radius use CCSprite.contentSize.width/2 or CCSprite.boundingBox.width/2 or what ever.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isCollidingSphere<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCSpriteExt<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj1 WithSphere<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCSprite <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> obj2
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> minDistance <span style="color: #002200;">=</span> object.radius <span style="color: #002200;">+</span> self.radius;
	<span style="color: #a61390;">float</span> dx <span style="color: #002200;">=</span> obj2.position.x <span style="color: #002200;">-</span> obj1.position.x;
	<span style="color: #a61390;">float</span> dy <span style="color: #002200;">=</span> obj2.position.y <span style="color: #002200;">-</span> obj1.position.y;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span> <span style="color: #002200;">&#40;</span>dx &gt; minDistance || dy &gt; minDistance<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">float</span> actualDistance <span style="color: #002200;">=</span> <span style="color: #a61390;">sqrt</span><span style="color: #002200;">&#40;</span> dx <span style="color: #002200;">*</span> dx <span style="color: #002200;">+</span> dy <span style="color: #002200;">*</span> dy <span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>actualDistance &lt; <span style="color: #002200;">=</span> minDistance<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>If you dont want mix up CCSprite with your collision detection code you could also try to create a new class "CCCollisionBody" and put all the collision attributes like "radius" and "collisionBodyType" there and also a reference to your sprite so you can access the position and dimensions of your sprite :</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  CCCollisionBody.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * collision body types
 * @version 1.0
 */</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_NONE		0</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_POINT		1</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_LINE		2</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_SPHERE	3</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_RECT		4</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_POLY		5</span>
<span style="color: #6e371a;">#define COLLISIONBODYTYPE_PIXEL		6</span>
&nbsp;
<span style="color: #a61390;">@class</span> CCNode; <span style="color: #11740a; font-style: italic;">// pre decleration of CCNode, import will be in .m file</span>
<span style="color: #11740a; font-style: italic;">/** 
 * A class to define the collision body for a proxy game object.
 * collisionbodys are classified by types. Not all types are yet defined to collide with each other.
 * please use sphere and rect collisionbodys only to ensure proper functionallity
 */</span>
<span style="color: #a61390;">@interface</span> CCCollisionBody <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> 
<span style="color: #002200;">&#123;</span>
	CCNode <span style="color: #002200;">*</span> proxy; <span style="color: #11740a; font-style: italic;">// proxy object </span>
	uint type; <span style="color: #11740a; font-style: italic;">// defines the collision body type (see above)</span>
	CGSize size; <span style="color: #11740a; font-style: italic;">// if used by the type : defines the dimensions of the collision body.</span>
	<span style="color: #a61390;">float</span> radius; <span style="color: #11740a; font-style: italic;">// if used by the type : defines the radial dimensions of the collision body.</span>
	<span style="color: #a61390;">BOOL</span> disabled; <span style="color: #11740a; font-style: italic;">// if true the collions are deactivated otherwise collisions with this body will be checked.</span>
	<span style="color: #a61390;">int</span> collisionGroupID; <span style="color: #11740a; font-style: italic;">// collision group ID - only objects of the same group IDs will collide</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">int</span> collisionGroupID;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> bool disabled;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> CCNode <span style="color: #002200;">*</span> proxy;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> uint type;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> CGSize size;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">float</span> radius;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * creates a spherical collision body specified by a given radius and a proxy game object.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initAsSphereWithProxy<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> proxyObject Radius<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> sphereRadius;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * creates a rectangle collision body specified by a given size and a proxy game object.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initAsRectWithProxy<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> proxyObject Size<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span> rectSize;
&nbsp;
<span style="color: #11740a; font-style: italic;">/**
 * returns if a collision body collides with another collision body.
 * v1.0 only supports sphereical and rectangle collisionbodys.
 */</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> collidesWith<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CollisionBody<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> object;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  CollisionBody.m</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;CCCollisionBody.h&quot;</span>
<span style="color: #6e371a;">#import &quot;cocos2d.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark CCCollisionBody Implementation</span>
<span style="color: #a61390;">@implementation</span> CCCollisionBody
<span style="color: #a61390;">@synthesize</span> radius, size, type, proxy, disabled, collisionGroupID;
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark Initialisierung</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark COLLISIONBODYTYPE_NONE : no collision</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithProxy<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> proxyObject
<span style="color: #002200;">&#123;</span>
	self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		self.type <span style="color: #002200;">=</span> COLLISIONBODYTYPE_NONE;
		self.proxy <span style="color: #002200;">=</span> proxyObject;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark COLLISIONBODYTYPE_RECT : bounding box</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initAsRectWithProxy<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> proxyObject Size<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span> rectSize
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self initWithProxy<span style="color: #002200;">:</span>proxyObject<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		self.size <span style="color: #002200;">=</span> rectSize;
		self.type <span style="color: #002200;">=</span> COLLISIONBODYTYPE_RECT;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark COLLISIONBODYTYPE_SPHERE : bounding sphere</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initAsSphereWithProxy<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CCNode<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> proxyObject Radius<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> sphereRadius;
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self initWithProxy<span style="color: #002200;">:</span>proxyObject<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		self.radius <span style="color: #002200;">=</span> sphereRadius;
		self.type <span style="color: #002200;">=</span> COLLISIONBODYTYPE_SPHERE;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark cleanup</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc
<span style="color: #002200;">&#123;</span>
	self.proxy <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark -</span>
<span style="color: #6e371a;">#pragma mark collision tests</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark Rect-Rect collision </span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> isRectRectIntersection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CollisionBody<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> object
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">!</span>CGRectIsNull<span style="color: #002200;">&#40;</span> CGRectIntersection<span style="color: #002200;">&#40;</span>proxy.boundingBox, object.proxy.boundingBox<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;	
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark Sphere-Sphere collision </span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> isSphereSphereIntersection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CollisionBody<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> object
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> minDistance <span style="color: #002200;">=</span> object.radius <span style="color: #002200;">+</span> self.radius;
	<span style="color: #a61390;">float</span> dx <span style="color: #002200;">=</span> object.proxy.position.x <span style="color: #002200;">-</span> self.proxy.position.x;
	<span style="color: #a61390;">float</span> dy <span style="color: #002200;">=</span> object.proxy.position.y <span style="color: #002200;">-</span> self.proxy.position.y;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span> <span style="color: #002200;">&#40;</span>dx &gt; minDistance || dy &gt; minDistance<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">float</span> actualDistance <span style="color: #002200;">=</span> <span style="color: #a61390;">sqrt</span><span style="color: #002200;">&#40;</span> dx <span style="color: #002200;">*</span> dx <span style="color: #002200;">+</span> dy <span style="color: #002200;">*</span> dy <span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>actualDistance &lt; <span style="color: #002200;">=</span> minDistance<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #6e371a;">#pragma mark  </span>
<span style="color: #6e371a;">#pragma mark main collision detection</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> collidesWith<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CollisionBody<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> object
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>self.disabled <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span>object.disabled <span style="color: #002200;">&amp;&amp;</span> self.collisionGroupID <span style="color: #002200;">==</span> object.collisionGroupID<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self.type <span style="color: #002200;">==</span> COLLISIONBODYTYPE_RECT <span style="color: #002200;">&amp;&amp;</span> object.type <span style="color: #002200;">==</span> COLLISIONBODYTYPE_RECT<span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isRectRectIntersection<span style="color: #002200;">:</span>object<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self.type <span style="color: #002200;">==</span> COLLISIONBODYTYPE_SPHERE <span style="color: #002200;">&amp;&amp;</span> object.type <span style="color: #002200;">==</span> COLLISIONBODYTYPE_SPHERE<span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self isSphereSphereIntersection<span style="color: #002200;">:</span>object<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// NOT SUPPORTED COLLISION TYPE!</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/03/programming-iphone-games-with-cocos2d-part-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Understanding OpenGL Blend Function</title>
		<link>http://www.gehacktes.net/2010/01/alphablending-with-opengl/</link>
		<comments>http://www.gehacktes.net/2010/01/alphablending-with-opengl/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 18:19:08 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[openAL]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=564</guid>
		<description><![CDATA[I really don&#8217;t know who and why some one create a so complex thing like alpha blending modes in openGL. When I first saw these GL_ONE, GL_ONE_MINUS_SRC_ALPHA stuff I really did not get it. I did not get on second time, and the same when using it for thousand times. Long story short, here is [...]]]></description>
			<content:encoded><![CDATA[<p>I really don&#8217;t know who and why some one create a so complex thing like alpha blending modes in openGL. When I first saw these <strong>GL_ONE</strong>, <strong>GL_ONE_MINUS_SRC_ALPHA</strong> stuff I really did not get it. I did not get on second time, and the same when using it for thousand times. Long story short, here is what you need in the most cases: cross blending, additive blending, multiplied blending.<br />
<span id="more-564"></span></p>
<h1>Cross Blending</h1>
<p>Alpha of source will control the blending. If alpha is 1.0 you will only see the source, if its 0.0 you will see the target. Use : <strong>glBlendFunc( GL_SRC_ONE_MINUS_ALPHA, GL_SRC_ALPHA );</strong></p>
<p><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/crossblend.png"><img class="alignnone size-thumbnail wp-image-608" title="crossblend" src="http://www.gehacktes.net/wp-content/uploads/2010/01/crossblend-150x150.png" alt="OpenGL Crossblending" width="150" height="150" /></a><br />
</strong></p>
<pre>Rn = Rz * (1.0 - Aq) + Rq * Aq;
Gn = Gz * (1.0 - Aq) + Gq * Aq;
Bn = Bz * (1.0 - Aq) + Bq * Aq;
An = Az * (1.0 - Aq) + Aq * Aq;
</pre>
<h1>Additive Color Blending</h1>
<p>You need this to lighten up things. Use: <strong>glBlendFunc( GL_ONE, GL_ONE );</strong></p>
<p><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/additive.png"><img class="alignnone size-thumbnail wp-image-610" title="additive" src="http://www.gehacktes.net/wp-content/uploads/2010/01/additive-150x150.png" alt="Additive Color Blending" width="150" height="150" /></a></strong></p>
<pre>Rn = Rz + Rq;
Gn = Gz + Gq;
Bn = Bz + Bq;
An = Az + Aq;</pre>
<h1>Additive Alpha Blending</h1>
<p>Its same like additive color blending, but it uses alpha channel of source to define the blending strength. Use <strong>glBlendFunc( GL_ONE, GL_SRC_ALPHA );</strong></p>
<p><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/additive.png"><strong></strong></a><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/additive_color.png"><img class="alignnone size-thumbnail wp-image-609" title="additive_color" src="http://www.gehacktes.net/wp-content/uploads/2010/01/additive_color-150x150.png" alt="Additive Color Blending" width="150" height="150" /></a></strong><strong><br />
</strong></p>
<pre>Rn = Rz + Rq * Aq;
Gn = Gz + Gq * Aq;
Bn = Bz + Bq * Aq;
An = Az + Aq * Aq;</pre>
<h1>Multiplied Blending</h1>
<p>You just multiply the color values. You could use this to colorize a scene. etc. Use: <strong>glBlendFunc( GL_ZERO, GL_SRC_COLOR );</strong></p>
<p><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/multiply.png"><img class="alignnone size-thumbnail wp-image-611" title="multiply" src="http://www.gehacktes.net/wp-content/uploads/2010/01/multiply-150x150.png" alt="Multiplied Blending" width="150" height="150" /></a><br />
</strong></p>
<pre>Rn = Rz * Rq;
Gn = Gz * Gq;
Bn = Bz * Bq;
An = Az * Aq;
</pre>
<p>Hope this is useful for some one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/01/alphablending-with-opengl/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Math: EaseIn EaseOut, EaseInOut and Beziér Curves</title>
		<link>http://www.gehacktes.net/2009/03/math-easein-easeout-and-easeinout/</link>
		<comments>http://www.gehacktes.net/2009/03/math-easein-easeout-and-easeinout/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 19:20:00 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=378</guid>
		<description><![CDATA[If you search for easeIn or easeOut you get many results of flash examples how to use the build-in method to accelerate and slowdown objects when you move them. Actually this was not really what you&#8217;re looking for isnt&#8217;t it? You looked for a pseudo code of how you can write this method by your [...]]]></description>
			<content:encoded><![CDATA[<p>If you search for <em>easeIn</em> or <em>easeOut</em> you get many results of flash examples how to use the build-in method to accelerate and slowdown objects when you move them. Actually this was not really what you&#8217;re looking for isnt&#8217;t it? You looked for a pseudo code of how you can write this method by your self and may some explenation how it functions.</p>
<p><span id="more-378"></span><br />
There are several methods to make a curve smooth or let objects follow a spline. I will talk a bit about:</p>
<ol>
beziér curves<br />
velocity curves
</ol>
<ol>
<li>
<h5>Beziér Curves</h5>
<p>You need four coordinates for one curve, lets name them <em>A</em>, <em>B</em>, <em>C</em> and <em>D</em>, where <em>A</em> is the Start Point, <em>B</em> is the controll Point for <em>A</em> and <em>C</em> is controll point for <em>D</em> and <em>D</em> is the destination point.</p>
<p><img src="http://www.anima-creative.de/fileadmin/images/bezier.png" alt="" /></p>
<p><em>Pseudo Code</em></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">&nbsp;
<span style="color: #339933;">#define pow2(x) ((x)*(x))</span>
<span style="color: #339933;">#define pow3(x) ((x)*(x)*(x))</span>
&nbsp;
Vector3 bezier <span style="color: #009900;">&#40;</span>Vector3 A<span style="color: #339933;">,</span> Vector3 B<span style="color: #339933;">,</span> Vector3 C<span style="color: #339933;">,</span> Vector3 D<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	Vector3 OUT<span style="color: #339933;">;</span>
	OUT.<span style="color: #202020;">x</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>pow3<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>A.<span style="color: #202020;">x</span><span style="color: #339933;">+</span><span style="color: #0000dd;">3</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>pow2<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>t<span style="color: #339933;">*</span>B.<span style="color: #202020;">x</span><span style="color: #339933;">+</span><span style="color: #0000dd;">3</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>pow2<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>C.<span style="color: #202020;">x</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>pow3<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>D.<span style="color: #202020;">x</span><span style="color: #339933;">;</span>
	OUT.<span style="color: #202020;">y</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>pow3<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>A.<span style="color: #202020;">y</span><span style="color: #339933;">+</span><span style="color: #0000dd;">3</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>pow2<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>t<span style="color: #339933;">*</span>B.<span style="color: #202020;">y</span><span style="color: #339933;">+</span><span style="color: #0000dd;">3</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>pow2<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>C.<span style="color: #202020;">y</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>pow3<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>D.<span style="color: #202020;">y</span><span style="color: #339933;">;</span>
	OUT.<span style="color: #202020;">z</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>pow3<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>A.<span style="color: #202020;">z</span><span style="color: #339933;">+</span><span style="color: #0000dd;">3</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>pow2<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>t<span style="color: #339933;">*</span>B.<span style="color: #202020;">z</span><span style="color: #339933;">+</span><span style="color: #0000dd;">3</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>pow2<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>C.<span style="color: #202020;">z</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>pow3<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>D.<span style="color: #202020;">z</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> OUT<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li>
<h5>Velocity Curves</h5>
<p>Accelleration will control these curves. Lets say you start with a high velocity on A and slow down by a negative acceleration on D you would get a similar curve like the obove. There are three typical curves :</p>
<p><img src="http://www.anima-creative.de/fileadmin/images/ease.png" alt="" /></p>
<p><em>Pseudo Code :</em></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define pow2(x) ((x)*(x))</span>
<span style="color: #339933;">#define pow3(x) ((x)*(x)*(x))</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// assuming :</span>
<span style="color: #666666; font-style: italic;">// t is a value between 0 and 1</span>
<span style="color: #666666; font-style: italic;">// b is an offset</span>
<span style="color: #666666; font-style: italic;">// c is the height</span>
public <span style="color: #993333;">double</span> easeIn <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> t<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> b<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> c<span style="color: #339933;">*</span>pow3<span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
public <span style="color: #993333;">double</span> easeOut <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> t<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> b<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> c<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>pow3<span style="color: #009900;">&#40;</span>t<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
public <span style="color: #993333;">double</span> easeInOut <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> t<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> b<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>t<span style="color: #339933;">*</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
  <span style="color: #b1b100;">return</span> c<span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>pow3<span style="color: #009900;">&#40;</span>t<span style="color: #339933;">*</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
  <span style="color: #b1b100;">return</span> c<span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>pow3<span style="color: #009900;">&#40;</span>t<span style="color: #339933;">*</span><span style="color: #0000dd;">2</span><span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/03/math-easein-easeout-and-easeinout/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
