ios - No bouncing occurs when dragging an object to collide with another object in Sprite Kit -
here video of issue having. can see in video, move gray ball try , collide red ball. when 2 objects collide no bouncing occurs, red ball moves. i've tried playing around densities of red balls, such making red ball densities 0.00001. there no difference in collision behavior.
how can change collision behavior there bouncing?
here properties of gray ball:
func propertiesgrayball() { gray = skshapenode(circleofradius: frame.width / 10 ) gray.physicsbody = skphysicsbody(circleofradius: frame.width / 10 ) gray.physicsbody?.affectedbygravity = false gray.physicsbody?.dynamic = true gray.physicsbody?.allowsrotation = false gray.physicsbody?.restitution = 1 } here properties of red ball:
func propertiesredball { let redball = skshapenode(circleofradius: self.size.width / 20 redball.physicsbody = skphysicsbody(self.size.width / 20) redball.physicsbody?.affectedbygravity = false redball.physicsbody?.dynamic = true redball.physicsbody?.density = redball.physicsbody!.density * 0.000001 redball.physicsbody?.allowsrotation = false redball.physicsbody?.restitution = 1 } here how move gray ball.
override func touchesmoved(touches: set<uitouch>, withevent event: uievent?) { if fingerisongrayball { let touch = touches.first var position = touch!.locationinview(self.view) position = self.convertpointfromview(position) grayball.position = position } } major edits ball had attachments. deleted them simplify problem. that's why comments might not add code.
if move node (with physics body) settings position, directly or skaction, node not part of physics simulation. instead, should move node applying force/impulse or setting velocity. here's example of how that:
first, define variable store position of touch
class gamescene: skscene { var point:cgpoint? then, delete following statement code
redball.physicsbody?.density = redball.physicsbody!.density * 0.000001 lastly, add following touch handlers
override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { /* called when touch begins */ touch in touches { let location = touch.locationinnode(self) let node = nodeatpoint(location) if (node.name == "redball") { point = location } } } override func touchesmoved(touches: set<uitouch>, withevent event: uievent?) { /* called when touch begins */ touch in touches { let location = touch.locationinnode(self) if point != nil { point = location } } } override func touchesended(touches: set<uitouch>, withevent event: uievent?) { point = nil } override func update(currenttime: cftimeinterval) { if let location = point { let dx = location.x - redball.position.x let dy = location.y - redball.position.y let vector = cgvector(dx: dx*100, dy: dy*100) redball.physicsbody?.velocity = vector } }
Comments
Post a Comment