Raycast collisiion only detected once instead of every frame

I believe what I’m trying to accomplish is a much more watered down version of how a car’s wheels are managed by four rays cast down to the terrain.

I’m trying to have my obstacles cast a ray below it to detect the surface, and to keep casting every frame and check how far it is from the surface as well as what this new contact point is compared to the previous one.

However, my raycast collision only picks up the first one or two collisions and then says no collisions are occurring any more.

HERE is a video to demonstrate the issue.

The code I use is here:

Vector3f up_dir = Vector3f.UNIT_Y.mult(-1).clone();
//spatial.worldToLocal(Vector3f.UNIT_Y, up_dir); 
    
Ray up_ray = new Ray(spatial.getLocalTranslation(),up_dir);

CollisionResults up_collisions = new CollisionResults();
    
track.collideWith(up_ray,up_collisions);
    
if(up_collisions.size() > 0){
    CollisionResult up_closest =  up_collisions.getClosestCollision();

    Geometry m = mark.clone()
    track.getParent().attachChild(m);
    m.setLocalTranslation(spatial.localToWorld(up_closest.getContactPoint(),null));

    System.out.println("Up dist: " + up_closest.getDistance() + up_closest.getGeometry().getName());

       
}

if(up_collisions.size() == 0) {
    System.out.println("No up collisions");
}

I’m sure there is some underlying physics problem I’m not understanding. Or if I can do this in another way, do tell. This is the first way that came to my mind.

The logs show initial collisions on the tubes, and the no collisions. As the video demonstrates, there is that first registered mark, and then no more.

Any thoughts as to why?

Regardless of anything else going on, this will be a problem because contact points should already be in world position as I recall.

Edit: not to mention that ‘spatial’ is undefined in our view and the mark is attached to a track.getParent()… which if it has its own translation or rotation then you’ll need to call worldToLocal for that anyway.

I should have clarified more, apologies.

I wasn’t sure about the contact point reference, but that makes sense.

This is in a control, so spatial is the control’s spatial.

track.getParent() is the rootNode. and I haven’t manipulated the root in
anyway.

Barring all that though, that whole if statement never gets entered again.
So it seems like the collisiononly occurs once, somehow.

EDIT:

Regardless of anything else going on, this will be a problem because
contact points should already be in world position as I recall.

So I logged the position of each contact, from stdout print in the code above, and all I get in the logs are

Up dist: 17.585117 (0.0, -9.585117, 0.0) tube_straight
Up dist: 17.585117 (0.0, -9.585117, 0.0) tube_straight
Up dist: 17.585117 (0.0, -9.585117, 0.0) tube_straight
Up dist: 17.585117 (0.0, -9.585117, 0.0) tube_straight
Up dist: 17.585117 (0.0, -9.585117, 0.0) tube_straight
Up dist: 17.585117 (0.0, -9.585117, 0.0) tube_straight
Up dist: 17.585117 (0.0, -9.585117, 0.0) tube_straight

Which is why I thought the contacts must be local, How they are defining the zero point confuses me too, but I’m still digging.

Well, what is the world position of tube_straight?

Note that you are also only casting your ray in local space:

Maybe your spatial is only a child of the root node, but still. You might as well have the code say what it does and getWorldTranslation() instead.

That made everything work hunky dory, @pspeed. That also improved my understanding of how to use the ray properly, thank you.

I always feel so happy coming to these forums because you guys solve problems so quickly, yet try not to spoonfeed. You da best :fist: :monkey_face:

1 Like