Objects fall through the ground. Gravity

General Box2D issues or C++ specific issues
Pilsje
Posts: 1
Joined: Fri Jan 06, 2017 4:34 am

Objects fall through the ground. Gravity

Postby Pilsje » Mon Jan 09, 2017 3:24 am

Hello,

I am a graphic design student with coding as subject. This semester we are experimenting with box2d. I am working on a website where every text elements falls to the ground. I implemented gravity in my website, but the problem is that it won't stop falling. I want to set the ground to my own position and not to the bottom of the screen. Here is my problem.. I managed to do this with random shapes thanks to a tutorial. Though I have struggles with combining the different parts of codes in one code.

I have two different options. In this option the text falls through the ground:

Code: Select all

   var w = $(window).width();
      var h = $(window).height();

      createBox.position.x = 9;
      createBox.position.7x = 19;

      //Do one animation interation and start animating
      interval = setInterval(update,1000/60);
      update();
    }


In my other code I don't know how to change the random shapes into my own divs.

Code: Select all

<script type="text/javascript">
      var world;
     
      function init() {
         var   b2Vec2 = Box2D.Common.Math.b2Vec2
            ,   b2BodyDef = Box2D.Dynamics.b2BodyDef
            ,   b2Body = Box2D.Dynamics.b2Body
            ,   b2FixtureDef = Box2D.Dynamics.b2FixtureDef
            ,   b2Fixture = Box2D.Dynamics.b2Fixture
            ,   b2World = Box2D.Dynamics.b2World
            ,   b2MassData = Box2D.Collision.Shapes.b2MassData
            ,   b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
            ,   b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
            ,   b2DebugDraw = Box2D.Dynamics.b2DebugDraw
            ;
         
         world = new b2World(
               new b2Vec2(0, 10)    //gravity
            ,  true                 //allow sleep
         );
         
         var fixDef = new b2FixtureDef;
         fixDef.density = 1.0;
         fixDef.friction = 0.5;
         fixDef.restitution = 0.2;
         
         var bodyDef = new b2BodyDef;
         
         //create ground
         bodyDef.type = b2Body.b2_staticBody;
         bodyDef.position.x = 9;
         bodyDef.position.y = 50; //grond positie
         fixDef.shape = new b2PolygonShape;
         fixDef.shape.SetAsBox(10, 0.5);
         world.CreateBody(bodyDef).CreateFixture(fixDef);
         
         //create some objects
         bodyDef.type = b2Body.b2_dynamicBody;
         for(var i = 0; i < 10; ++i) {
            if(Math.random() > 0.5) {
               fixDef.shape = new b2PolygonShape;
               fixDef.shape.SetAsBox(
                     Math.random() + 0.1 //half width
                  ,  Math.random() + 0.1 //half height
               );
            } else {
               fixDef.shape = new b2CircleShape(
                  Math.random() + 0.1 //radius
               );
            }
            bodyDef.position.x = Math.random() * 10;
            bodyDef.position.y = Math.random() * 10;
            world.CreateBody(bodyDef).CreateFixture(fixDef);
         }
         
         //setup debug draw
         var debugDraw = new b2DebugDraw();
         debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
         debugDraw.SetDrawScale(30.0);
         debugDraw.SetFillAlpha(0.3);
         debugDraw.SetLineThickness(1.0);
         debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
         world.SetDebugDraw(debugDraw);
         
         window.setInterval(update, 1000 / 60);
      };
     
      function update() {
         world.Step(
               1 / 60   //frame-rate
            ,  10       //velocity iterations
            ,  90       //position iterations
         );
         world.DrawDebugData();
         world.ClearForces();
      };
   
   </script>


I really hope someone can help me out, since the deadline is coming closer and closer. Thank you for taking your time for reading this and hopefully someone can help me out!!

Thank you

Louis Langholtz
Posts: 20
Joined: Tue Dec 20, 2016 10:53 am

Re: Objects fall through the ground. Gravity

Postby Louis Langholtz » Fri May 05, 2017 7:00 am

It may be too late by now but if this is still a problem you may also want to post this issue on one of the StackExchange websites like https://gamedev.stackexchange.com .


Return to “Bugs, Requests, and Feedback”



Who is online

Users browsing this forum: No registered users and 2 guests