Dynamic Object Floating Through Static Object
Posted: Thu Jul 05, 2012 7:00 pm
Hello,
We are designing a platformer game in Cocos2D using Chipmunk physics, and we find that our main character (a dynamic object) does not collide with our platform (a static object) as expected. When the character is above the platform and falls onto it, the collision is fine - the character lands on the platform as expected. However, when the platform is low enough and the main character jumps from underneath it, the character slows down and moves to the top of the platform, almost as if it is sucked in.
We are using Xcode 4.3.3, Cocos2D 1.0.1, and the built in Chipmunk Physics version that comes with these.
Our main character is defined in a class that inherits from CPSprite_Dynamic (that creates dynamic Chipmunk objects). This is the code for that class:
CPSprite_Dynamic.m
Our platform is defined in a class that inherits from CPSprite_Static (which creates static chipmunk objects). This is the code for that class:
CPSprite_Static.m
We have tried downsizing our time step to increase the frequency of collision checks, but that did not work for us. Is this a bug that others have experienced? Please let us know if you have any solutions! Thanks
We are designing a platformer game in Cocos2D using Chipmunk physics, and we find that our main character (a dynamic object) does not collide with our platform (a static object) as expected. When the character is above the platform and falls onto it, the collision is fine - the character lands on the platform as expected. However, when the platform is low enough and the main character jumps from underneath it, the character slows down and moves to the top of the platform, almost as if it is sucked in.
We are using Xcode 4.3.3, Cocos2D 1.0.1, and the built in Chipmunk Physics version that comes with these.
Our main character is defined in a class that inherits from CPSprite_Dynamic (that creates dynamic Chipmunk objects). This is the code for that class:
CPSprite_Dynamic.m
Code: Select all
#import "CPSprite_Dynamic.h"
@implementation CPSprite_Dynamic
@synthesize body;
- (id)initWithSpace:(cpSpace *)theSpace location:(CGPoint)location spriteWithFile:(NSString *)spriteFrameName {
if ((self = [super initWithFile:spriteFrameName])) {
space = theSpace;
[self createBodyAtLocation:location];
canBeDestroyed = YES;
}
return self;
}
- (void)update {
self.position = body->p;
self.rotation = CC_RADIANS_TO_DEGREES(-1 * body->a);
}
- (void)createBodyAtLocation:(CGPoint)location {
float mass = 1.0f;
body = cpBodyNew(mass, cpMomentForBox(mass, self.contentSize.width, self.contentSize.height));
body->p = location;
body->data = self;
cpSpaceAddBody(space, body);
shape = cpBoxShapeNew(body, self.contentSize.width, self.contentSize.height);
shape->e = 0.3f;
shape->u = 1.0f;
shape->data = self;
cpSpaceAddShape(space, shape);
}
Our platform is defined in a class that inherits from CPSprite_Static (which creates static chipmunk objects). This is the code for that class:
CPSprite_Static.m
Code: Select all
#import "CPSprite_Static.h"
@implementation CPSprite_Static
@synthesize body;
- (void)update {
self.position = body->p;
self.rotation = CC_RADIANS_TO_DEGREES(-1 * body->a);
}
- (void)createBodyAtLocation:(CGPoint)location {
body = cpBodyNewStatic();
body->p = location;
body->data = self;
//cpSpaceAddBody(space, body); //this line made our object picture fall due to gravity, so it's out.
shape = cpBoxShapeNew(body, self.contentSize.width, self.contentSize.height);
shape->e = 0.0f;
shape->u = 1.0f;
shape->data = self;
shape->collision_type = 1;
cpSpaceAddShape(space, shape);
}
- (id)initWithSpace:(cpSpace *)theSpace location:(CGPoint)location spriteWithFile:(NSString *)spriteFrameName {
if ((self = [super initWithFile:spriteFrameName])) {
space = theSpace;
[self createBodyAtLocation:location];
canBeDestroyed = YES;
}
return self;
}
We have tried downsizing our time step to increase the frequency of collision checks, but that did not work for us. Is this a bug that others have experienced? Please let us know if you have any solutions! Thanks