How to change border of this image - ios

I am making a game using this function:
if (CGRectIntersectsRect(object.frame, object2.frame)) {
[self GameOver];
}
Both objects are square however the image of object #2 is not. Therefore when the borders collide (but not the actual pictures) the game ends. Is there a way I can have the border "fit" to the image, so that the game only ends when the actual pictures collide.
Thanks :)
**my image is a shark and therefore a rectangle cannot be used

A view's frame is always a CGRect, which is a rectangle. You can use UIKit Dynamics for collision detection with views, but that only support rectangles too.
As #jammycoder mentioned, try out SpriteKit or other game engines, if you need to detect custom shape bounds.

Related

Gravity with UIview

I drew a circle in UIView with a help of UIBezierPath. Then I added gravity behavior to that view using UIGravityBehavior and collision behavior using UICollisionBehavior. When the circle collides with other objects this view bumps as square, but I want to work with this view in collisions as with a circle. How can I do it?
You can’t use UIKit Dynamics to simulate a circle. It works with rectangular bodies only. You can add collision paths, but I think that is only for the reference frame. I recommend looking at SpriteKit’s SKPhysicsBody. Unfortunately, I haven’t used it, so I can’t provide a code sample.

Image based collision detection taking transparency into account

Basically this is my code to check if two UIImageViews collide:
if (CGRectIntersectsRect(self.fishImage.frame, self.minebomb.frame))
{
// some code
}
The code above only detects when the two frames collide. How do I detect when non-transparent pixels collide?

How to apply full-screen SKEffectNode for post-processing in SpriteKit

I'm trying out SpriteKit with the following setup:
An SKScene with two child nodes used merely for grouping other
nodes: foreground and background.
background is really empty as of now, but would eventually hold some
type of background sprite / layers.
foreground is a SKEffectNode and whenever the user taps on the
screen, a new intance of a SKnode subclass which represents a game
element is added as child to it.
This SKNode subclass basically creates 3 SKShapeNodes and two labels: an outter
circumference, an inner circumference, 2 labels and an inner quarter circumference. The inner quarter circumference has an SKAction that
makes it rotate forever about its origin / center.
Now here's the issue, as long as foreground doesn't have any CIFilter or has shouldEnableEffects = NO, everything is fine. That is, I can tap on the screen and my game elements are instantiated and added to the main scene. But the minute I try adding a CIGaussianBlur or CIBloom to the foreground, I notice two things:
The framerate drops to about 2fps. Mind you, this happens even with
as little as 6 nodes alive in the scene.
The effect seems to be constantly cropping its contents or adjusting
it's frame. That is, if I have one node, the "full screen" effect
seems to try and constantly crop or adjust its bounds to the minimum
area required to hold all nodes. This is for one node:
And this is for 2 nodes:
In OpenGL ES 2, one would do a post blur / bloom by basically rendering the whole framebuffer (all objects) to texture, then doing at least one more pass to blur,etc on that texture and then either present that in the framebuffer attached to the display or compose that with the original render back to the framebuffer. I'd expect SKEffectNode to work in a similar way. However the cropping and the poor performance makes me think I might be using the effect node the wrong way. Any thoughts?
It seems to be a bug with the SKEffectNode trying to apply a filter on children SKShapeNodes as far as I can tell. I played around with this and achieved your results, but when I switched out the SKShapeNodes for SKSpriteNodes (using a simple png of a circle) the cropping no longer appears. It's a bug in that SKEffectNode doesn't handle the stroke of the SKShapeNode very well. If you take off the stroke (lineWidth = 0) and give it a fill color, you'll see that there is no cropping.
As for the frame rate, SKShapeNodes perform poorly. Doing the switch to SKSpriteNodes I mentioned earlier boosted my fps from 40 to 50 when I had 35 nodes on the screen (iPhone 5) with the filter applied.

ccDraw. Fill screen everywhere except on sprite

So I am trying to get a very basic "flashlight"-style thing going in one of my games.
The way I was getting it to work, was having a layer on top of my game screen, and this layer would draw a black rectangle with ~ 80% opacity, creating the look of darkness on top of my game scene.
ccDrawSolidRect(ccp(0,0), ccp(480,320), ccc4f(0, 0, 0, 0.8));
What I want to do is draw this rectangle EVERYWHERE on the screen, except for around a cone of vision that will represent the "light source".
What this would create would be a dark overlay on top of everything except for the light, giving it the illusion of a torch/light/flashlight.
The only way I can foresee this happening is by using ccDrawSolidPoly(), but since the position of the light source changes, so would the vertices for the poly.
Any suggestions on how to achieve this would be great.
You can use ccDrawSolidPoly() and avoid having to manually update vertices. For this you can create a new subclass of CCNode representing your light object, and do your custom shape drawing in its -(void)draw method.
The ccDraw...() functions will draw relative to the local sprite coordinates, so you can then move and rotate your new sprite to suit your needs and cocos2d will do the vertices transformations for you.
Update: I found out that you might be better off subclassing CCDrawNode instead of CCNode, as it has some facilities for raw OpenGL drawing (OpenGL's vertexArrayBuffer and vertexBufferObject internal variables and a buffer for vertices, their colors and their texCoords). If your stuff is very simple, maybe subclassing the plain CCNode is enough.
Could a png be used instead as a mask, as the layer above
Like that binocular vision you sometimes see in cartoons?
Or a filter similar to a photoshop mask that darkens as it grows outwardly to wards the edge of the screen
Just a thought anyway...
A picture of more of what your trying to explain might be good too

How to use a UIView with a CCLayer cocos2d?

I am playing a short video with CCVideoPlayer in cocos2d and at the end of it I am capturing the very last frame of the video and I am showing it on the screen using a UIView because trying to draw it on a CCLayer makes the image show up with slightly different colors. I would imagine that is because the way that things are drawn in cocos2d is different than the way they are drawn on a UIView. So I need some way to keep the image on the screen Using the UIView, and I need to be able to draw sprites etc... on top of this view. So my question is, Is there a way to make a CCLayer transparent so that the UIView containing the image can still be seen and then draw on top of the image using a CCLayer?
(P.S I am using cocos2d v1.1.0-beta2b)

Resources