jBullet Collision/Physics not working correctly
Posted
by
Kenneth Bray
on Game Development
See other posts from Game Development
or by Kenneth Bray
Published on 2012-10-21T21:03:07Z
Indexed on
2012/10/21
23:20 UTC
Read the original article
Hit count: 557
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.
package Object;
import static org.lwjgl.opengl.GL11.GL_QUADS; import static org.lwjgl.opengl.GL11.glBegin; import static org.lwjgl.opengl.GL11.glColor3f; import static org.lwjgl.opengl.GL11.glEnd; import static org.lwjgl.opengl.GL11.glPopMatrix; import static org.lwjgl.opengl.GL11.glPushMatrix; import static org.lwjgl.opengl.GL11.glVertex3f;
import javax.vecmath.Matrix4f; import javax.vecmath.Quat4f; import javax.vecmath.Vector3f;
import com.bulletphysics.collision.shapes.BoxShape; import com.bulletphysics.collision.shapes.CollisionShape; import com.bulletphysics.dynamics.RigidBody; import com.bulletphysics.dynamics.RigidBodyConstructionInfo; import com.bulletphysics.linearmath.DefaultMotionState; import com.bulletphysics.linearmath.Transform;
public class Cube {
// Cube size/shape variables
private float size;
boolean cubeCollidable;
boolean cubeDestroyable;
// Position variables - currently this defines the center of the cube
private float posX;
private float posY;
private float posZ;
// Rotation variables - should be between 0 and 359, might consider letting rotation go higher though I can't think of a purpose currently
private float rotX;
private float rotY;
private float rotZ;
//collision shape is a box shape
CollisionShape fallShape;
// setup the motion state for the ball
DefaultMotionState fallMotionState;
Vector3f fallInertia = new Vector3f(0, 1, 0);
RigidBodyConstructionInfo fallRigidBodyCI;
public RigidBody fallRigidBody;
int mass = 1;
// 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;
// define the physics based on the values passed in
fallShape = new BoxShape(new Vector3f(size, size, size));
fallMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(0, 50, 0), 1f)));
fallRigidBodyCI = new RigidBodyConstructionInfo(mass, fallMotionState, fallShape, fallInertia);
fallRigidBody = new RigidBody(fallRigidBodyCI);
}
public void Update() {
Transform trans = new Transform();
fallRigidBody.getMotionState().getWorldTransform(trans);
posY = trans.origin.x;
posX = trans.origin.y;
posZ = trans.origin.z;
}
public void Draw() {
fallShape.calculateLocalInertia(mass, fallInertia);
// center point posX, posY, posZ
float radius = size / 2;
//top
glPushMatrix();
glBegin(GL_QUADS);
{
glColor3f(1.0f,0.0f,0.0f); // red
glVertex3f(posX + radius, posY + radius, posZ - radius);
glVertex3f(posX - radius, posY + radius, posZ - radius);
glVertex3f(posX - radius, posY + radius, posZ + radius);
glVertex3f(posX + radius, posY + radius, posZ + radius);
}
glEnd();
glPopMatrix();
//bottom
glPushMatrix();
glBegin(GL_QUADS);
{
glColor3f(1.0f,1.0f,0.0f); // ?? color
glVertex3f(posX + radius, posY - radius, posZ + radius);
glVertex3f(posX - radius, posY - radius, posZ + radius);
glVertex3f(posX - radius, posY - radius, posZ - radius);
glVertex3f(posX + radius, posY - radius, posZ - radius);
}
glEnd();
glPopMatrix();
//right side
glPushMatrix();
glBegin(GL_QUADS);
{
glColor3f(1.0f,0.0f,1.0f); // ?? color
glVertex3f(posX + radius, posY + radius, posZ + radius);
glVertex3f(posX + radius, posY - radius, posZ + radius);
glVertex3f(posX + radius, posY - radius, posZ - radius);
glVertex3f(posX + radius, posY + radius, posZ - radius);
}
glEnd();
glPopMatrix();
//left side
glPushMatrix();
glBegin(GL_QUADS);
{
glColor3f(0.0f,1.0f,1.0f); // ?? color
glVertex3f(posX - radius, posY + radius, posZ - radius);
glVertex3f(posX - radius, posY - radius, posZ - radius);
glVertex3f(posX - radius, posY - radius, posZ + radius);
glVertex3f(posX - radius, posY + radius, posZ + radius);
}
glEnd();
glPopMatrix();
//front side
glPushMatrix();
glBegin(GL_QUADS);
{
glColor3f(0.0f,0.0f,1.0f); //blue
glVertex3f(posX + radius, posY + radius, posZ + radius);
glVertex3f(posX - radius, posY + radius, posZ + radius);
glVertex3f(posX - radius, posY - radius, posZ + radius);
glVertex3f(posX + radius, posY - radius, posZ + radius);
}
glEnd();
glPopMatrix();
//back side
glPushMatrix();
glBegin(GL_QUADS);
{
glColor3f(0.0f,1.0f,0.0f); // green
glVertex3f(posX + radius, posY - radius, posZ - radius);
glVertex3f(posX - radius, posY - radius, posZ - radius);
glVertex3f(posX - radius, posY + radius, posZ - radius);
glVertex3f(posX + radius, posY + radius, posZ - radius);
}
glEnd();
glPopMatrix();
}
}
© Game Development or respective owner