Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
    <meta name="viewport" content="width=device-width">
    <title>test bounding box</title>
    <script src="https://cdn.jsdelivr.net/npm/planck-js@0.2/dist/planck-with-testbed.js"></script>

</head>
<body>
    <svg id="new" viewBox="-250 -230 700 700" width="100%" height="100%"></svg>
    <script>


        planck.testbed('RopeJoint', function (testbed) {
            var world = planck.World({ gravity: planck.Vec2(0, 0) });
             //data -------------------------------------------------
            let material = {
                density: 0.001,
                friction: 0.1
            };
            let distance_joint_setting = {
                frequencyHz: 0.4,
                dampingRatio: 0.001
            };
            let convex_hull_material = 0.7;
            //fisrt attractor
            let attractorBody1 = world.createBody(planck.Vec2(0, 20));
            //second attractor
            let attractorBody2 = world.createBody(planck.Vec2(20, 0));
            //third attractor
            let attractorBody3 = world.createBody(planck.Vec2(12, 12));
            //last attractor
            let attractorBody_middle = world.createBody(planck.Vec2(10, 10));

            var circle_group1_radius = []; 
            circle_group1_radius.push(2.3);
            circle_group1_radius.push(1.2);
            circle_group1_radius.push(3.0);
            var circle_group1_positions = [];
            circle_group1_positions.push(planck.Vec2(0, 15));
            circle_group1_positions.push(planck.Vec2(-5, 20));
            circle_group1_positions.push(planck.Vec2(0, 25));

            var circle_group2_radius = [];  
            circle_group2_radius.push(1.2);
            circle_group2_radius.push(2.2);
            circle_group2_radius.push(1.0);
            var circle_group2_positions = [];
            circle_group2_positions.push(planck.Vec2(15, 0));
            circle_group2_positions.push(planck.Vec2(25, 0));
            circle_group2_positions.push(planck.Vec2(20, 5));

            var circle_group3_radius = [];  
            circle_group3_radius.push(2.0);
            circle_group3_radius.push(0.5);
            circle_group3_radius.push(1.5);
            var circle_group3_positions = [];
            circle_group3_positions.push(planck.Vec2(25, 20));
            circle_group3_positions.push(planck.Vec2(20, 25));
            circle_group3_positions.push(planck.Vec2(20, 15));

             //generate circle and distance joint  -------------------------------------------------
            for (var n = 0; n < 3; n++) {
                let body_from_group1 = world.createDynamicBody(circle_group1_positions[n]); 
                body_from_group1.setUserData(1);
                var fixture_from_group1 = body_from_group1.createFixture(planck.Circle(planck.Vec2(0, 0), circle_group1_radius[n]), material);
                let distanceJoint_from_group1 = planck.DistanceJoint(distance_joint_setting,
                    attractorBody1,
                    attractorBody1.getPosition(),
                    body_from_group1,
                    body_from_group1.getPosition()
                );
                distanceJoint_from_group1.m_length = 0;
                world.createJoint(distanceJoint_from_group1);

                let body_from_group2 = world.createDynamicBody(circle_group2_positions[n]); 
                body_from_group2.setUserData(2);
                var fixture_from_group2 = body_from_group2.createFixture(planck.Circle(planck.Vec2(0, 0), circle_group2_radius[n]), material);
                let distanceJoint_from_group2 = planck.DistanceJoint(distance_joint_setting,
                    attractorBody2,
                    attractorBody2.getPosition(),
                    body_from_group2,
                    body_from_group2.getPosition()
                );
                distanceJoint_from_group2.m_length = 0;
                world.createJoint(distanceJoint_from_group2);

                let body_from_group3 = world.createDynamicBody(circle_group3_positions[n]); 
                body_from_group3.setUserData(3);
                var fixture_from_group3 = body_from_group3.createFixture(planck.Circle(planck.Vec2(0, 0), circle_group3_radius[n]), material);
                let distanceJoint_from_group3 = planck.DistanceJoint(distance_joint_setting,
                    attractorBody3,
                    attractorBody3.getPosition(),
                    body_from_group3,
                    body_from_group3.getPosition()
                );
                distanceJoint_from_group3.m_length = 0;
                world.createJoint(distanceJoint_from_group3);
            }

            // run simulation -------------------------------------------------
            let timestep = 1.0 / 10.0;
            let velocityIterations = 5;
            let positionIterations = 2;
            for (let i = 0; i < 300; ++i) {// Simulation loop.
                world.step(timestep, velocityIterations, positionIterations);
            }
            // delete all previous joints ------------------------------------------------
            for (let joint = world.getJointList(); joint; joint = joint.getNext()) {
                world.destroyJoint(joint);
            }

            // gather points for generate convex hull  -------------------------------------------------
            var factor_amplify = 0.5;// enlarge all points by a factor
            var points1 = [];// for generate convex hull
            var points2 = []; // for generate convex hull
            var points3 = [];// for generate convex hull
            for (let body = world.getBodyList(); body; body = body.getNext()) {
                for (let fixture = body.getFixtureList(); fixture; fixture = fixture.getNext()) {
                    if (fixture.getShape().getType() == 'circle') {
                        var c_position = body.getPosition();
                        var radius = fixture.getShape().m_radius;
                        var segments = 30;
                        var coord;
                        if (body.getUserData() == 1) {
                            coord = attractorBody1.getPosition();
                        }
                        if (body.getUserData() == 2) {
                            coord = attractorBody2.getPosition();
                        }
                        if (body.getUserData() == 3) {
                            coord = attractorBody3.getPosition();
                        }
                        for (var m = 0; m < segments; m++) {
                            var s_x = c_position.x + radius
                                * Math.cos(2 * Math.PI * m / segments);
                            var s_y = c_position.y + radius
                                * Math.sin(2 * Math.PI * m / segments);
                            var direction_x = s_x - coord.x;
                            var direction_y = s_y - coord.y;
                            if (body.getUserData() == 1) {
                                points1.push({
                                    x: s_x + direction_x * factor_amplify,
                                    y: s_y + direction_y * factor_amplify
                                });
                            }
                            if (body.getUserData() == 2) {
                                points2.push({
                                    x: s_x + direction_x * factor_amplify,
                                    y: s_y + direction_y * factor_amplify
                                });
                            }
                            if (body.getUserData() == 3) {
                                points3.push({
                                    x: s_x + direction_x * factor_amplify,
                                    y: s_y + direction_y * factor_amplify
                                });
                            }
                        }
                    }
                }
            }

            //generate all convex hulls
            // some settings
            var use_box = true;
            var use_edge = false;
            var extra = 0.1;//maybe don't need this extra part for connecting two boxes
            var h_width = 0.2;//fixed width

            var points_array = [];
            points_array.push(points1);
            points_array.push(points2);
            points_array.push(points3);

            for (var i = 0; i < 3; i++) {
                // get the center of the convex hull
                var centroid_x = 0;
                var centroid_y = 0;
                var count_e = 0;
                var chull = convexhull.makeHull(points_array[i]);
                var body = world.createDynamicBody(); //{ bullet: true }
                var hull_name = i+"h";
                body.setUserData(hull_name);
                for (var m = 0; m < chull.length - 1; m++) {
                    var h_center_x = (chull[m].x + chull[m + 1].x) / 2;
                    var h_center_y = (chull[m].y + chull[m + 1].y) / 2;
                    var h_length = Math.sqrt((chull[m].x - chull[m + 1].x) * (chull[m].x - chull[m + 1].x) + (chull[m].y - chull[m + 1].y) * (chull[m].y - chull[m + 1].y)) + extra;
                    var h_angle = Math.atan((chull[m + 1].y - chull[m].y) / (chull[m + 1].x - chull[m].x));
                    var fixture_convex_m;
                    if (use_box) {
                        fixture_convex_m = body.createFixture(planck.Box(h_length / 2, h_width, planck.Vec2(h_center_x, h_center_y), h_angle), convex_hull_material);
                        fixture_convex_m.setUserData(hull_name);
                        var center_box = fixture_convex_m.getShape().m_centroid;
                        let vertex_middle = body.getWorldPoint(center_box);
                        centroid_x += vertex_middle.x;
                        centroid_y += vertex_middle.y;
                        count_e++;
                    }
                    if (use_edge) {
                        fixture_convex_m = body.createFixture(planck.Edge(planck.Vec2(h_center_x - h_length / 2 * Math.cos(h_angle), h_center_y - h_length / 2 * Math.sin(h_angle)), planck.Vec2(h_center_x + h_length / 2 * Math.cos(h_angle), h_center_y + h_length / 2 * Math.sin(h_angle))));
                        fixture_convex_m.setUserData(hull_name);
                        var vertex1 = fixture_convex_m.getShape().m_vertex1;
                        var vertex2 = fixture_convex_m.getShape().m_vertex2;
                        let vertex_begin = body.getWorldPoint(vertex1);
                        let vertex_end = body.getWorldPoint(vertex2);
                        var vertex_middle_x = (vertex_begin.x + vertex_end.x) / 2;
                        var vertex_middle_y = (vertex_begin.y + vertex_end.y) / 2;
                        centroid_x += vertex_middle_x;
                        centroid_y += vertex_middle_y;
                        count_e++;
                    }  
                }
                var h_center_x = (chull[chull.length - 1].x + chull[0].x) / 2;
                var h_center_y = (chull[chull.length - 1].y + chull[0].y) / 2;
                var h_length = Math.sqrt((chull[chull.length - 1].x - chull[0].x) * (chull[chull.length - 1].x - chull[0].x) + (chull[chull.length - 1].y - chull[0].y) * (chull[chull.length - 1].y - chull[0].y)) + extra;
                var h_angle = Math.atan((chull[0].y - chull[chull.length - 1].y) / (chull[0].x - chull[chull.length - 1].x));
                if (use_box) {
                    var fixture_convex = body.createFixture(planck.Box(h_length / 2, h_width, planck.Vec2(h_center_x, h_center_y), h_angle), convex_hull_material);
                    fixture_convex.setUserData(hull_name);
                    var center_box = fixture_convex.getShape().m_centroid;
                    let vertex_middle = body.getWorldPoint(center_box);//body.getWorldPoint(
                    centroid_x += vertex_middle.x;
                    centroid_y += vertex_middle.y;
                    count_e++;
                }
                if (use_edge) {
                    var fixture_convex = body.createFixture(planck.Edge(planck.Vec2(h_center_x - h_length / 2 * Math.cos(h_angle), h_center_y - h_length / 2 * Math.sin(h_angle)), planck.Vec2(h_center_x + h_length / 2 * Math.cos(h_angle), h_center_y + h_length / 2 * Math.sin(h_angle))));
                    fixture_convex.setUserData(hull_name);
                    var vertex1 = fixture_convex.getShape().m_vertex1;
                    var vertex2 = fixture_convex.getShape().m_vertex2;
                    let vertex_begin = body.getWorldPoint(vertex1);
                    let vertex_end = body.getWorldPoint(vertex2);
                    var vertex_middle_x = (vertex_begin.x + vertex_end.x) / 2;
                    var vertex_middle_y = (vertex_begin.y + vertex_end.y) / 2;
                    centroid_x += vertex_middle_x;
                    centroid_y += vertex_middle_y;
                    count_e++;
                }
                //generate joint to center
                let last_distanceJoint = planck.DistanceJoint(distance_joint_setting,
                    attractorBody_middle,
                    attractorBody_middle.getPosition(),
                    body,
                    planck.Vec2(centroid_x / count_e, centroid_y / count_e)
                );
                last_distanceJoint.m_length = 0; 
                world.createJoint(last_distanceJoint);
            }

            return world;
        });



        // referenced from https://www.nayuki.io/page/convex-hull-algorithm
        var convexhull = new function () {

            // Returns a new array of points representing the convex hull of
            // the given set of points. The convex hull excludes collinear points.
            // This algorithm runs in O(n log n) time.
            this.makeHull = function (points) {
                var newPoints = points.slice();
                newPoints.sort(this.POINT_COMPARATOR);
                return this.makeHullPresorted(newPoints);
            };


            // Returns the convex hull, assuming that each points[i] <= points[i + 1]. Runs in O(n) time.
            this.makeHullPresorted = function (points) {
                if (points.length <= 1)
                    return points.slice();

                // Andrew's monotone chain algorithm. Positive y coordinates correspond to "up"
                // as per the mathematical convention, instead of "down" as per the computer
                // graphics convention. This doesn't affect the correctness of the result.

                var upperHull = [];
                for (var i = 0; i < points.length; i++) {
                    var p = points[i];
                    while (upperHull.length >= 2) {
                        var q = upperHull[upperHull.length - 1];
                        var r = upperHull[upperHull.length - 2];
                        if ((q.x - r.x) * (p.y - r.y) >= (q.y - r.y) * (p.x - r.x))
                            upperHull.pop();
                        else
                            break;
                    }
                    upperHull.push(p);
                }
                upperHull.pop();

                var lowerHull = [];
                for (var i = points.length - 1; i >= 0; i--) {
                    var p = points[i];
                    while (lowerHull.length >= 2) {
                        var q = lowerHull[lowerHull.length - 1];
                        var r = lowerHull[lowerHull.length - 2];
                        if ((q.x - r.x) * (p.y - r.y) >= (q.y - r.y) * (p.x - r.x))
                            lowerHull.pop();
                        else
                            break;
                    }
                    lowerHull.push(p);
                }
                lowerHull.pop();

                if (upperHull.length == 1 && lowerHull.length == 1 && upperHull[0].x == lowerHull[0].x && upperHull[0].y == lowerHull[0].y)
                    return upperHull;
                else
                    return upperHull.concat(lowerHull);
            };


            this.POINT_COMPARATOR = function (a, b) {
                if (a.x < b.x)
                    return -1;
                else if (a.x > b.x)
                    return +1;
                else if (a.y < b.y)
                    return -1;
                else if (a.y > b.y)
                    return +1;
                else
                    return 0;
            };

        };

    </script>
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console