[SOLVED] Collision Detection Problems

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

Assuming we’re too lazy to build and implement your code, what are the symptoms of the problem?

Check out the attachment.
Basically, when I shoot the lasers, the detection algo is indicating that they hit in-between the balls. It also picks up that it hits the balls too. In the image, collisions are indicated by the swerly round things and the laser is the cylindrical red thing. When I shoot away from the balls, the lasers fly as they should and don’t hit anything.

Let me know if you need more info. Thanks!!!

That makes things a lot clearer…

I’m not really a picking expert, but I would make a bit of a guess that it could be picking based on bounds rather than polygon collisions?

Try changing the appearance of the object being hit when it is hit- that way you can tell what shape Java3D thinks you are hitting when you think you are shooting in between them.

Yea, that would make sense except that my algorithm makes sure that we really hit a shape3d object. Unless something is wrong with the code below…
Also, what do u mean by change the apperance?

Thanks


   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)
        }
    }

Make sure you are setting it to pick by triangle data

Also make sure your Shape3Ds are set to allow such a pick

Just hint, here is an example how FlyingGuns uses picking to detect cannon hits (look for method evalCannonHits()):

http://cvs.sourceforge.net/viewcvs.py/drts/projects/flyingguns/core/client/java/com/flyingguns/scene/Vehicle.java?view=markup

So I found this in the collisions function.


mShotAnalysis.setup( m3DModel.getTransformGroup() );
mShotAnalysis.mPicker.setShapeCylinderRay( mShotAnalysis.mCenter, mShotAnalysis.mAhead, 0.5 );

PickResult result = mShotAnalysis.mPicker.pickClosest();

The problem is, I can’t find Vehicle.shotanalyis which is what mShotAnalysis is.
Thats where the meat of the collision detection takes place. I use the PickResult the same way those guys use it.
So I am thinknig however they first get the bounds of their collisions is where they differ for me.

But I can’t tell that until I get at their code.

Scroll a bit upwards :slight_smile:

(I admit that Vehicle.java is a bit messy … refactoring is planned)

Hey.
Just wanted to let everyone know that I got collision detection working on my lasers as well as running into stuff.
From looking through Flying Guns, I was able to get the following routine to work. Thanks a bunch. Now I just need to figure out how to better deal with collisions from the ship that you are flying – sometime you get stuck which is bad bad bad.


        curTrans.get(locationVector);
        nextTrans.get(collisionVector);

        locationPoint.set(locationVector);
        locationEndPoint.set(collisionVector);

        cylinderPicker.set(locationPoint, locationEndPoint,0.5);
        PickTool mPicker = new PickTool(collidables);
        mPicker.setMode( PickTool.GEOMETRY_INTERSECT_INFO );
        mPicker.setShapeCylinderSegment(locationPoint,locationEndPoint,0.05);
        PickResult result = mPicker.pickClosest();
        if(result == null) // we didn't hit anything
        // else we hit something so do hit routine