collision detection in cocos2d - iphone

i want to detect collision detection two times in same row.
for example:-(see the below image)
the ellipse and rectangle or detcted. after that my ellipse will travelling in the straight line path to down and detect the another rectangle.
first one is( travelled in trajectory path ) working fine. second one i want to pass in straight line to down for collision detection.
how to do this process.

Use the Box2D physics library for collision detection. It is by far the best option in your case and elegantly supported in Cocos2d.
See here: http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone

As i know cocos2d have no collision detection of sprites because it's not a phys engine. If you want the collision be detected automatically use Box2D or chipmunk physics engine, supported by cocos2d.
If the number of object you want to check for collision is small you can just run over your object and check if some of them (or only one if it's enough for you) overlaps with the others.
Making more complex collision detection will bring you for writing a collision detection part of a physics engine. It's much simpler to use en existing one

Related

SpriteKit and particle collision

I have a particle emitter and I would like to detect it when the particles collide some physics bodies.
Is there a native way to do that in the SpriteKit API or do I need "to cheat" ?
Individual particles can not collide. Not with physics, not any other way. You do not even get any information about an individual particle - you can't access it's position, rotation, velocity .. nothing.
If you wanted to "cheat" you'd have to emulate the particle emitter using sprites, and animate the sprites with actions or manually. However keep in mind that this is much less efficient than a particle emitter.
In addition, if we're talking "particles" which often means dozens or even hundreds of them on screen, the amount of physics processing and collision detection quickly becomes prohibitively expensive if you were to model them using sprites with physics bodies attached. Do a performance test before you go down this path.
Particles do not have physics bodies, so they don't collide with Sprite Kit's physics engine
You can set the physics body of the particle emitter the same way you set it for any sprite node. Then you can set the category bit mask property and the contact test bit mask. The method didBeganContact can detect the collision afterwards.
didBegan contact is called whenever two bodies contact each other . Here is the apple reference link for how this method works:
Click [here](https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsContactDelegate_Ref/Reference/Reference.html#//apple_ref/occ/intfm/SKPhysicsContactDelegate/didBeginContact:"Apple iOS developer library")!

Collision detection for race course in iphone

i am wondering how could I detect collision on the course given in the attached image for example:
In XNA that could be done easily but i dunno how to make it possible in OpenGLES for such scenario as simple collision with rectangles is not a big deal but for this case I need help.
Opengl doesn't support any native collision detection, its just a polygon rendering utility. If you wanted to preform collision detection on the given image, you could set up an edge finder, and then load the detected edges into your own custom collision detection algorithm - but that has nothing to do with opengl or opengles.
For collision detection a simple and very dirty solution is to map your player position to the bitmap's coordinate system and check the pixel color/value. Grey is on-course, white is off.
Collision response is a whole other question :)

Cocos2d - Creating collidable Sprites?

Hello everyone I an very new to cocos2d, so I apologize if this is a simple question. I would like to create to sprites that collide when they hit each other.
For instance, one sprite, spriteA, is in a fixed position on the screen. And another sprite, spriteB, is moving around the screen. SpriteB will collide with spriteA. If that doesn't make sense or you don't understand it, tell me and I will elaborate further. Any help is appreciated. Thank YOU!
Try using Chipmunk or Box2d physics systems. These are included with Cocos2d and work by having a physics simulation that updates with every time the graphics change on screen.
The physics simulation will tell you if two objects are overlapping, and will provide physical attributes like weight, slipperiness (friction), speed and direction, which creates reactions like bouncing, sliding, realistic loss of speed and changes of direction on impact.
If you are new to physics simulation, here's a 30 second run down:
Create "bodies" in the physics simulation that represent each graphical sprite
Bodies are usually defined more simply than their graphical counterparts, like a circle, square or simple polygon shape
In order to update the graphics on the screen accurately, first you establish a pixels to meters ratio. Meters are notional (i.e. made up) measurement that is used in the physics sim
Each time the physics simulation "ticks", you update the graphics on screen accordingly
So if a body in the physics sim moves 1 meter, you might transform the pixel sprite 32 pixels
Here's a good tute on collision detection using Box2d.
http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone
enjoy
Its actually very simple:
Just schedule a timer: [self schedule:#selector(checkForCollision:)];
Specify the method: - (void)checkForCollision:(ccTime)dt {}
In the curly braces, make CGRects for each sprite using CGRectMake.
Then in the same method, just call: if (CGRectIntersectsRect) {}
That easy!
Well technically speaking when the 2 sprites interact, or share at least one common point, then they are colliding. I'm a little confused by your question. Are you asking for direction on how to make the sprite move on screen, or are you asking how to handle the actual collision(such as calling a method if they collide)?

Iphone I am using cocos2d and need the rgba value of a pixel on a sprite

I am programing a shooter game in cocos2d for the iphone. I and I want to exclude collision detection an the alpha of my sprite.
How do I read the RGBA value of the single pixel.
Thanks for the help.
First off, think about if you really want to do this -- it's much more computationally expensive than other methods of collision detection (i.e, bounding box, circle collision) which may not be as accurate, but for most purposes work well enough.
If you really do want to do this, you should generate a mask for each of your sprites that you want to detect the collision between. This mask should ideally be a simple 1-bit representation of your sprite.
Next, for each sprite you want to check for a collision, you need to do a few steps:
Since the pixel accurate collision is going to take a long time, do a bounding box collision first between the two sprites to see if you even need to check for pixel level collision.
If you know the sprites have collided at the boundaries, calculate the rect of the collision. You may want to do an optimization at this stage that says if they are greater than X% overlapped that they have collided. Especially if your alpha areas of the sprite are along the outside edges.
Using the rect area of the collision, go through the masks of each of your sprite -- if both pixels are 1, you have a collision -- exit out of the loop and do what you need to mark them as collided.
As you can see, this is very CPU heavy compared to the other methods, which is why I recommended against it at the beginning. Unless a system has hardware support for pixel accurate collisions (the Atari 2600 actually had this), most games are not going to use pixel values for their collisions.
If you decide to go with a bounding box approach, you can do things like shrinking the bounding box used for collisions to prevent false positives for when two sprites touch each other at a corner.
Good Luck!
Not specific to Cocos2d, but should work on any iPhone project -- check out this question & answer

dealing with lots of collision

What would be the best way to go about adding collision to my application. Right now I have a lot of jagged walls and a couple of weird shapes that I wanna make collision for but am not sure which is the right path to get the job done. What would you guys do if you had a room full of walls with different shapes and sizes that needed collision implemented ?
I would read a set of articles on collision detection. Paul Nettle used to write about the topic (PDF) and has a nice library for free.
This document will describe a
collision technique that allows you to
move an ellipsoid (a sphere with three
different radii, one for each axis)
through a world that not only properly
detects collisions, but also reacts in
a way that gamers would expect from
the common first person shooter.
This technique also allows for sliding
along surfaces as well as easy
implementation of gravity, which
includes sliding downhill when
standing stationary. This technique
also allows automatic climbing of
stairs and sliding over bumps in walls
(like door frames) and any other
randomly oriented “stair-like
topography”.
You can use Chipmunk physics engine, which has a very good physics + collisions.
Or even Cocos2d-iphone library - 2d game engine with Chipmunk inside. Here are examples of games, created with it.