CollisionDetectionDemo - Please can you help a complete newbie to get this code working
I have tried to implement the bullet CollisionDetectionDemo.cpp as a java demo along the lines of the other jbullet demos. Unfortunately I am new to jbullet, don't know either c++ or openGL, hence I am stuck!
Here is my code:
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans
http://continuousphysics.com/Bullet/This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
///
/// CollisionInterfaceDemo shows high level usage of the Collision Detection.
///
import com.bulletphysics.collision.shapes.BoxShape;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Matrix3f;
import javax.media.j3d.Transform3D;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.dispatch.CollisionWorld;
import com.bulletphysics.collision.dispatch.CollisionObject;
import com.bulletphysics.collision.narrowphase.VoronoiSimplexSolver;
import com.bulletphysics.collision.broadphase.AxisSweep3;
import com.bulletphysics.collision.narrowphase.ManifoldPoint;
import com.bulletphysics.collision.narrowphase.SimplexSolverInterface;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.narrowphase.PersistentManifold;
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
import com.bulletphysics.linearmath.Transform;
import static com.bulletphysics.demos.opengl.IGL.*;
import com.bulletphysics.demos.opengl.GLDebugDrawer;
import com.bulletphysics.demos.opengl.IGL;
import com.bulletphysics.demos.opengl.LWJGL;
import org.lwjgl.LWJGLException;
import com.bulletphysics.demos.opengl.DemoApplication;
import com.bulletphysics.demos.opengl.*;
public class CollisionInterfaceDemo extends DemoApplication {
float yaw=0.f;
float pitch=0.f;
float roll=0.f;
int maxNumObjects = 4;
int numObjects = 2;
GLDebugDrawer debugDrawer;
CollisionObject[] objects = new CollisionObject[maxNumObjects];
CollisionWorld collisionWorld;
static VoronoiSimplexSolver sGjkSimplexSolver; //




?
SimplexSolverInterface gGjkSimplexSolver = sGjkSimplexSolver; //?




?
//GL_Simplex1to4 simplex;





public CollisionInterfaceDemo(IGL gl){
super(gl);
setDebugMode(

;
}
public static void main(String[] args) throws LWJGLException {
CollisionInterfaceDemo collisionInterfaceDemo = new CollisionInterfaceDemo(LWJGL.getGL());
collisionInterfaceDemo.initPhysics();
collisionInterfaceDemo.clientResetScene();
LWJGL.main(args, 800, 600, "Collision Interface Demo",collisionInterfaceDemo);
}
public void initPhysics(){
//m_debugMode |= btIDebugDraw::DBG_DrawWireframe;
Matrix3f basisA = new Matrix3f();
basisA.setIdentity();
Matrix3f basisB = new Matrix3f();
basisB.setIdentity();
objects[0] = new CollisionObject();
objects[1] = new CollisionObject();
objects[0].getWorldTransform(new Transform()).set(basisA);
objects[1].getWorldTransform(new Transform()).set(basisB);
//btPoint3 points0[3]={btPoint3(1,0,0),btPoint3(0,1,0),btPoint3(0,0,1)};
//btPoint3 points1[5]={btPoint3(1,0,0),btPoint3(0,1,0),btPoint3(0,0,1),btPoint3(0,0,-1),btPoint3(-1,-1,0)};
BoxShape boxA = new BoxShape(new Vector3f(1,1,1));
BoxShape boxB = new BoxShape(new Vector3f(0.5f,0.5f,0.5f));
//ConvexHullShape hullA(points0,3);
//hullA.setLocalScaling(btVector3(3,3,3));
//ConvexHullShape hullB(points1,4);
//hullB.setLocalScaling(btVector3(4,4,4));
objects[0].setCollisionShape(boxA);//&hullA;
objects[1].setCollisionShape(boxB);//&hullB;
DefaultCollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);
Vector3f worldAabbMin = new Vector3f(-1000,-1000,-1000);
Vector3f worldAabbMax = new Vector3f(1000,1000,1000);
AxisSweep3 broadphase = new AxisSweep3(worldAabbMin,worldAabbMax);
//SimpleBroadphase is a brute force alternative, performing N^2 aabb overlap tests
//SimpleBroadphase* broadphase = new btSimpleBroadphase;
collisionWorld = new CollisionWorld(dispatcher,broadphase,collisionConfiguration);
collisionWorld.addCollisionObject(objects[0]);
collisionWorld.addCollisionObject(objects[1]);
for (int i=0;i<numObjects;i++){
System.out.println("Object=" + objects
);
System.out.println("Object collisionShape=" + objects.getCollisionShape());
System.out.println("Object worldTransform=" + objects.getWorldTransform(new Transform()));
}
}
//to be implemented by the demo
public void clientMoveAndDisplay(){
displayCallback();
}
public void displayCallback() {
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gl.glDisable(GL_LIGHTING);
collisionWorld.setDebugDrawer(new GLDebugDrawer(gl));
if (collisionWorld != null) collisionWorld.performDiscreteCollisionDetection();
///one way to draw all the contact points is iterating over contact manifolds / points:
int numManifolds = collisionWorld.getDispatcher().getNumManifolds();
System.out.println("numManifolds =" + numManifolds);
for (int i=0;i<numManifolds;i++)
{
PersistentManifold contactManifold = collisionWorld.getDispatcher().getManifoldByIndexInternal(i);
System.out.println("ContactManifold =" + contactManifold);
CollisionObject obA = (CollisionObject)(contactManifold.getBody0());
CollisionObject obB = (CollisionObject)(contactManifold.getBody1());
contactManifold.refreshContactPoints(obA.getWorldTransform(new Transform()),obB.getWorldTransform(new Transform()));
int numContacts = contactManifold.getNumContacts();
System.out.println("numContacts =" + numContacts);
for (int j=0;j<numContacts;j++)
{
ManifoldPoint pt = contactManifold.getContactPoint(j);
System.out.println("Manifold pt=" + pt);
gl.glBegin(GL_LINES);
gl.glColor3f(1, 0, 1);
Vector3f ptA = pt.getPositionWorldOnA(new Vector3f());
Vector3f ptB = pt.getPositionWorldOnB(new Vector3f());
gl.glVertex3f(ptA.x,ptA.y,ptA.z);
gl.glVertex3f(ptB.x,ptB.y,ptB.z);
gl.glEnd();
}
//you can un-comment out this line, and then all points are removed
//contactManifold->clearManifold();
}
//GL_ShapeDrawer::drawCoordSystem();
for (int i=0;i<numObjects;i++){
GLShapeDrawer.drawOpenGL(gl,objects.getWorldTransform(new Transform()),objects.getCollisionShape(),new Vector3f(1,1,1),getDebugMode());
}
Quat4f orn = new Quat4f();
Transform3D t=new Transform3D();
t.setEuler(new Vector3d(new Vector3f(yaw,pitch,roll)));
t.set(orn);
Vector3f origin1 = objects[1].getWorldTransform(new Transform()).origin;
origin1.add(new Vector3f(0f,-0.01f,0f));
objects[1].getWorldTransform(new Transform()).transform(origin1);
objects[0].getWorldTransform(new Transform()).setRotation(orn);
pitch += 0.005f;
yaw += 0.01f;
//gl.glFlush();
//gl.glutSwapBuffers();
}
public void clientResetScene(){
objects[0].getWorldTransform(new Transform()).transform(new Vector3f(0.0f,3.f,0.f));
objects[1].getWorldTransform(new Transform()).transform(new Vector3f(0.0f,9.f,0.f));
}
}