What are the SKPhysics settings to make a ball bounce?

I've created a ball, and boxed my game scene.

The balls physics settings are:

self.physicsBody!.affectedByGravity = true
self.physicsBody!.restitution = 1.0
self.physicsBody!.linearDamping = 0
self.physicsBody!.friction = 0.3
self.physicsBody!.isDynamic = true
self.physicsBody!.mass = 0.5
self.physicsBody?.contactTestBitMask = bodyMasks.blankMask.rawValue
self.physicsBody?.collisionBitMask  = bodyMasks.edgeMask.rawValue
self.physicsBody?.categoryBitMask   = bodyMasks.ballMask.rawValue

When I box the game scene with:

self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)

the ball bounces back up as intended.

However, if I replace edgeLoopFrom with my own custom borders:

yourLine.lineWidth = 5
yourLine.path = pathToDraw
yourLine.isUserInteractionEnabled = false
yourLine.strokeColor = .clear
yourLine.isHidden = false
yourLine.zPosition = 10
yourLine.physicsBody = SKPhysicsBody(edgeLoopFrom: pathToDraw)
yourLine.physicsBody?.affectedByGravity = false
yourLine.physicsBody?.pinned = true
yourLine.physicsBody?.categoryBitMask       = bodyMasks.edgeMask.rawValue
yourLine.physicsBody?.collisionBitMask      = bodyMasks.ballMask.rawValue
yourLine.physicsBody?.contactTestBitMask    = bodyMasks.blankMask.rawValue 

the ball just comes down with a thud, stops, and doesn't bounce.,

So am trying to figure out what physics settings I have to give to my custom made edges so that the ball bounces?

Answered by Claude31 in 691475022

And their restitutions doesn't combine, the software just used the higher one?

restitution is how much energy the physics body loses when it bounces off another object.

So it seems logical to have 0.8 as a result.

You should try:

ball.restitution = 0.4  // <<-- Swapped with block
block.restitution = 0.8

My guess would be

restitution used by physics engine should be 0.4

I probably miss something (as I'm not using SpriteKit a lot).

In init(edgeLoopFrom rect: CGRect) rect is the rectangle that defines the edges. The rectangle is specified relative to the owning node’s origin.

So you need only to set with your own rect

self.physicsBody = SKPhysicsBody(edgeLoopFrom: myOwnRect)

How did you define this rect in yourLine ?

It turns out that swapping out

yourLine.physicsBody = SKPhysicsBody(edgeLoopFrom: pathToDraw)

for

yourLine.physicsBody = SKPhysicsBody(edgeChainFrom" pathToDraw)

made it work

Not sure why though.

The only difference I note in doc is: For init(edgeLoopFrom:), if the path is not already closed, a loop is automatically created by joining the last point to the first.

How did you define pathToDraw ?

  • If closed path, the 2 init should be equivalent.
  • If not, problem probably comes from open path when using edgeChainFrom.

Note: I cannot why it has impact !

How did you define pathToDraw ?

I ran the code below four times, once for each edge.

func makeLine (pA: CGPoint, pB: CGPoint) -> SKShapeNode {

    let yourLine = SKShapeNode()
    let pathToDraw = CGMutablePath()

    pathToDraw.move(to: pA)
    pathToDraw.addLine(to: pB)
    yourLine.lineWidth = 5
    yourLine.path = pathToDraw
    yourLine.isUserInteractionEnabled = false
    yourLine.strokeColor = .clear
    yourLine.isHidden = false
    yourLine.zPosition = 1
    yourLine.physicsBody = SKPhysicsBody(edgeLoopFrom: pathToDraw)
    yourLine.physicsBody?.affectedByGravity = false
    yourLine.physicsBody?.isDynamic = false
    yourLine.physicsBody?.pinned = true
    yourLine.physicsBody?.categoryBitMask       = bodyMasks.edgeMask.rawValue
    yourLine.physicsBody?.collisionBitMask      = bodyMasks.ballMask.rawValue
    yourLine.physicsBody?.contactTestBitMask    = bodyMasks.blankMask.rawValue
    
    return yourLine
}

So to make sure I understand...from your posts and my playing around with the code, it doesn't matter where the higher restitution is? i.e. ball, edge, block

And their restitutions doesn't combine, the software just used the higher one?

i.e.  
ball.restitution = 0.8
block.restitution = 0.4

restitution used by physics engine will be 0.8
Accepted Answer

And their restitutions doesn't combine, the software just used the higher one?

restitution is how much energy the physics body loses when it bounces off another object.

So it seems logical to have 0.8 as a result.

You should try:

ball.restitution = 0.4  // <<-- Swapped with block
block.restitution = 0.8

My guess would be

restitution used by physics engine should be 0.4

buggy forum. I have to repost to get the previous post visible !

What are the SKPhysics settings to make a ball bounce?
 
 
Q