Help performing raycast collision detection (failing to hit object)

I’m having a problem properly raycasting for collision detection. However, if you believe there is another way to tackle this problem, please let me know. I’ve been mulling over this for a while now, so I’m open to suggestions.

This pictures shows the problem:

Obstacles are being randomly generated along the track, and I need to know when I can delete them. I would like to delete them when the player passes the obstacle without being hurt by it, but i don’t see any easy way to do “pass” detection. When the player finishes a certain piece of the track, that track piece is deleted. So my though was, why don’t I just cast a ray ever few seconds and have the obstacle check if it is still within a track piece. If the collision occurs, then keep the obstacle. If the result is null, or the collision is really far away, then delete the obstacle.

Problem: The raycast reports null collisions whenever it is cast, inside the track or not. The purple line demonstrates the raycast direction

Code:

    if(poll <= 0){
            
            CollisionResults results_piece = new CollisionResults();
            Ray ray_piece = new Ray(this.spatial.getLocalTranslation(),this.spatial.getLocalTranslation().add(0, 10, 0).normalize());
            Line line = new Line(this.spatial.getLocalTranslation(),this.spatial.getLocalTranslation().add(0, 10, 0).normalize());
            line.setLineWidth(2);
            Geometry geometry = new Geometry("Bullet", line);
            Material orange = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
            orange.setColor("Color", ColorRGBA.Magenta);
            geometry.setMaterial(orange);                  
            spatial.getParent().attachChild(geometry);
            if(track != null){
                track.collideWith(ray_piece, results_piece);
                System.out.println("Size: " + track.getChildren());
                System.out.println("Before: " + this.getObjectId() + " " + results_piece.getClosestCollision());
                if(results_piece.getClosestCollision() == null){
                    //destroyObstacle();
                    poll = Float.MAX_VALUE;
                } else {
                    poll = 1;
                }
                
            }
        } else {
            

            poll -= tpf;
        }

Track is updating properly, since it is changing fchildren. However, before always reports the id of the controller, and then null. So ever obstacle gets delete (I’ve commented out the destroyObstacle).

Anyone have a clue as to what I’m missing. Or perhaps another approach to it?

Not sure about your original question but I’m wondering how you are placing the obstacles in the first place and why they aren’t children of the track section or something. Then they’d get automatically deleted.

…I mean, they must know which section they are in or how would they know where to be placed?

Yeah, so I’m free to discuss that too. My problem is I want my obstacle system to scale. Later, if I include rockets as an obstacle that move along the track, then it isn’t quite associated with track piece. So how do I effectively delete that. And these pieces are randomly placed, so I don’t know how I’d associate it with the track piece.

How are you placing them on the track? Why do they not just float randomly in space?

Ahh, well, OOOOH. I have an idea—hold on