Hello,
i implemented collision detection between a box and a node of spheres in my game, but the app become very slow (10-15 FPS) when profiling i saw that there was a lot of memory allocation of Vector3f, here is the code that i use :
[java]Geometry g; // My Box
CollisionResults results = new CollisionResults();
BoundingVolume bv = g.getWorldBound();
node.collideWith(bv, results);
if (results.size() > 0) // Collision
else // No Collision[/java]
What is the best way to optimize the collision detection ?
Thank you for your help
- You can use jBullet for it. Is faster
- Show more code. Show your update function. Memory allocation rising means you create a lot of Vector3f’s just to throw them away.
@Dodikles said:
1. You can use jBullet for it. Is faster
2. Show more code. Show your update function. Memory allocation rising means you create a lot of Vector3f's just to throw them away.
I'll try jBullet.
Thank's for your answer, here is more code :
[java]
@Override
public void simpleUpdate(float tpf) {
movePlayer();
moveObstacle(tpf);
camNode.lookAt(geom1.getLocalTranslation(), Vector3f.UNIT_Y);
camNode.setLocalTranslation(camtranslation.setX(geom1.getLocalTranslation().x));
}
private void moveObstacle(float tpf) {
sphereGroup.getNode().rotate(0, 1f*tpf, 0);
sphereGroup2.getNode().rotate(0, -1f*tpf, 0);
CollisionResults results1 = new CollisionResults();
BoundingVolume bv = geom1.getWorldBound();
sphereGroup.getNode().collideWith(bv, results1);
if (results1.size() > 0) {
player.setPhysicsLocation(new Vector3f(0, 5f, 0));
player.setLinearVelocity(new Vector3f(0,0,0));
}
sphereGroup2.getNode().collideWith(bv, results1);
if (results1.size() > 0) {
player.setPhysicsLocation(new Vector3f(0, 5f, 0));
player.setLinearVelocity(new Vector3f(0,0,0));
}
}
private void movePlayer() {
speed = player.getLinearVelocity();
player.setAngularVelocity(Vector3f.ZERO);
if (geom1.getLocalTranslation().y >=-8.95f) {
// speed = new Vector3f(0,0,0); //geom1.getLocalTranslation();
if (left) { if(speed.x > -4f) speed.x -= 0.4f; }
if (right) { if(speed.x < 4f) speed.x += 0.4f; }
if (up) { if(speed.z > -4f) speed.z -= 0.4f; }
if (down) { if(speed.z < 4f) speed.z += 0.4f; }
if( left || right || up || down){
//player.setWalkDirection(new Vector3f(v.x,v.y,v.z));
//geom1.setLocalTranslation(v);
player.setLinearVelocity(speed);
}
}
}
[/java]
Here are the profiling for the CPU :
http://imageshack.us/photo/my-images/812/cpuq.png/
And for the memory :
http://imageshack.us/photo/my-images/40/memoryil.png/
Well, I cannot say if this is the culprit since I don’t know the amount of calls, but it could be: You create new Vector3f’s everytime you encounter collisions. If this happens a lot ( I mean a lot) then you create a bunch of useless objects on every update. Just define those two vectors outside somewhere and reference them instead of "new"ing…
Collision creates a lot of vectors but thats not exactly whats resource intense about it. You collide a bounding volume with geometry, if you need to do that often you should probably reside to kinematic rigidbodies and physics raytests and sweeptests.
Thank’s normen and Dodikles, Kinematic is working like a charm