How to stop PhysicsSprite from bouncing off Wall?

  • efares
    Likes 0

    Problem Description

    I am creating a wall like this:

    // Wall
            var bottomWall = new cp.SegmentShape(this.space.staticBody,
                cp.v(0, this.bottomWallHeight),
                cp.v(MAX_INT, this.bottomWallHeight),
                wallWidth);
            bottomWall.setElasticity(0);
            bottomWall.setFriction(0);
    
            this.space.addStaticShape(bottomWall);

    And making a physicsSprite shape like this:

    // Create and add the physics shape
            var carShape = new cp.BoxShape(this.carBody, carSize.width * scaleCarSize, carSize.height * scaleCarSize);
            carShape.setElasticity(0);
            carShape.setFriction(0);
            this.space.addShape(carShape);

    And then when a button is pressed I apply this force to the phyics shape:

    // Apply down impulse on player
        downImpulse: function () {
            this.carBody.applyImpulse(cp.v(0, -500), cp.v(0, 0));
        },

    Then when the carBody hits the wall segment, it bounces back and creates a constant force the other way. How can I make it so that the car just stops from moving any lower and stays against the bottomWall?

  • efares
    Likes 0

    I have gotten collision detection working but how do I set the movement to 0 exactly?


    This reply has been verified.
  • Sonar Systems admin
    Likes 0

    Try setImpulse.

  • efares
    Likes 0

    No, setImpulse doesn’t work, it’s applyImpulse but even when you apply cc.v(0,0) it doesn’t really stop it, you have to apply an equal and opposite force on the body to stop it.

    I fixed it by comparing the height/position of the wall and height/position of the car then applied an opposite force when they got close right before colliding.


Login to reply