ios7 - SKSpriteNode collision detection error -
i creating game ball suppose bounce off platforms. have set physics properties ball , platform(platform attains physics property when it's below ball). problem is: ball not bouncing (i have applied impulse in didbegincontact method) when ball makes contact platform, detects contact.
here didbegincontact code:
- (void) didbegincontact:(skphysicscontact *)contact { skspritenode *firstnode, *secondnode; firstnode = (skspritenode*) contact.bodya.node; secondnode = (skspritenode*) contact.bodyb.node; if ((contact.bodya.categorybitmask == ballcategory) && (contact.bodyb.categorybitmask == solidplatformcategory)) { nslog(@"platform hit"); cgpoint contactpoint = contact.contactpoint; [_ball.physicsbody applyimpulse:cgvectormake(0, 4) atpoint:contactpoint]; } } ///// here code skspritenode ball - (void) addball { _myball = [skspritenode spritenodewithimagenamed:@"ball.png"]; if (ui_user_interface_idiom() == uiuserinterfaceidiompad) { _myball.scale = 0.4; } else { _myball.scale = 0.3; } _ball.position = cgpointmake(self.frame.size.width/2, _solidplatform.position.y + 2.5*_ball.size.height); _ball.zposition = 2; _ball.name = @"doodle"; _ball.physicsbody = [skphysicsbody bodywithrectangleofsize:_mydoodle.frame.size]; _ball.physicsbody.mass = 1.0; _ball.physicsbody.restitution = 0.8; _ball.physicsbody.dynamic = yes; _ball.physicsbody.allowsrotation = no; _ball.physicsbody.usesprecisecollisiondetection = yes; _ball.physicsbody.categorybitmask = ballcategory; _ball.physicsbody.collisionbitmask = solidplatformcategory; _ball.physicsbody.contacttestbitmask = solidplatformcategory; //skaction *moveupaction = [skaction movebyx:0.0 y:8*numberofplatforms duration:0.5]; [self addchild:_ball]; } ////platform has been defined (not complete code): _solidplatform7.physicsbody = [skphysicsbody bodywithrectangleofsize:_solidplatform7.frame.size]; _solidplatform7.physicsbody.dynamic = no; _solidplatform7.physicsbody.affectedbygravity = no; _solidplatform7.physicsbody.usesprecisecollisiondetection = yes; _solidplatform7.physicsbody.categorybitmask = solidplatformcategory; ps: not getting collusion detection if define platform bodywithedgefromrect
difficult answer without seeing code, i'll try:
if ball not bounce @ all, check restitution property. higher values provide higher "bounciness":
ball.physicsbody.restitution=0.8; if want ball bounce endless between bottom , ceiling can invert gravity after each collision:
self.physicsworld.gravity = cgvectormake(0, self.physicsworld.gravity.dy * (-1)); hope helps. if not, please share code.
i've tried code. smaller changes works:
- (void) addball { // bottom platforms (int i=0; i<10; i++) { skspritenode *mysprite = [skspritenode spritenodewithcolor:[uicolor redcolor] size:cgsizemake(40, 20)]; cgpoint location = cgpointmake(i*40+60, 10); //mysprite.size =cgsizemake(20, 40); mysprite.position=location; mysprite.physicsbody=[skphysicsbody bodywithrectangleofsize:mysprite.size]; mysprite.physicsbody.dynamic=false; mysprite.physicsbody.categorybitmask=solidplatformcategory; [self addchild:mysprite]; } _ball = [skspritenode spritenodewithimagenamed:@"ball.png"]; if (ui_user_interface_idiom() == uiuserinterfaceidiompad) { _ball.scale = 0.4; } else { _ball.scale = 0.3; } _ball.position = cgpointmake(self.frame.size.width/2, self.frame.size.width/2); _ball.zposition = 2; _ball.name = @"doodle"; //_ball.physicsbody = [skphysicsbody bodywithrectangleofsize:_mydoodle.frame.size]; _ball.physicsbody=[skphysicsbody bodywithcircleofradius:_ball.size.width/2]; _ball.physicsbody.mass = 1.0; _ball.physicsbody.restitution = 1; _ball.physicsbody.dynamic = yes; _ball.physicsbody.allowsrotation = no; _ball.physicsbody.usesprecisecollisiondetection = yes; _ball.physicsbody.categorybitmask = ballcategory; _ball.physicsbody.collisionbitmask = solidplatformcategory; _ball.physicsbody.contacttestbitmask = solidplatformcategory; //skaction *moveupaction = [skaction movebyx:0.0 y:8*numberofplatforms duration:0.5]; [self addchild:_ball]; }
Comments
Post a Comment