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?