Help on multiple same ball colliding with land

Official forum for the Chipmunk2D Physics Library.
Post Reply
must_think
Posts: 4
Joined: Fri Jan 29, 2010 7:45 am
Contact:

Help on multiple same ball colliding with land

Post by must_think »

Hi

Hope someone can shed some light. Had stuck for few days. Its an iPhone apps.. Pardon me as I am a learner in Obj-C and OO as well

Objective of the app is to have multiple thrown balls collided or hit on a specific area of the land.

I created an an object class called ball
Ball.m
----------------------------------
@implementation Ball
@synthesize isHitLand;

-(id) initWithCPBody: (cpBody *) bodyIn
{
self = [super init];
if(self != nil){
ballBody = bodyIn;
isHitLand = NO;
}
return self;
}
@end
--------------------------------------------
isHitLand is a bool.

In my GameLayer.h, I defined the following
---------------------------------------
@interface GameLayer : Layer {
..
..
Ball *smallBall;
...
@end
----------------------------------------
In my GameLayer.m, I create the object with the following
----------------------------------------------------
-(void) createSmallBall
{
smallBall = [[Ball alloc] initWithCPBody:[self makeBallX:kballX y:kballY name:@"ball.png" colType:kCollisionTypeBallSmall]];

}

-(cpBody *) makeBallX:(float) x y:(float)y name:(NSString *)name colType:(int)colType
{

Sprite *emoti = [[Sprite alloc] init];
emoti = [[Sprite spriteWithFile:name] retain];
emoti.position = cpv(x,y);
[self addChild:emoti];
cpBody *emotiBody = cpBodyNew(100.0,INFINITY);
emotiBody->p = cpv(x,y);
emotiBody->v = cpvmult(cpvforangle(6.1),100);
cpSpaceAddBody(space, emotiBody);
emotiShape = cpCircleShapeNew(emotiBody, kEmotiRadius, cpvzero);
emotiShape->e = 1.1; emotiShape->u = 0.5;
emotiShape->data = emoti;
emotiShape->collision_type = colType;
emotiShape->group = 2;
cpSpaceAddShape(space, emotiShape);

return emotiBody;
}
--------------------------------------------

In my collision function. (outside of the GameLayer implementation)
static int landCollision(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
GameLayer *game = (GameLayer *) data;
[game keephitSmallball:a];
return 0;
}
-------------------------------------

My keephitSmallball as following (within the GameLayer implementation)
-------------------------------------
-(void) keephitSmallball:(cpShape *)a
{
if(smallBall.isHitLand == NO)
{
NSLog(@"Ball hitted target on land");
smallBall.isHitLand == YES;
[cleanupShapeArray addObject:[NSValue valueWithPointer:a]];
[self delayclearShapes]
}

------------------------------------------------------------------

My problem
I created 4 balls on interval of 1 seconds and added to the space. Bacause smallBall is a global (if I don't get the term wrong), everytime when 1 ball hit the land on a specific area, the keephitSmallball will set the isHitLand to YES and the other 3 balls will have set to YES as well. How can I ensure that the keephitSmallball only set the 1st ball isHitLand to YES without affecting other balls? How can I ensure that whatever operation being done to the class/object only for that object. Will the collision function have any visibility on which ball collided with the land. I know that I should not set the smallBall global but I need some guidance and direction on how to achieve the objective without doing what I am currently doing. [ FYI, if the ball thrown 1 at a time - after the 1st ball collided and then generate new, then everything work perfectly]

Many thanks for those who able to shed some light.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Help on multiple same ball colliding with land

Post by slembcke »

You need to use the shape->data pointer to get at your game or sprite object in some way. It looks like you currently set that to be a Cocos2D sprite, but then never use it. You could subclass the Sprite class, or set a different pointer to data that has it's own isHit boolean.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
must_think
Posts: 4
Joined: Fri Jan 29, 2010 7:45 am
Contact:

Re: Help on multiple same ball colliding with land

Post by must_think »

My shape->data is point to the Sprite. I can point it to an NSObject that has the isHit information but if I change the shape->data from pointing to other thing other than the sprite, the simulation not working. I guess, the shape->data is being used to simulate the sprite on the scene. My step is as follow
----------------------------------------
-(void) step: (ccTime) delta
{
int steps = 2;
cpFloat dt = delta/(cpFloat)steps;

for(int i=0; i<steps; i++){
cpSpaceStep(space, dt);
}
cpSpaceHashEach(space->activeShapes, &eachShape, nil);

}
----------------------------------
And eachShape is
--------------------------------------
static void
eachShape(void *ptr, void* unused)
{
cpShape *shape = (cpShape*) ptr;
Sprite *sprite = shape->data;
if( sprite ) {
cpBody *body = shape->body;
[sprite setPosition: cpv( body->p.x, body->p.y-10)];
// [sprite setRotation: (float) CC_RADIANS_TO_DEGREES( -body->a )];
}
}
------------------------------------------------
Anyway, I can use the shape->data information to simulate the sprite and at the same time pass the NSObject pointer to the callback function?

Thanks
must_think
Posts: 4
Joined: Fri Jan 29, 2010 7:45 am
Contact:

Re: Help on multiple same ball colliding with land

Post by must_think »

Or are they any better way to encapsulate Boolean info into the body or shape? Currently using NSobject to wrap body and Boolean info. The objective just to have some state information for the body. Eg ishit, isActibe etc.
User avatar
Tam Toucan
Posts: 141
Joined: Tue Jun 23, 2009 4:26 pm
Contact:

Re: Help on multiple same ball colliding with land

Post by Tam Toucan »

You can create your own class which has a Sprite pointer and a isHit Boolean. Then change data to point at that and update the eachShape function to get the Sprite pointer from that object. The collison function can the use isHit as Scott suggested.
must_think
Posts: 4
Joined: Fri Jan 29, 2010 7:45 am
Contact:

Re: Help on multiple same ball colliding with land

Post by must_think »

Thanks Scott and Tam. Got it. Will try to work on that suggestion.
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests