0
\$\begingroup\$

Below is the code for one of my objects in the game I am creating (yes although this is a cube, I am not making anything remotely like MineCraft), and my issue is I while the cube will display and is does follow the physics if the cube falls, it does not interact with any other objects in the game. If I was to have multiple cubes in screen at once they all just sit there, or shoot off in all directions never stopping. Anyway, I am new to jBullet, and any help would be appreciated.

    // Constructor
public Cube(float pX, float pY, float pZ, float pSize)  {
    posX = pX;
    posY = pY;
    posZ = pZ;
    size = pSize;
    rotX = 0;
    rotY = 0;
    rotZ = 0;

    // physics stuff
    fallMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(posX, posY, posZ), 1)));
    fallRigidBodyCI = new RigidBodyConstructionInfo(mass, fallMotionState, fallShape, fallInertia);
    fallRigidBody = new RigidBody(fallRigidBodyCI);
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Welcome to GDSE. We're not fixing bugs, take a look at FAQ: gamedev.stackexchange.com/faq Stack Exchange is about solving repeatable problems. Please rephrase your question in the way it may in future show up in google for someone else having a similar problem. Changing title and replacing the code with a special case designed just to show the problem would be a good start. \$\endgroup\$ Oct 21, 2012 at 21:30
  • 1
    \$\begingroup\$ Move fallShape.calculateLocalInertia(mass, fallInertia); to before you create RigidBodyConstructionInfo \$\endgroup\$
    – msell
    Oct 22, 2012 at 5:28
  • \$\begingroup\$ I tried your recommendation msell, but everything still just kinda doesnt sit where they are coded to. They just fly around.. I think my issue is on the below line, any ideas? fallMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(posX, posY, posZ), 1))); \$\endgroup\$
    – user22020
    Oct 22, 2012 at 22:02

1 Answer 1

0
\$\begingroup\$

From the given code I cannot say what the problem could be. The following code is working fine for me.

First create the physics world:

// Create the physics world
BroadphaseInterface broadphase = new DbvtBroadphase();
CollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
Dispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);
ConstraintSolver solver = new SequentialImpulseConstraintSolver();
DiscreteDynamicsWorld dynamicsWorld =
    new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
dynamicsWorld.setGravity(new Vector3f(0, gravity, 0));

Then create physics objects:

// Create a physics object (sphere)
MotionState motionState = new DefaultMotionState();
CollisionShape collisionShape = new SphereShape(radius);
Vector3f localInertia = new Vector3f(0, 0, 0);
collisionShape.calculateLocalInertia(mass, localInertia);
RigidBody rigidBody = new RigidBody(mass, motionState, collisionShape, localInertia);
rigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
Transform transform = new Transform();
transform.origin.set(x, y, z);
rigidBody.setWorldTransform(transform);

And finally add the physics object to the physics world:

// Add the object to the world
dynamicsWorld.addRigidBody(rigidBody);
\$\endgroup\$
1
  • \$\begingroup\$ That helped out quite a bit, I like the activationstate disabling deactivation. \$\endgroup\$
    – user22020
    Oct 24, 2012 at 3:45

You must log in to answer this question.