<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>cocos2d for iPhone &#187; Topic: Pixel detection using CCMutableTexture2D and scaling issue</title>
		<link>http://www.cocos2d-iphone.org/forum/topic/15539</link>
		<description>A fast, easy to use, free, and community supported 2D game engine</description>
		<language>en-US</language>
		<pubDate>Sat, 18 May 2013 10:52:07 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.cocos2d-iphone.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.cocos2d-iphone.org/forum/rss/topic/15539" rel="self" type="application/rss+xml" />

		<item>
			<title>abitofcode on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-103203</link>
			<pubDate>Wed, 06 Jul 2011 15:10:12 +0000</pubDate>
			<dc:creator>abitofcode</dc:creator>
			<guid isPermaLink="false">103203@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>... and an example of using the data output from photoshop (or Gimp) to create a complex irregular hit area map </p>
<p><a href="http://abitofcode.com/2011/07/irregular-touch-detection-when-cgrect-is-not-enough-part-1/" rel="nofollow">http://abitofcode.com/2011/07/irregular-touch-detection-when-cgrect-is-not-enough-part-1/</a>
</p></description>
		</item>
		<item>
			<title>abitofcode on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-102537</link>
			<pubDate>Sun, 03 Jul 2011 17:17:40 +0000</pubDate>
			<dc:creator>abitofcode</dc:creator>
			<guid isPermaLink="false">102537@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Finally got around to writing up the use of Photoshop as a means to create a lookup array <a href="http://abitofcode.com/2011/07/building-arrays-with-photoshop/" rel="nofollow">http://abitofcode.com/2011/07/building-arrays-with-photoshop/</a>
</p></description>
		</item>
		<item>
			<title>quenns on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-92251</link>
			<pubDate>Sun, 08 May 2011 11:40:50 +0000</pubDate>
			<dc:creator>quenns</dc:creator>
			<guid isPermaLink="false">92251@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>hi,i have the same problem that when the sprite rotate or scale,then Pixel detection was not right,seems use the init texture coord,the<br />
texture content size not rotate or scale,so Pixel detection was wrong,anybodt can help?<br />
and CCMutableTexture2D can use for the frame animate?<br />
thanks
</p></description>
		</item>
		<item>
			<title>jotajota on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88443</link>
			<pubDate>Fri, 15 Apr 2011 14:09:03 +0000</pubDate>
			<dc:creator>jotajota</dc:creator>
			<guid isPermaLink="false">88443@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>umm MutableTexture2D is proving quite memory intensive indeed. Will have to give it a go to your solution. Hope the weather is baaaad during the weekend so that I can put some more time to work :)
</p></description>
		</item>
		<item>
			<title>ehoffman on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88212</link>
			<pubDate>Thu, 14 Apr 2011 09:05:04 +0000</pubDate>
			<dc:creator>ehoffman</dc:creator>
			<guid isPermaLink="false">88212@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Yes ... if you have memory warnings or if your game is running slow ... use the described method.
</p></description>
		</item>
		<item>
			<title>abitofcode on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88141</link>
			<pubDate>Thu, 14 Apr 2011 00:31:43 +0000</pubDate>
			<dc:creator>abitofcode</dc:creator>
			<guid isPermaLink="false">88141@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I've played with it a bit for a displacement map I was working on <a href="http://www.cocos2d-iphone.org/forum/topic/11659" rel="nofollow">http://www.cocos2d-iphone.org/forum/topic/11659</a> with Manucorporat's Gaussian blur code <a href="http://www.cocos2d-iphone.org/forum/topic/11745" rel="nofollow">http://www.cocos2d-iphone.org/forum/topic/11745</a></p>
<p>It's the reading of pixels that has an overhead, if you hit any bottlenecks in performance just move across to the alternative method
</p></description>
		</item>
		<item>
			<title>jotajota on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88133</link>
			<pubDate>Thu, 14 Apr 2011 00:19:44 +0000</pubDate>
			<dc:creator>jotajota</dc:creator>
			<guid isPermaLink="false">88133@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Got it working! </p>
