Suggestion: calcmindistance

Started by trisco, November 29, 2009, 11:29:37 PM

Previous topic - Next topic

trisco

Wouldn't it be easy if the code for calcmindistance would return the distance and the object related to that distance? That would make picking much easier (and less lines of code)

public float calcMinDistance(SimpleVector paramSimpleVector1, SimpleVector paramSimpleVector2, float paramFloat)
  {
    Object3D localObject3D = null;
    float f1 = 3.4028235E+38F;
    for (int i = 2; i < this.objectList.size(); ++i)
    {
      localObject3D = this.objectList.elementAt(i);
      if ((!(localObject3D.isPotentialCollider)) || ((!(localObject3D.isMainWorld)) && (localObject3D.oneSectorOnly) && (Config.useFastCollisionDetection) && (localObject3D.hasBoundingBox) && (localObject3D.rayIntersectsAABB(paramSimpleVector1, paramSimpleVector2, true) >= paramFloat)))
        continue;
      float f2 = localObject3D.calcMinDistance(paramSimpleVector1, paramSimpleVector2, paramFloat);
      if (f2 >= f1)
        continue;
      f1 = f2; <- save object
    }    if (f1 != 3.4028235E+38F)
      return f1; <- return array of object and distance
    return 1.0E+012F;
  }


just a suggestion ofcourse

EgonOlsen


EgonOlsen

I'll add that. The new method will return a [Float, Object3D] array of Object....not very elegant, but anyway...
In addition, there is a flaw with the CollisionEvent when used with the calcMinDistance-method in World, which caused the event to be fired for each object in the ray's way and not just the closest one. I'll fix this too.

trisco

ah ok, that was my main reason for the suggestion. Because now I execute calcmindistance, store all the objects I get an event on in a vector, loop through that vector and calculate the distance again to get the closest one. Result: the distance to objects had to be calculated twice (a nice bottle neck for larger scenes), hence the suggestion :).

EgonOlsen

Here's a jar that contains the new method (calcMinDistanceAndObject3D()...yeah, stupid name) as well as the fix for the multiple CollisionEvents. The new method returns an Object[2] with the first value being a Float with the distance and the second one the Object3D (i didn't want to create a container class for this, so i'm using the rather ugly Object[]-thingy). If the second one is null and the first one is Object3D.COLLISION_NONE, there was no collision.

Please let me know if it works, i haven't tested it thoroughly: http://www.jpct.net/download/beta/jpct.jar

trisco

it works, thank you!

PS: sorry for the late response, short break from work the last week ;)