How to trace the intersection of an image with the boundaries of an irregular shaped image in cocos2d? - iphone

I have an image of mountain with small gutters and tunnels in it. I want to pass a small image through that tunnels. How to trace the intersection of that small image with the exact boundaries of large image in cocos2d?

I would make a collision mask for this.
What this means is to create an exact copy of the image you are using for your terrain except make it only two colors: white and black.
Make the areas that you want the player to be able to move through (not walls) white. Make the walls and anything you want the player to collide with back. Next, just do some pixel collision detection. To do this, I would get the RGB (not RGBA because alpha doesn't matter) data. Loop through this data (or a section of it for better performance) and detect whether or not the player is on a black or white pixel.
Do whatever you need to accordingly.
If you need more help, feel free to ask.

Related

Unity3D - Make texture edges not stretch

I've been searching around for this one for a bit, and unfortunately I can't seem to find any good, consistent results. So, in the Unity UI system, buttons can stretch without becoming pixelated or distorted. This is because the texture is split up into 9 parts - the corners, middle, and sides.
This works because the button's middle and sides are stretched, but not the corners. Then, the button appears not pixelated, at any dimension.
So, the question is as follows: How can I do the same thing for a transparent, unlit texture in 3D space? I have a speech bubble texture on a flat plane that I know how to re-scale to fit the text in the speech bubble.
I've set the texture type to Multiple Sprite, and divided it up into 9 parts. However, I cannot seem to find where I can set the texture to act like the UI button does, and I'm not sure that this is even possible in this way in 3D space.
Is there a way, or should I just make the different parts of the texture different objects, and move them together? That would seem very inefficient and ugly compared to this.
To accomplish what you are asking, you would need to create tiles for this speech bubble and then write a script that procedurally builds a speech bubble based on the plane's scale value. You could also try just changing the texture's Filter Mode to Point.
However I really don't think you should be using textures for this anyway. Why not just use a Unity Canvas and set the Render Mode to World Space? Then you can just set your text box to be a sprite, not a texture, and set its filter mode to Point (See below). This would also make it a lot easier for when you want there to be text in the speech bubble later on.

Unity 2D: Irregular shape window to a different background world?

I want to make an irregular shape display its colors from a different set of images. Currently, I have a flashlight represented by an irregular shape (kite shaped) that when casted over an area, certain objects appear. When the shape is removed, the items disappear. Now, I want to have the background that is within this irregular shape to display an altered version of the background.
I am planning to use a RenderTexture to get its info from a camera that views the flashlight's corrosponding location in the other world, and then use this image as the basis of the flashlight's altered background. However, when I try this, the flashlight shows black instead of the other world. When I texture a plane, the RenderTexture works properly. Anyone have any ideas how to accomplish this?

ios game make a mask layer effect

I need a 'mask' layer that covers the whole screen, with the center part (a circle) to be transparent. Then I can move the mask layer around using touch. User are only able to see the transparent part in the middle.
I don't think a png file can help because the file need to be very large to cover the whole screen.
So is it possible to do it by coding?
i found this online, but don't know much about openGL. http://www.cocos2d-iphone.org/forum/topic/7921.
it would be great if i can use a CCMaskLayer and input with the radius. i can handle the touch event by my self.
the attached png file is expected result, the center part is transparent. i need this to cover my screen, and only show the middle part. the red part is covered.
I write a CCMaskLayer to do the exactly same thing.
https://github.com/smilingpoplar/CCMaskLayer
You may solve this task with cropped circle texture in two ways:
1) Draw sprite with circle texture in screen center and draw another 4 sprites around (on top, bottom, left and right sides) with small red texture but scaled to cover all screen.
2) (more elegant but harder to implement) Make your mask layer fullscreen but adjust texture coordinates. In details:
set wrap mode to GL_CLAMP_TO_EDGE to your circle texture
adjust texture coordinates of your layer vertices (to do this you need to subclass base CCLayer):
Here v means vertex position and t - texture coordinates. You need to set correct texture coordinates for four corner vertices of layer. For future if you will want to drag circle you will need to add some offset values to texture coordinates.

How to draw a light effect over a texture on iPhone using UIKit/Quartz

I have a scene with a background image (a lit room), and a black image (shadow) over that. I need to be able to move my finger over the background and reveal some parts of the scene, simulating a dim light source in a dark room.
My current approach was to generate a mask depending on the position of the touch, and then applying that mask to the shadow image. The problem is I'm generating a new mask and applying it every time I receive a touch event. It's a large image (800x600) and this causes the performance to go down and it increases a lot the memory usage, eventually crashing the game (I think I don't have any memory leaks, but that's not warrantied... anyway the performance itself isn't acceptable).
Can anyone think of a better approach (which doesn't involve using OpenGL ES -- that's not an option in this project) to do this?
To go with my comments above.
Maybe to get around the different shadow levels you could also have a grid of views (squares) between the image and the shadow view. each grid square has a different alpha opacity and when the spot is over a grid square, the grid square's alpha opacity changes to 0. when the spot moves off the grid square it's alpha opacity changes back to it's default.
Without more information it is a little difficult to know whether this approach will work in your case but what you could do is generate a single mask image, say, a radial alpha gradient and then apply an affine transform to it to shape it according to the touches. This can be used to simulate a torch/flashlight beam.
I would try this: use one view with a custom drawRect implemetation: first draw the shadow image (in grayscale) then a bright spot image in white an alpha. And finally the background image in a 'multiply' blend mode.
Just a thought, does the shadow has to be an image? Perhaps you could simply fill the shadow layer with a color and mask it then? This way the memory usage should be less and the effect should be nearly identical (if not exactly the same).
There is no reason to generate a new mask on every touch move. Instead, let the mask be initialized once and manipulate it (reset it's frame) as needed upon touch events.

Getting the pixel color from a image

I'm working in a view based application and am trying to find some code that will let me grab some pixel colors from one of my images and use it for collision detection against one of my UIImageViews but haven't had any luck finding anything on this subject. So if my UIImageView for my player collides with the UIImageView of my map && collides with the color black in my image that's placed inside of my map view... then run collision code... or something along those lines.
Is your question about getting the pixel color, or about doing collision detection?
If you want to get the pixel color, I'm not sure there's an easy way to do it - you may have to mess with your current graphics context to get it, and nothing is coming up in the docs.
If it's just collision detection you want to do, take a look at UIView's convertPoint:toView: and convertPoint:fromView: methods. They let you take defined points within a given view and get their equivalents in other views. With some basic math on the resultant points, you could theoretically do some pretty good collision detection without having to worry about pixel colors.