In my world there are only one StaticPlaneShape and many SphereShape which i created in any time by pressing 'f' key.
At some point StaticPlaneShape starts to behaving strangely. It just moving with some Spheres, you could watch the video below.
http://youtu.be/cUpEYoh9h0w
And one more problem.
Why all Spheres does not falling down? Instead of it, they are balancing on eachother. And only when I move the lower Sphere, they are doing something like fall. But actually they does not moves in Z direction, only X and Y.
So why it is?
Here is main parts of code.
Code: Select all
public class Main implements GLEventListener {
private static DynamicsWorld dynamicsWorld;
private static Set<RigidBody> balls = new HashSet<RigidBody>();
private static RigidBody groundRigidBody,ballRigidBody;
private static Transform groundTransform;
private static MotionState ballMotionState;
private static CollisionShape ballShape;
private static void setUpPhysics()
{
BroadphaseInterface broadphase = new DbvtBroadphase();
CollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
CollisionDispatcher dispatcher = new CollisionDispatcher(collisionConfiguration);
ConstraintSolver solver = new SequentialImpulseConstraintSolver();
dynamicsWorld=new DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
dynamicsWorld.setGravity(new Vector3f(0,-9.81f,0));
CollisionShape groundShape = new StaticPlaneShape(new Vector3f(0,1,0),0.25f);
groundShape.setLocalScaling(new Vector3f(1,1,1));
ballShape = new SphereShape(1);
groundTransform=new Transform(new Matrix4f(new Quat4f(0,0,0.0f,1),new Vector3f(0,0,0),1.0f));
MotionState groundMotionState = new DefaultMotionState();
RigidBodyConstructionInfo groundBodyConstructionInfo = new RigidBodyConstructionInfo(0, groundMotionState, groundShape, new Vector3f(0,0,0));
groundBodyConstructionInfo.restitution=0.25f;
groundRigidBody = new RigidBody(groundBodyConstructionInfo);
groundRigidBody.setCollisionFlags(groundRigidBody.getCollisionFlags() | CollisionFlags.KINEMATIC_OBJECT);
dynamicsWorld.addRigidBody(groundRigidBody);
ballShape = new SphereShape(1);
ballMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0,0,0,1),new Vector3f(0,40,0),1.0f)));
RigidBodyConstructionInfo ballBodyConstructionInfo = new RigidBodyConstructionInfo(1,ballMotionState,ballShape, new Vector3f(0,0,0));
ballShape.calculateLocalInertia(1.0f,new Vector3f(0,0,0));
ballBodyConstructionInfo.restitution=0.5f;
ballBodyConstructionInfo.angularDamping=0.95f;
ballRigidBody = new RigidBody(ballBodyConstructionInfo);
balls.add(ballRigidBody);
dynamicsWorld.addRigidBody(ballRigidBody);
BulletGlobals.setContactAddedCallback(new ContactAddedCallback() {
@Override
public boolean contactAdded(ManifoldPoint manifoldPoint, CollisionObject collisionObject, int i, int i2,
CollisionObject collisionObject2, int i3, int i4) {
System.out.println("collide!");
return false;
}
});
}
private static void AddBall()
{
SphereShape ballShape = new SphereShape(1);
MotionState ballMotionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0,0,0,1),new Vector3f(0,40,0),1.0f)));
RigidBodyConstructionInfo ballBodyConstructionInfo = new RigidBodyConstructionInfo(1,ballMotionState,ballShape, new Vector3f(0,0,0));
ballShape.calculateLocalInertia(1.0f,new Vector3f(0,0,0));
ballBodyConstructionInfo.restitution=0.5f;
ballBodyConstructionInfo.angularDamping=0.95f;
RigidBody ballRigidBody = new RigidBody(ballBodyConstructionInfo);
ballRigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);
balls.add(ballRigidBody);
dynamicsWorld.addRigidBody(ballRigidBody);
}
public static void StartGL()
{
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
final GLCanvas canvas = new GLCanvas(caps);
Frame frame = new Frame("AWT Window Test");
frame.setSize(1024, 720);
frame.add(canvas);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
canvas.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyChar()) {
case ']' :
Transform transform=new Transform();
ballMotionState.getWorldTransform(transform);
transform.origin.setX(transform.origin.x-1);
ballMotionState.setWorldTransform(transform);
ballRigidBody.setMotionState(ballMotionState);
break;
case '[' :
Transform transform1=new Transform();
ballMotionState.getWorldTransform(transform1);
transform1.origin.setX(transform1.origin.x+1);
ballMotionState.setWorldTransform(transform1);
ballRigidBody.setMotionState(ballMotionState);
break;
case 'f' :
AddBall();
}
}
});
canvas.addGLEventListener(new Main());
FPSAnimator animator = new FPSAnimator(canvas, 60);
animator.start();
}
public static void main(String[] args) throws Exception{
setUpPhysics();
StartGL();
}
@Override
public void display(GLAutoDrawable glAutoDrawable) {
update();
render(glAutoDrawable);
}
private void render(GLAutoDrawable drawable)
{
gl.glEnable(gl.GL_DEPTH_TEST);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0.2f, 0.4f, 0.7f, 0);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(cameraPosX, cameraPosY, cameraPosZ, cameraRotX, cameraRotY, cameraRotZ, 0, 1, 0);
for(int i =0; i<balls.size()+1; i++)
{
gl.glPushMatrix();
}
int x=0;
for(RigidBody ball : balls)
{
gl.glPopMatrix();
Vector3f ballPosition = ball.getWorldTransform(new Transform()).origin;
gl.glTranslatef(ballPosition.x,ballPosition.y,ballPosition.z);
gl.glColor3f(0, 1, 0);
glut.glutSolidSphere(1,10,10);
x++;
}
gl.glColor3f(0.5f,0,0);
gl.glPopMatrix();
gl.glBegin(gl.GL_POLYGON);
gl.glVertex3f(-50,0,-50);
gl.glVertex3f(-50,0,50);
gl.glVertex3f(50,0,50);
gl.glVertex3f(50,0,-50);
gl.glEnd();
}
private void update()
{
dynamicsWorld.stepSimulation(1.0f / 60.0f);
}
}