ios - moving paddles only pushing the puck, not hitting it -
i trying move paddle, if hits puck, puck should move away. puck move, more paddle pushing it.
declaring paddle:
p1.fillcolor = skcolor.blackcolor() p1.name = p1name p1.physicsbody = skphysicsbody(circleofradius: 30) p1.physicsbody.restitution = 1.0 p1.physicsbody.angulardamping = 0.0 p1.physicsbody.allowsrotation = false p1.physicsbody.dynamic = true p1.physicsbody.categorybitmask = p1category p1.physicsbody.collisionbitmask = wallcategory | pcategory p1.physicsbody.contacttestbitmask = pcategory p1.position = cgpointmake(self.frame.width/2, self.frame.height/4) self.addchild(p1) delcaring puck:
puck.fillcolor = skcolor.redcolor() puck.name = pname puck.physicsbody = skphysicsbody(circleofradius: 25) puck.physicsbody.restitution = 0.8 puck.physicsbody.angulardamping = 0.5 puck.physicsbody.lineardamping = 0.5 puck.physicsbody.allowsrotation = false puck.physicsbody.usesprecisecollisiondetection = true puck.physicsbody.friction = 0.0 puck.physicsbody.dynamic = true puck.physicsbody.categorybitmask = pcategory puck.physicsbody.collisionbitmask = wallcategory | p1category puck.physicsbody.contacttestbitmask = p1category puck.position = cgpointmake(self.frame.width/2, self.frame.height/2) self.addchild(puck) how move paddle:
override func touchesbegan(touches: nsset, withevent event: uievent) { /* called when touch begins */ touch: anyobject in touches { allow location = touch.locationinnode(self) allow body: skphysicsbody = self.nodeatpoint(location).physicsbody if (body.node.name == self.p1name) { self.fingeronp1 = true self.p1.physicsbody.collisionbitmask = wallcategory } } } override func touchesmoved(touches: nsset, withevent event: uievent) { touch: anyobject in touches { allow location = touch.locationinnode(self) allow previouslocation = touch.previouslocationinnode(self) allow body = self.nodeatpoint(location).physicsbody if (body != nil && body.node != nil) { if (body.node.name == self.p1name && self.fingeronp1 == true) { allow paddle = self.childnodewithname(p1name) skshapenode var paddlex: int = int(paddle.position.x) + int(location.x - previouslocation.x) allow _x: int = int(paddle.frame.width/2) paddlex = max(paddlex, _x) paddlex = min(paddlex, int(self.size.width)-_x) var paddley: int = int(paddle.position.y) + int(location.y - previouslocation.y) allow _y: int = int(paddle.frame.height/2) paddley = max(paddley, _y) paddley = min(paddley, int(self.size.height)-_y) paddle.position = cgpointmake(cgfloat(paddlex), cgfloat(paddley)) } } } } override func touchesended(touches: nsset, withevent event: uievent) { var onp1 = false touch: anyobject in touches { allow location = touch.locationinnode(self) if (self.nodeatpoint(location) == p1) { onp1 = true } } if (onp1 == true) { self.fingeronp1 == false self.p1.physicsbody.collisionbitmask = wallcategory | pcategory } } i guess problem i'm not applying forcefulness paddle, i'm moving paddle, too. how apply forcefulness while moving it?
i thought of apply impulse puck after contact, have no thought how move proper angle or create move @ proper speed.
func didbegincontact(contact: skphysicscontact) { var firstbody: skphysicsbody! var secondbody: skphysicsbody! if (contact.bodya.categorybitmask > contact.bodyb.categorybitmask) { firstbody = contact.bodya secondbody = contact.bodyb } else { firstbody = contact.bodyb secondbody = contact.bodya } if (firstbody.categorybitmask == p1category && secondbody.categorybitmask == pcategory) { if (self.fingeronp1) { nslog("hit", nil) secondbody.applyimpulse(cgvectormake(20, 0)) } } } ios swift sprite-kit game-physics
No comments:
Post a Comment