How to change restitution value when the collision occurs, depending on the secondBody?

I am trying to change the restitution of a physic object depending on what it collides. The problem is the restitution works “after” the collision, the restitution works fine for the next collision.
I tried to change the collision right after the contact is detected, or with didSimulatePhysics but I also have the same issue. Would you know how to solve this? The goal is to have a bouncing effect when two balls collide (if the restitution is the same : 1, the first moving ball does not bounce. But when the ball collides on the edge, it has to be almost stopped right away).

Here is the code :

override func didSimulatePhysics() {
    if ( canShowRestitution) {
        println("allo")
        bodyToChange!.restitution = 0.1
    }
}

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
    }

    let contactScene = (firstBody.categoryBitMask & SCENE_EDGE_CATEGORY) != 0
    let contactScene_Ball = (secondBody.categoryBitMask & BALL_CATEGORY) != 0
    let contactScene_BallUser = (secondBody.categoryBitMask & BALL_USER_CATEGORY) != 0

    if ( contactScene && ( contactScene_BallUser || contactScene_Ball ) ){
        bodyToChange = secondBody
    }
}

func createBallPhysics(type:UInt32)->SKPhysicsBody{
    var body = SKPhysicsBody(circleOfRadius: ball.frame.width / 2)
    body.friction = 0;
    if ( type == BALL_USER_CATEGORY ){
        body.restitution = 1
    }
    else {
        body.restitution = 1
    }
    //body.restitution = 1; //bounce back with equal force
    body.linearDamping = 0;
    body.allowsRotation = false;
    body.categoryBitMask = type
    body.contactTestBitMask = BALL_USER_CATEGORY | BALL_CATEGORY
    body.collisionBitMask = SCENE_EDGE_CATEGORY | BALL_USER_CATEGORY | BALL_CATEGORY
    return body
}

//WORLD
func createPhysics(){
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: billard.frame)
    self.physicsBody!.categoryBitMask = SCENE_EDGE_CATEGORY
    self.physicsBody!.contactTestBitMask = BALL_CATEGORY | BALL_USER_CATEGORY
    self.physicsWorld.gravity = CGVectorMake(0,0)
    // 0 = ball is not slowed down
    self.physicsBody!.friction = 0;

    ball.physicsBody = createBallPhysics(BALL_USER_CATEGORY)
    ball2.physicsBody = createBallPhysics(BALL_CATEGORY)

}

Thanks


Download how.to.change.restitution.value.when.the.collision.occurs.depending.on.the.secondbody.zip
Direct Link


Download


Download how.to.change.restitution.value.when.the.collision.occurs.depending.on.the.secondbody.zip
Mediafire


Download


Download how.to.change.restitution.value.when.the.collision.occurs.depending.on.the.secondbody.zip
4Shared


Download

Both comments and pings are currently closed.

Comments are closed.