Hey,
Im in the middle of creating my first game using JME, so far it hasnt been to bad, but i have started Collision Detection over a week ago and basically im very lost with it.
My terrain contains hundreds of tree models, and i dont want to walk through them, so i have browsed through the forum to see if i could find anything useful and i came across this could here…
private void checkForCollision() {
boundCollision.clear();
character.calculateCollisions(collisionGeoms, boundCollision);
// check for bounding collision
if(boundCollision.getNumber() > 0) {
triCollision.clear();
character.calculateCollisions(collisionGeoms, triCollision);
// check for actual triangle collision (always true if bounds collision)
if(triCollision.getNumber() > 0) {
temp.zero();
for(int index = 0; index < triCollision.getNumber(); index++)
temp.addLocal(getCollisionNormal(reCalcCollision(triCollision.getCollisionData(index))));
if(temp != Vector3f.ZERO)
System.out.println("Collision Surface Normal: " + temp.normalize());
temp.normalizeLocal();
temp.multLocal(delta.length()); // try this instead if you still have probs : temp.multLocal(delta.length()).multLocal(1.05f);
temp.addLocal(delta);
character.setLocalTranslation(prevLoc.add(temp));
}
}
}
private Vector3f getCollisionNormal(CollisionData triData) {
Vector3f normal = new Vector3f();
if(triData.getTargetTris().size() > 0) {
TriMesh mesh = (TriMesh) triData.getTargetMesh();
Triangle[] triangles = mesh.getMeshAsTriangles(triData.getTargetBatchId(), null);
ArrayList<Integer> triIndex = triData.getTargetTris();
for(Integer i : triIndex) {
Triangle t = triangles;
t.calculateNormal();
normal.addLocal(triData.getTargetMesh().getLocalRotation().mult(t.getNormal()));
}
}
return normal.normalizeLocal();
}
private CollisionData reCalcCollision(CollisionData data) {
TriangleCollisionResults tcr = new TriangleCollisionResults();
data.getSourceMesh().calculateCollisions(data.getTargetMesh(), tcr);
return (tcr.getNumber() > 0 ? tcr.getCollisionData(0) : null);
}
i changed the checkForCollision() method around so when i call it in my update i will look like this, checkForCollision(character, terrain, prevLoc,delta), but the problem i encounter is that if i input the terrain node which includes the whole gaming environment, collisions are constantly the detected, but if i just input a node which contain all the tree models nothing happens, ie.checkForCollision(character, tree, prevLoc,delta), has anyone encountered this problem? and can u help me fix it?
any help would me much appreciated,
Thanks