SKSpriteNode Accelerated Crash Speed

I am making an iOS application and am facing some physics issues. As you can tell from .GIF below, when I rotate the hexagon and the ball hits the rectangle at an angle, it loses part of its speed and does not bounce as high. This is because the general is common here (mainly because I limit the horizontal position of the balls, it uses only vertical speed when it hits an angle, thereby losing speed).

I can’t understand in my whole life how to solve a problem. Does anyone have any ideas?

Code for node ball:

func createBallNode(ballColor: String) -> SKSpriteNode {
    let ball = SKSpriteNode(imageNamed: ballColor)
    ball.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame)+30)
    ball.zPosition = 1

    ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
    ball.physicsBody?.affectedByGravity = true
    ball.physicsBody?.restitution = 1
    ball.physicsBody?.linearDamping = 0
    ball.physicsBody?.friction = 0

    ball.physicsBody?.categoryBitMask = ColliderType.Ball.rawValue
    ball.physicsBody?.contactTestBitMask = ColliderType.Rect.rawValue
    ball.physicsBody?.collisionBitMask = ColliderType.Rect.rawValue

    let centerX = ball.position.x
    let range = SKRange(lowerLimit: centerX, upperLimit: centerX)

    let constraint = SKConstraint.positionX(range)
    ball.constraints = [constraint]

    return ball
}

enter image description here

+4
source share
1

, , , , , "". .

" ", , ( , ). SKAction, .

class GameScene: SKScene {

    override func didMove(to view: SKView) {
        super.didMove(to: view)
        let ball = createBallNode()
        self.addChild(ball)
    }

    func createBallNode() -> SKSpriteNode {
        let ball = SKSpriteNode(imageNamed: "ball")
        ball.position = CGPoint(x: frame.midX, y: frame.minY + ball.frame.height / 2)

        let goUp = SKAction.move(by: CGVector(dx: 0, dy: 600), duration: 1)
        goUp.timingMode = .easeOut

        let goDown = SKAction.move(by: CGVector(dx: 0, dy: -600), duration: 1)
        goDown.timingMode = .easeIn

        let goUpAndDown = SKAction.sequence([goUp, goDown])
        let forever = SKAction.repeatForever(goUpAndDown)
        ball.run(forever)
        return ball
    }
}

enter image description here

Update

, Hexagon,

class GameScene: SKScene {

    override func didMove(to view: SKView) {
        super.didMove(to: view)
        let ball = createBallNode()
        self.addChild(ball)
    }

    func createBallNode() -> SKSpriteNode {
        let ball = SKSpriteNode(imageNamed: "ball")
        ball.position = CGPoint(x: frame.midX, y: frame.minY + ball.frame.height / 2)

        let goUp = SKAction.move(by: CGVector(dx: 0, dy: 600), duration: 1)
        goUp.timingMode = .easeOut

        let goDown = SKAction.move(by: CGVector(dx: 0, dy: -600), duration: 1)
        goDown.timingMode = .easeIn

        let check = SKAction.customAction(withDuration: 0) { (node, elapsedTime) in
            self.ballTouchesBase()
        }

        let goUpAndDown = SKAction.sequence([goUp, goDown, check])
        let forever = SKAction.repeatForever(goUpAndDown)
        ball.run(forever)
        return ball
    }

    private func ballTouchesBase() {
        print("The ball touched the base")
    }
}

, ballTouchesBase , y. .

+1

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


All Articles