Trying to bounce a box with a plane using DirectX 11.1

Post Reply
andreahmed
Posts: 3
Joined: Tue Nov 18, 2014 9:50 pm

Trying to bounce a box with a plane using DirectX 11.1

Post by andreahmed »

I'm trying to simulate a box that bounces with a plane and I'm beginner with bullet physics. I created a plane (box) and and a box that is falling. The problem is the ball is stalled while colliding with the plane and doesn't rotate (no bouncing effect), even if the box is rotated so that it can sharply bounce with the plane. Here is my configuration.

Code: Select all

if (m_pBoxMotionState) 
    {
        btTransform transform;
        // get the world transform from our motion state
        m_pBoxMotionState->getWorldTransform(transform);

        btQuaternion quat;
        quat.setEuler(0, 290, 0); //or quat.setEulerZYX depending on the ordering you want
        transform.setRotation(quat);

        XMMATRIX world = btTransform_to_XMMATRIX(transform);
        m_Cube->Draw(world, m_Graphics.getViewMatrix(), m_Graphics.getProjectionMatrix());
    }


m_Grid.CreateGrid(360.0f, 360.0f, 50, 50);
    m_Grid.SetPosition(1, -90, 1);
    m_Cube = GeometricPrimitive::CreateCube(m_Graphics.getContext(), 5.0f, false);

      m_pCollisionConfiguration = new btDefaultCollisionConfiguration();
            // create the dispatcher
            m_pDispatcher = new btCollisionDispatcher(m_pCollisionConfiguration);
            // create the broadphase
            m_pBroadphase = new btDbvtBroadphase();
            // create the constraint solver
            m_pSolver = new btSequentialImpulseConstraintSolver();
            // create the world
            m_pWorld = new btDiscreteDynamicsWorld(m_pDispatcher, m_pBroadphase, m_pSolver, m_pCollisionConfiguration);
            m_pWorld->setGravity(btVector3(0, -10, 0));

            btBoxShape* pBoxShape = new btBoxShape(btVector3(1.0f, 1.0f, 1.0f));
            // give our box an initial position of (0,0,0)
            btTransform transform;
            transform.setIdentity();
            transform.setOrigin(btVector3(0.0f, 50, 0.0f));
            pBoxShape->calculateLocalInertia(4, btVector3(0, 0, 0));
            // create a motion state
            m_pBoxMotionState = new btDefaultMotionState(transform);
            // create the rigid body construction info object, giving it a 
            // mass of 1, the motion state, and the shape
            btRigidBody::btRigidBodyConstructionInfo rbInfo(4.0f, m_pBoxMotionState, pBoxShape);
            btRigidBody* pRigidBody = new btRigidBody(rbInfo);
            pGroundShape->calculateLocalInertia(0, btVector3(0, 0, 0));
            btBoxShape* pGroundShape = new btBoxShape(btVector3(360, 1,360));
            btTransform groundShapeTransform;
            groundShapeTransform.setIdentity();
            groundShapeTransform.setOrigin(btVector3(1.0f, -90.0f, 1.0f));

            // create a motion state
            m_pGroundMotionState = new btDefaultMotionState(groundShapeTransform);
            btRigidBody::btRigidBodyConstructionInfo rbGroundInfo(0, m_pGroundMotionState, pGroundShape);
            rbGroundInfo.m_friction = 1.0f;
            rbGroundInfo.m_restitution = 1.0f;
            btRigidBody* pRigidBodyGround = new btRigidBody(rbGroundInfo);

            // inform our world that we just created a new rigid body for 
            // it to manage
            m_pWorld->addRigidBody(pRigidBodyGround);
            m_pWorld->addRigidBody(pRigidBody);
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Trying to bounce a box with a plane using DirectX 11.1

Post by Flix »

andreahmed wrote:I'm trying to simulate a box that bounces with a plane and I'm beginner with bullet physics. I created a plane (box) and and a box that is falling
Ok, you got a box and a plane.
andreahmed wrote:The problem is the ball is stalled while colliding with the plane
I suppose you mean, the box is stalled...
andreahmed wrote:Here is my configuration.[...]
andreahmed wrote:pBoxShape->calculateLocalInertia(4, btVector3(0, 0, 0));
[...]
pGroundShape->calculateLocalInertia(0, btVector3(0, 0, 0));
This is wrong!

Code: Select all

virtual void btCollisionShape::calculateLocalInertia 	( 	btScalar  	mass,
		btVector3 &  	inertia	 
	) 			const
Here the inertia is just calculated and returned by reference without modifying the collision shape (it's a const method!).Try something like:

Code: Select all

btVector3 localInertia;
pBoxShape->calculateLocalInertia(4, localInertia);
btRigidBody::btRigidBodyConstructionInfo rbInfo(4,pBoxMotionState,pBoxShape,localInertia);
[Also note that inertia should be strictly necessary only for moving bodies].
andreahmed
Posts: 3
Joined: Tue Nov 18, 2014 9:50 pm

Re: Trying to bounce a box with a plane using DirectX 11.1

Post by andreahmed »

Thanks, but that actually didn't solve the problem. Still the box sinks into the ground and never get bounced
Post Reply