Hi Guys,
This is my first time playing around with cocos2D iPhone and the Chipmunk physics engine, and I was wondering if I could get some help. I have two objects on my screen, one is a marker that should follow the users touch, the other is an object that I would like to move with physics when the marker has collided with it. I have been trying things for a few hours now, but being new too Obj-C and C, a lot of the information seems to be going over my head. I have the all the basic stuff set up in my file, chipmunk should be running, and I have the dragging working on my marker Sprite, but if someone could please give me some insight into how I could get my marker to "bump" the second object that would be great. Below please find the code that I have been using inside of my GameLayer( which extends the cocos2d Layer ). Thank you for any help that can be provided. It is greatly appreciated.
#import "GameLayer.h"
#import "chipmunk.h"
#import "Director.h"
@implementation GameLayer
@synthesize marker1;
@synthesize sumo1;
@synthesize background;
- (id) init{
self = [super init];
if(self != nil){
isTouchEnabled = YES;
//create the background
background = [Sprite spriteWithFile:@"BackGround.png"];
[background setTransformAnchor:CGPointMake(0, 0)];
[self addChild:background];
//create the first marker
marker1 = [[Marker alloc] init];
marker1.position = CGPointMake(50, 50);
[self addChild:marker1];
sumo1 = [[SumoWrestler alloc] init];
sumo1.position = CGPointMake(160, 240);
[self addChild:sumo1];
[self setupChipmunk];
}
return self;
}
#pragma mark chipmuk setup
- (void)setupChipmunk{
//init chipmunk
cpInitChipmunk();
//set up a space to work within
space = cpSpaceNew();
//define a gravity vector
space->gravity = cpv(0, -50);
//resize the spatial hash to increase collison detection speed
//the space to use, average object radius, approx amount of objects
cpSpaceResizeActiveHash(space, 50.0f, 10);
//create the body for marker #1
marker1Body = cpBodyNew(INFINITY, INFINITY);
marker1Body->p = cpv(50, 50);
cpSpaceAddBody(space, marker1Body);
//create the shape for marker #1
marker1Shape = cpCircleShapeNew(marker1Body, 12.5, cpvzero);
marker1Shape->u = 0.5; //friction
marker1Shape->data = marker1;
marker1Shape->collision_type = 1;
//add the shape to the space
cpSpaceAddShape(space, marker1Shape);
//setup the sumo body
sumo1Body = cpBodyNew(INFINITY, INFINITY);
sumo1Body->p = cpv(160, 240);
cpSpaceAddBody(space, sumo1Body);
//set up the shape for sumo1
sumo1Shape = cpCircleShapeNew(sumo1Body, 50, cpvzero);
sumo1Shape->u = 0.8;
sumo1Shape->data = sumo1;
sumo1Shape->collision_type = 0;
//set up collision data
cpSpaceAddCollisionPairFunc(space, 0, 1, &collFunc, self);
//set up the timer to loop over the tick method
[NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
- (void)tick:(NSTimer *)timer{
//cpSpaceHashEach(space->activeShapes, &collFunc, nil);
cpSpaceStep(space, (1.0f/30.0f) );
}
static int collFunc(cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data)
{
NSLog(@"collision function");
if(a->collision_type != 1){
return 0;
}else{
return 1;
}
}
#pragma mark TOUCHES
- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
NSLog(@"touch has begain at :: x:%f, y:%f", location.x, location.y);
return YES;
}
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
cpShape *shape = marker1Shape;
[shape->data setPosition: [[Director sharedDirector] convertCoordinate:location] ];
return YES;
}
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
NSLog(@"touch has end at :: x:%f, y:%f", location.x, location.y);
return YES;
}
#pragma mark DEALLOC
- (void) dealloc{
[sumo1 dealloc];
[background dealloc];
[marker1 dealloc];
[super dealloc];
}
@end
Thanks,
Michael