Author Topic: Find an object at a 3D location  (Read 2259 times)

Offline nathan

  • byte
  • *
  • Posts: 15
    • View Profile
Find an object at a 3D location
« on: April 08, 2013, 02:09:56 am »
Suppose I have a cursor in 3D space and want to perform a click.  I want to know what object the cursor is intersecting.  Is there a way to ask the world what object is at X,Y,Z location?  The checkCollision functions only check collision with the polygons of an object, so it only detects collisions on entering/exiting the object.

The best solution I can come up with is to constantly check collision whenever the cursor moves,  and keep track of entering/exiting objects. 

However, this solution isn't perfect, and I can think of many ways that this will fail.  An obvious example is that the object moves far enough away, without the cursor moving / updating, and next time the cursor moves, it doesn't realize it has left the object.

Is there any way to get the bounding box of the object?  I know you can setBoundingBox, and calculationBoundingBox, but there is no getBoundingBox, as far as I can tell.

Offline nathan

  • byte
  • *
  • Posts: 15
    • View Profile
Re: Find an object at a 3D location
« Reply #1 on: April 08, 2013, 02:51:04 am »
Okay, I found that you can get the BoundingBox using

object.getMesh().getBoundingBox();

This solves my problem.

But just to confirm (it works now), I would do:

Code: [Select]
boolean doesIntersect;
SimpleVector cursorLocation = new SimpleVector(x,y,z);
SimpleVector centerOfObject = object.getTransformedCenter();
SimpleVector relativeLocation = new SimpleVector(cursorLocation);
relativeLocation.sub(centerOfObject);

float[] bb = object.getMesh().getBoundingBox();
if(relativeLocation.x < bb[0]) doesIntersect = false;
else if(relativeLocation.x > bb[1]) doesIntersect = false;
..
..
..
else doesIntersect = true;

Is there any other way?  And will object.getTransformedCenter() always be good?

What if there are child objects? Will this method still work?




Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12297
    • View Profile
    • http://www.jpct.net
Re: Find an object at a 3D location
« Reply #2 on: April 08, 2013, 07:26:11 am »
You are mixing object and world space with that approach. In this case, it might not matter that much, but it's better to use world space bounds anyway: http://www.jpct.net/wiki/index.php/Getting_Worldspace_Bounds

Offline nathan

  • byte
  • *
  • Posts: 15
    • View Profile
Re: Find an object at a 3D location
« Reply #3 on: April 09, 2013, 03:37:57 am »
Thank you, that will help.

But, what if the object has children?  I would have to do getWorldSpaceBounds() on every child, and their children recursively. 

For instance, I loaded a 3DS file of a car.  The wheels are different objects than the car body.  I then created a new empty Object3D and added these loaded Object3Ds as children to it.  However, of course, getWorldSpaceBounds() only works on the object itself, not its children.  How do I get the objects children?  There is no getChildren() method?

Alternatively (and I don't want to do this), is to check collision for every object (including the children) and then follow the parent trail up.  A problem here is an object can have multiple parents.

Another solution that I haven't tested, and I think will have other drawbacks, is to use mergeAll().  Will this remove animations / ability for the wheels to rotate?  It seems that it would merge the meshes so that getWorldSpaceBounds() would work.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12297
    • View Profile
    • http://www.jpct.net
Re: Find an object at a 3D location
« Reply #4 on: April 09, 2013, 08:17:43 am »
One solution would be to create an invisible bounding object (like a box) that includes all sub-objects and use that for your checks. I'm doing that for collisions too (which is why i usually call this approach "collision mesh") and prevent the player from climbing up NPCs for example...

Offline nathan

  • byte
  • *
  • Posts: 15
    • View Profile
Re: Find an object at a 3D location
« Reply #5 on: April 10, 2013, 10:57:18 pm »
Yes, that seems to work well. Thank you