Best way to implement targetting crosshair

I'm trying to wrap my mind around how might be the best way to implement this.

I'm writing a space shooter and would like to implement a targetting system that displays the following behaviors. It should extend a ray, or ray-like projection (I wouldn't be averse to a very narrow cylinder/box) from the nose of the ship. If any part of a target intersects/collides with this ray and is greater than 15 units away then a low, slow tone should sound. If a target is 15 or fewer units away then a higher, faster tone should sound. The system only needs to report the closest target, and the ray can stop at that point, though it wouldn't really be a problem to note that the ray intersects two targets and behave appropriately for whichever is closer.

That being said, what would be the best way of implementing something like this? I'm still learning about the depths of JME; are there any utilities buried somewhere that might help? I'm using JMEphysics for collision detection, so I'm looking for something more lightweight that I can add to only check one non-physical geometry, not something huge that would require lots more effort.

Also, along similar lines, I'd like to write a method that computes the distance between two game entities while taking into account their sizes. For instance, if two spheres with a radius of 1 are 10 units apart then I'd like to report their distance as 8, since any closer would have them colliding. These distances can be approximated to use bounding volumes if that would be simpler, but I'd rather strive for something generic that doesn't need special hacks/huge if statements for each possible geometry. Is this possible? Does something already exist for this?

If you want some quick examples you can check out jmetest.TutorialGuide.HelloMousePick, this shows how to use the Ray, and PickResults objects to find objects intersecting a given Ray (you can define one to be a line coming from your ships gun/nose), in the scene graph from a given Node (usually rootNode).

Here's an example of finding a pick between the mouse and a spatial and taking an action if the distance from object to ray origin is >100.

Oh, cool, thanks for the examples. I ended up getting something to work that tests a ray's intersection with all game entities, but I wasn't sure if the collision detection checkers could be used on a smaller scale if I wanted to avoid doing two checks/tick. :slight_smile: Think I'll be refactoring to this.

Also, in case anyone else has a similar issue, I think I found a less verbose way of calculating more accurate distances between game entities. This is from my engine's Entity class, but it should be easy enough to follow:

   public float distanceTo(Entity entity) {
      float centerDistance = getPosition().distance(entity.getPosition());
      BoundingVolume myBound = getSpatial().getWorldBound();
      BoundingVolume tBound = entity.getSpatial().getWorldBound();
      float myDistanceFromEdge = myBound.distanceToEdge(tBound.getCenter());
      float tDistanceFromEdge = tBound.distanceToEdge(myBound.getCenter());
      return centerDistance-(centerDistance-myDistanceFromEdge)-(centerDistance-tDistanceFromEdge);

Basically, calculate the distance in the normal way, then determine how much of that distance is distance between centers and edges, subtracting those figures from the center distance to obtain a distance between nearest edges. Anyone see anything wrong with this method?

Cool, I like the way you did it definetly alot cleaner and less verbose, plus less vector calculations.