When you drag an object into a collision with another object in the sprite set, there is no bouncing

Here is a video with the question I have. As you can see in the video, I move the gray ball to try to collide with the red ball. When both objects collide, no bouncing occurs, the red ball simply moves. I tried to play with a density of red balls, for example, with a density of red balls of 0.00001. There were no differences in collision behavior.

How can I change the collision behavior to bounce?

Here are the properties of the 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 are the properties of the 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
}

This is how I move the 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 changes The ball initially had attachments. I deleted them to simplify the problem. Therefore, comments may not match the code.

+4
1

node ( ) , SKAction, node . node, / . , :

,

class GameScene: SKScene {
    var point:CGPoint?

redBall.physicsBody?.density = redBall.physicsBody!.density * 0.000001

,

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

    for 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 a touch begins */

    for 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
    }
}
+3

Source: https://habr.com/ru/post/1609711/


All Articles