How to set collision type for game like crayon ball?

Official forum for the Chipmunk2D Physics Library.
ipmart
Posts: 12
Joined: Fri Apr 23, 2010 11:36 pm
Contact:

How to set collision type for game like crayon ball?

Post by ipmart »

Hi all,

Im a newbie in chipmunk and must say im very impressed with the physics engine, great work :shock: :shock: :shock:

Im having problem with a collision type.
I'm trying to detect a collision between balls with many different color, very similar to crayon ball
http://howlingmoonsoftware.com/iPhoneCrayonball.php

I set all balls to have collision type 1 and add cpSpaceAddCollisionHandler like this:
cpSpaceAddCollisionHandler(space, 1, 1, &collisionBegin, &collisionPreSolve, nil, &collisionSeparate, self);

But all balls doesnt collide with each other.

How do you set the collision type and cpSpaceAddCollisionHandler to achieve same thing like crayon ball?


Many thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to set collision type for game like crayon ball?

Post by slembcke »

For Crayon Ball, each color of ball was a collision type (0-5) and we created collision handlers for each collision of a type to itself. It was as simple as a for loop for setting them up.

Code: Select all

for(int i=0; i<6; i++)
	cpSpaceAddCollisionHandler(_space, i, i, NULL, (cpCollisionPreSolveFunc)ballCollisionFunc, NULL, NULL, self);
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ipmart
Posts: 12
Joined: Fri Apr 23, 2010 11:36 pm
Contact:

Re: How to set collision type for game like crayon ball?

Post by ipmart »

Thank you for replying,

I tried that but balls with same color doesnt collide with each other
only balls with different color will collide, the same color ball pass through each other.

How can i fix this?

Thanks in advance
ipmart
Posts: 12
Joined: Fri Apr 23, 2010 11:36 pm
Contact:

Re: How to set collision type for game like crayon ball?

Post by ipmart »

oh i found the problem, its because im returning 0 in the presolve function thats why the ball pass through
Silly me :roll: :roll:

oh ya another thing, would you mind to give me tips on how to detect 3 balls in same color touching each other?

Thanks in advance
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to set collision type for game like crayon ball?

Post by slembcke »

What you are looking for is how to find connected components of the graph. http://en.wikipedia.org/wiki/Connected_ ... ph_theory)
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ipmart
Posts: 12
Joined: Fri Apr 23, 2010 11:36 pm
Contact:

Re: How to set collision type for game like crayon ball?

Post by ipmart »

Thank you for replying

Im trying to figure out the structure now.
Meanwhile im having problem accessing variable that passed as user data like this:

I set the handler this way
cpSpaceAddCollisionHandler(space, sheetCounter, sheetCounter, &collisionBegin, nil, nil, &collisionSeparate, self);
self is ccLayer

int collisionBegin(cpArbiter *arb, cpSpace *space, void *data)
{
CCLayer *myLayer = (CCLayer *)data;
int ballShapeArrayCount = [myLayer.ballShapeArray count]; <--- this one error: request for member 'ballShapeArray' in something not a structure or union

}

please let me know what error that i make


Many thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to set collision type for game like crayon ball?

Post by slembcke »

That's a compiler error. First of all, are you sure the specific type is a CCLayer? By doing myLayer.ballShapeArray you are asking for the ballShapeArray property of CCLayer. If the property is defined on a subclass, you'll need to cast it to that class first. If you meant to get a field you need to use the '->' operator, and you also need to cast it to the correct class first.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
ipmart
Posts: 12
Joined: Fri Apr 23, 2010 11:36 pm
Contact:

Re: How to set collision type for game like crayon ball?

Post by ipmart »

im passing self which is a CCLayer class into user data

I tried this
CCLayer *myLayer = (CCLayer *)data;
BallShape *myArray = (BallShape *)myLayer.ballShapeArray;
it come out with this error: request for member 'ballShapeArray' in something not a structure or union

if i change to this
CCLayer *myLayer = (CCLayer *)data;
BallShape *myArray = (BallShape *)myLayer->ballShapeArray;
this error: 'struct CCLayer' has no member named 'ballShapeArray'

please give me example of how do you access data variable if you pass in ssLayer into the user data field

Many thanks
ipmart
Posts: 12
Joined: Fri Apr 23, 2010 11:36 pm
Contact:

Re: How to set collision type for game like crayon ball?

Post by ipmart »

Here is the header of ballshape.h

#include <stdlib.h>
#import <Foundation/Foundation.h>
#import "chipmunk.h"
#import "cpShape.h"

@interface BallShape : NSObject {

cpShape *shapePointer;

}

@property (nonatomic) cpShape *shapePointer;

@end



and the implementation:
#import "BallShape.h"


@implementation BallShape

@synthesize shapePointer;

@end


I use this class to wrap cpshape pointer so i can add into nsmutablearray
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How to set collision type for game like crayon ball?

Post by slembcke »

Looks like CCLayer is the Cocos2D layer class. Unless you changed the Cocos2D source, it is not going to have a field or property named ballShapesArray. What class was self? In order to call a property or use the -> operator, the compiler needs to know the precise class of the object you are calling it on. Neither can be used dynamically, it won't give you a warning like calling an unknown method, it's just a hard error.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 9 guests