cocos2d iphone - Chipmunk Physics Positioning -
i've looked around quite bit , haven't been able find concise answer question. cocos2d game have integrated chipmunk physics engine. on initialization, setup boundaries of 'playing field' establishing series of bodies , static shapes follows:
- (void)initplayingfield { // physics parameters cgsize fieldsize = _model.fieldsize; cgfloat radius = 2.0; cgfloat elasticity = 0.3; cgfloat friction = 1.0; // bottom cgpoint lowerleft = ccp(0, 0); cgpoint lowerright = ccp(fieldsize.width, 0); [self addstaticbodytospace:lowerleft finish:lowerright radius:radius elasticity:elasticity friction:friction]; // left cgpoint topleft = ccp(0, fieldsize.height); [self addstaticbodytospace:lowerleft finish:topleft radius:radius elasticity:elasticity friction:friction]; // right cgpoint topright = ccp(fieldsize.width, fieldsize.height); [self addstaticbodytospace:lowerright finish:topright radius:radius elasticity:elasticity friction:friction]; // top [self addstaticbodytospace:topleft finish:topright radius:radius elasticity:elasticity friction:friction]; }
this setup works, except 1 thing-- positions of boundaries appear off significant amount of pixels right. after initializing player, , sending him towards boundary walls, not bounce off edges of screen (as expect setting bounding box @ 0,0), rather number of pixels (maybe 30-50) inwards edges of screen. gives?
i thought issue came rendering player sprite, after running through debugger, looks good. there special protocols follow when using chipmunk's positions render sprites? need multiply sort of pixels_per_meter? matter if in sprite sheet? sprite batch node? here how set sprite position in view:
player *player = [[rootmodel sharedmodel] player]; self.playersprite.position = player.position; self.playersprite.rotation = player.rotation;
and here how set position in model:
- (void)update:(cgfloat)dt { self.position = self.body->p; self.rotation = self.body->a; }
(yes, know it's redundant store position because chipmunk inherently.)
also, bonus question for finished reading post. how might move body @ constant velocity in whatever direction facing, allowing turn left or right @ given time, never slow down?
what's shape of physics body? perhaps forgot consider making shape same size of sprite. if consider position, sprite able move halfway outside screen @ border because position @ center of sprite.
to move body @ constant velocity, set velocity while disabling friction (set 0?) , allowing rebound off of collisions impact speed (in box2d restitution = 1, don't know chipmunk).
Comments
Post a Comment