<p>After thinking on your ideas I've realized that what I had to do was to translate the touch coords into the original texture system, not to translate the system! </p>
<p>So I've carried on with my implementation using CCMutableTexture2D and did to translate the touch:</p>
<p><code><br />
<pre><code>- (BOOL)checkAlphaTouchPoint:(GCTouchPoint *)aTouchPoint
{
	//&#34;firstState&#34; is a CCSprite initialized in another place with a mutable texture:
	//CCMutableTexture2D *tex = [[CCMutableTexture2D alloc] initWithImage:[UIImage imageNamed:aSprite1File]];
	//firstState = [CCSprite spriteWithTexture:tex];

	CCMutableTexture2D *mutTex = (CCMutableTexture2D *)[firstState texture];

	//Converting the touch position to the node
	aTouchPoint.pos = [self convertToNodeSpace:aTouchPoint.pos];

	float texWidth = [mutTex contentSize].width;
	float texHeight = [mutTex contentSize].height;

	//Translating position
	float posX = (aTouchPoint.pos.x + texWidth / 2);
	float posY = (aTouchPoint.pos.y + texHeight / 2);

        //Now the CCMutableTexture2D does the magic
	ccColor4B pixel = [mutTex pixelAt:ccp(posX, posY)];

	int aaa = pixel.a &#38; 0xFF;			

	if (aaa &#62; 0)
	{
		//SOME SECRET LOGIC HERE :)
		return YES;
	}

	return NO;
}</code></pre>
<p></code></p>
<p>Your suggestion sounds completely doable as well but I wonder if you guys have looked into this mutable texture class before (I reckon it has only been added a few months ago). </p>
<p>I do appreciate your help!
</p></description>
		</item>
		<item>
			<title>abitofcode on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88120</link>
			<pubDate>Wed, 13 Apr 2011 22:53:26 +0000</pubDate>
			<dc:creator>abitofcode</dc:creator>
			<guid isPermaLink="false">88120@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Just done a quick test;</p>
<p>* create a 2x2 image in Photoshop.<br />
* Set mode to Grayscale<br />
* Fill top left square with black<br />
* Top right with a gray<br />
* Bottom left with white<br />
* Bottom right with gray<br />
* Save As &#62; Photoshop raw (probably an idea to save the Photoshop file at the same time in case you need it later)<br />
* e.g myfile.raw<br />
* I used settings - File Type: WARP File Creator:MIB8 Header:0<br />
* Open a terminal window<br />
* change directory to the file you have created i.e<br />
- type cd<br />
- drag the folder of containing the file onto the terminal window to pass in the path<br />
- (on a side note typing 'open .' while in the terminal window opens up a finder window to that location)<br />
* hexdump myfile.raw<br />
* You should now see the hex values 00 67 ff 00 (black, grey, white, black)</p>
<p>most of these steps can be automated using Photoshop actions, including a resize as per @<a href='http://www.cocos2d-iphone.org/forum/profile/32901'>ehoffman</a> 's suggestion </p>
<p>:D
</p></description>
		</item>
		<item>
			<title>jotajota on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88115</link>
			<pubDate>Wed, 13 Apr 2011 22:32:52 +0000</pubDate>
			<dc:creator>jotajota</dc:creator>
			<guid isPermaLink="false">88115@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thanks @<a href='http://www.cocos2d-iphone.org/forum/profile/1222'>abitofcode</a> and @<a href='http://www.cocos2d-iphone.org/forum/profile/32901'>ehoffman</a>. I will try what you've suggested. I have this as a background task at the moment but will come back with my final resolution.
</p></description>
		</item>
		<item>
			<title>abitofcode on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88067</link>
			<pubDate>Wed, 13 Apr 2011 20:02:11 +0000</pubDate>
			<dc:creator>abitofcode</dc:creator>
			<guid isPermaLink="false">88067@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/16'>jotajota</a> There's more to do before the game is ever run but most of that can be automated, at runtime all you are doing is providing an offset into an array. The 256 potential values for each lookup can be put to any number of uses.
</p></description>
		</item>
		<item>
			<title>ehoffman on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88061</link>
			<pubDate>Wed, 13 Apr 2011 19:39:48 +0000</pubDate>
			<dc:creator>ehoffman</dc:creator>
			<guid isPermaLink="false">88061@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/1222'>abitofcode</a> You guess right. I meant grayscale. :) And second yes, PhotoShop.</p>
<p>@<a href='http://www.cocos2d-iphone.org/forum/profile/16'>jotajota</a> Grayscale to get values in between ... visible ... half visible ... not visible. In the example of abitofcode the values represent the height of the terrain (0=valley, 255=mountain). I needed values in between to give the player a better chance at the borders of the trees (so the player can sometimes hit "through" the leafes of the tree).</p>
<p>This method is as exactly as you want it (depends on how much you scale down the images). It can be very difficult to define polygons for the leafs of a tree or hairs in a picture... With this method you have a lot of preprocessing, but what you use in the final game is very effective - less memory, fast (just one lookup to an array), ... If you have enough CPU/RAM (or you simply don't need to care if your program runs fast enough), you can do it in the game/program. But as ressources on mobile devices are limited ... pre processing can help.
</p></description>
		</item>
		<item>
			<title>jotajota on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88047</link>
			<pubDate>Wed, 13 Apr 2011 18:40:18 +0000</pubDate>
			<dc:creator>jotajota</dc:creator>
			<guid isPermaLink="false">88047@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/1222'>abitofcode</a> I think ehoffman refers here to B/W representing Alpha =0 and Alpha != 0.</p>
<p>Thanks for the tip on PS. If I understand correctly your solution (combined with ehoffman's second solution) would be to:</p>
<p>1. Convert each graphic into a B/W one (PS batch script)<br />
2. Convert these files into raw binary files<br />
3. Create C arrays with this data for each graphic<br />
4. Do the conversions of the Touch position according to scale/rotation<br />
5. Check touch position against C Array</p>
<p>Doesn't it look too cumbersome and pre-processing intensive?</p>
<p>I'm trying to automatize the pixel detection to stop using the polygon solution I already have in place, and this solution while better than my current one, looks like will add the same or possibly more burden to the process.</p>
<p>I'm looking into the coordinate translation against the texture/sprite to see if I can come with something more real-time... </p>
<p>any more pointers on this?</p>
<p>Thanks
</p></description>
		</item>
		<item>
			<title>abitofcode on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88041</link>
			<pubDate>Wed, 13 Apr 2011 18:07:05 +0000</pubDate>
			<dc:creator>abitofcode</dc:creator>
			<guid isPermaLink="false">88041@http://www.cocos2d-iphone.org/forum/</guid>
			<description><blockquote><p>convert them into C arrays with just the b/w values
</p></blockquote>
<p>I'm guessing you meant Grayscale here not b/w. I've used this approach in the past to generate data from maps to allow me to scale characters i.e make them smaller as they go into valleys and bigger as they reach the peaks. If i remember correctly you can save the data in raw format from Photoshop to get your array (1998 was the last time I did this :S )</p>
<p>edit: You may be able to record a macro in photoshop and batch convert all your images to raw format in one go.
</p></description>
		</item>
		<item>
			<title>jotajota on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88028</link>
			<pubDate>Wed, 13 Apr 2011 16:47:42 +0000</pubDate>
			<dc:creator>jotajota</dc:creator>
			<guid isPermaLink="false">88028@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I can't opt for your suggestion (2) as the number of different graphics is quite big and the work I'm doing should be agnostic to the graphics. This is indeed the reason for these investigations as I have already a working solution (using polygons for each graphic) but it's too time consuming and scales badly.</p>
<p>I will try to look into RenderBuffers. As I mentioned before, I don't master textures or "graphic processing" so it will take me some time...</p>
<p>But thanks! @<a href='http://www.cocos2d-iphone.org/forum/profile/32901'>ehoffman</a>
</p></description>
		</item>
		<item>
			<title>ehoffman on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88018</link>
			<pubDate>Wed, 13 Apr 2011 16:22:05 +0000</pubDate>
			<dc:creator>ehoffman</dc:creator>
			<guid isPermaLink="false">88018@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Someone may correct me, if I'm wrong here ...</p>
<p>The scaling and rotation of the texture is done by the GL engine/GPU. Have a look at the CCSprite draw method. A CCSprite is always a quad, which is drawn by splitting it into two triangles using VBO on newer devices.</p>
<p>Then the vertices of this two triangles (6 vertices) are feed into the GPU:</p>
<p>1--4<br />
&#124;\ &#124;<br />
&#124; \&#124;<br />
2--3</p>
<p>1 and 3 are used in both triangles. For every vertice there are texture coordinates (u,v) and color information. The texture coordinates are always in 0,1 range (ok, they are flipped and not always 0,1 when using sprite sheets... but for now...).</p>
<p>No scaling or rotation. I think you could do that manually, but I think you will waste a lot of memory.</p>
<p>How could you solve the problem?</p>
<p>(1) If you have a lot of changing sprites (I mean a lot of different graphics) to check you could just draw to a CCRenderTexture (RenderBuffer) and check there.</p>
<p>(2) If you know all your graphics at development time you could use smaller graphics to check. I did that for my HubliBubli... There I have three layers with an alpha channel. So I made a grey scale image from every layer representing only the alpha channel - black means 0% alpha, white is 100% alpha.</p>
<p>Then I scaled the images down - 25% of the original size is enough to get an acceptable touch accuracy on iPad and iPhone Retina. </p>
<p>Now you can use the images directly or -what I did- convert them into C arrays with just the b/w values (unsigned char) - I used a small PHP script for that. Include the arrays in some header file.</p>
<p>Now, how to check if the sprite is scaled/rotated: You know the screen position of the touch TOUCH(X,Y) and you know the rotation and the scale of your sprite. At first convert the TOUCH(X,Y) to your node space (the position inside the sprite). Now apply scaling and rotation to this and check it against your C array (take into account if you resized the images).
</p></description>
		</item>
		<item>
			<title>jotajota on "Pixel detection using CCMutableTexture2D and scaling issue"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/15539#post-88012</link>
			<pubDate>Wed, 13 Apr 2011 15:58:35 +0000</pubDate>
			<dc:creator>jotajota</dc:creator>
			<guid isPermaLink="false">88012@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I'm trying to implement a pixel detection mechanism. Basically I need to check whether a touch happens on a transparent pixel to trigger some logic. For that, I've been experimenting with CCMutableTexture2D which is a great implementation I think and adds value to the engine.</p>
<p>It works fine when I create the texture with a file and then initialise Sprites with these textures. I also have it working so that the detection works while a Sprite is moving. </p>
<p>However, it is not fully working for me when I scale the Sprite after initialising it. When I inspect pixels form the MutableTexture, these seem to reflect what it would be if I didn't scale the Sprite. </p>
<p>The problem really is that I've never worked with textures myself and I don't really understand what is that I'm not taking in count. I get that the textures themselves will never resize but I don't know how to "translate" the scaling done on a Sprite to its texture.</p>
<p>Any help much appreciated.<br />
Thanks!
</p></description>
		</item>

	</channel>
</rss>
