glLoadIdentity();
camera.applyTranslations();
dynamicsWorld.stepSimulation(1.0f / 60.0f);
Set<RigidBody> ballsToBeRemoved = new HashSet<RigidBody>();
for (RigidBody body : balls) {
Vector3f position = body.getMotionState().getWorldTransform(new Transform()).origin;
if (!body.equals(controlBall) && (position.x < -50 || position.x > 50 || position.z < -50 || position.z > 50)) {
ballsToBeRemoved.add(body);
}
}
for (RigidBody body : ballsToBeRemoved) {
balls.remove(body);
dynamicsWorld.removeRigidBody(body);
}
if (applyForce) {
Transform controlBallTransform = new Transform();
controlBall.getMotionState().getWorldTransform(controlBallTransform);
Vector3f controlBallLocation = controlBallTransform.origin;
Vector3f cameraPosition = new Vector3f(camera.x(), camera.y(), camera.z());
Vector3f force = new Vector3f();
force.sub(cameraPosition, controlBallLocation);
controlBall.activate(true);
controlBall.applyCentralForce(force);
}
if (createNewShape) {
CollisionShape shape = new SphereShape(3.0f);
DefaultMotionState motionState = new DefaultMotionState(new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), new Vector3f(camera.x(), 35, camera.z()), 1)));
Vector3f inertia = new Vector3f();
shape.calculateLocalInertia(1.0f, inertia);
RigidBodyConstructionInfo constructionInfo = new RigidBodyConstructionInfo(1.0f, motionState, shape, inertia);
constructionInfo.restitution = 0.75f;
RigidBody body = new RigidBody(constructionInfo);
dynamicsWorld.addRigidBody(body);
balls.add(body);
createNewShape = false;
}
if (resetControlBall) {
controlBall.setCenterOfMassTransform(DEFAULT_BALL_TRANSFORM);
controlBall.setAngularVelocity(new Vector3f(0, 0, 0));
controlBall.setLinearVelocity(new Vector3f(0, 0, 0));
resetControlBall = false;
}
}