www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: trisco on November 29, 2009, 11:29:37 pm

Title: Suggestion: calcmindistance
Post by: trisco on November 29, 2009, 11:29:37 pm
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)

Code: [Select]
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
Title: Re: Suggestion: calcmindistance
Post by: EgonOlsen on November 30, 2009, 07:49:23 am
I'll take a look...
Title: Re: Suggestion: calcmindistance
Post by: EgonOlsen on November 30, 2009, 01:42:02 pm
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.
Title: Re: Suggestion: calcmindistance
Post by: trisco on November 30, 2009, 03:18:16 pm
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 :).
Title: Re: Suggestion: calcmindistance
Post by: EgonOlsen on November 30, 2009, 08:30:29 pm
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 (http://www.jpct.net/download/beta/jpct.jar)
Title: Re: Suggestion: calcmindistance
Post by: trisco on December 05, 2009, 10:39:09 am
it works, thank you!

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