Jump to content

Box2d Soft body


NistorCristian
 Share

Recommended Posts

I'm trying to make a soft body width Box2D after this tutorial:

http://www.emanueleferonato.com/2012/09/21/step-by-step-creation-of-a-box2d-soft-body-blob/

This is my code and I attached a picture.

// Enable Box2D physics
        game.physics.startSystem(Phaser.Physics.BOX2D);
        game.physics.box2d.debugDraw.joints = true;
        game.physics.box2d.gravity.y = 500;
        game.physics.box2d.setBoundsToWorld();
        game.physics.box2d.restitution = 0.8;
        game.physics.box2d.setBoundsToWorld();

        // Set up handlers for mouse events
        game.input.onDown.add(mouseDragStart, this);
        game.input.addMoveCallback(mouseDragMove, this);
        game.input.onUp.add(mouseDragEnd, this);

        var particleNumber = 15;
        var particleDistance = 50;
        var blobX = 320;
        var blobY = 240;

        //  You can also create a Circle body directly, without binding it to a sprite:
        var circle = new Phaser.Physics.Box2D.Body(game, null, blobX, blobY);
        circle.setCircle(16);
        circle.bullet = true;

        for (var i = 0; i < particleNumber; i++) {

            var angle = (2*Math.PI) / particleNumber * i;
            var posX = blobX + particleDistance * Math.cos(angle);
            var posY = blobY + particleDistance * Math.sin(angle);
            var smallBall = new Phaser.Physics.Box2D.Body(game, null, posX, posY);
            smallBall.setCircle(2);
            smallBall.bullet = true;   
            dynamicBodies.push( smallBall );

            //bodyA, bodyB, length, ax, ay, bx, by, frequency, damping
            game.physics.box2d.distanceJoint(circle, smallBall);

        };   

        for (var i = 0; i < particleNumber; i++) {
            if (i < dynamicBodies.length - 1) {
                game.physics.box2d.distanceJoint(dynamicBodies[i], dynamicBodies[i+1]);
            }else{
                game.physics.box2d.distanceJoint(dynamicBodies[i], dynamicBodies[0]);
            };
        }

I have few problems:

1) I don't know how to make it soft when it touches the ground.

2) If I drag the object fast and smash it into the wall it will be like the second picture.

 

Thank you

softbody.png

123.png

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...