Hello all. I have a been working on a collision detection algo and I think I’ve got something wrong. I have been going off of the tutorial from j3d.org with some modifications.
Here is the code for detecting if a collision happened.
curTrans is the current position of the object.
nextTrans is the position we are checking for a collision with. Meaning, if we move the object to this position, will there be a collision.
public void detect(Transform3D curTrans, Transform3D nextTrans){
curTrans.get(locationVector);
nextTrans.get(collisionVector);
locationPoint.set(locationVector);
locationEndPoint.set(collisionVector);
cylinderPicker.set(locationPoint, locationEndPoint,0.01);
SceneGraphPath[] closest = collidables.pickAllSorted(cylinderPicker);
if((closest == null) || (closest.length == 0)){
// break out b/c we didn't hit anything (code left out for simplicity)
}
length = (float)collisionVector.length();
for(int i = 0; (i < closest.length); i++){
Transform3D local_tx = closest[i].getTransform();
Shape3D i_shape = (Shape3D)closest[i].getObject();
PickResult pr = new PickResult(i_shape,local_tx,cylinderPicker);
PickIntersection pis = pr.getClosestIntersection(locationPoint);
if(pis != null){
//we found a collision so we can stop for now
// notify engine code goes here (left out for simplicity)
}
}
}
This is the code that calls the function.
We basically need to get the world coordinates, so thats what most of this code does.
TransformGroup currentTransG; // holds the current transform information
Transform3D nextTrans; // Is the next position for the object to travel to
// these variables get set along the way
// get the World Coordinates of the current position and the next position
Transform3D t1 = new Transform3D();
currentTransG.getLocalToVworld(t1);
Transform3D tempT = new Transform3D();
currentTransG.getTransform(tempT);
t1.mul(tempT);
Transform3D t2 = new Transform3D();
currentTransG.getLocalToVworld(t2);
currentTransG.mul(nextTrans);
// detect if there was a collision
detect(t1,t2);
Anyone have any suggestions on what I need to fix?
Thanks