Checking for point within a cube

I have a unit cube that’s applied to as a mesh and the mesh is scaled and rotated to ultimate size. So the mesh data is is “valid” as a target to use for the test.

I want to determine if the Vector3 is within that cubeoid… the cube can be rotated arbitrarily and I can’t rely on colision events I just need to check “is Vector3 inside this rotate cube.”

Is there a babylonjs utility function for such a thing… also if not … why not you slackers !!! :stuck_out_tongue: (/s)

Ultimately I have the rotations/dimentions/center/pivot of the cube data in another structure, I just didn’t want to have to manually math that one out…

EDIT: I was trying to use mesh.getBoundingInfo() and intersectsPoint() - but this seems to not apply rotations to the test - and only uses min/max test. I noticed there’s a intersects() functions that takes a second bounding info and a precision mode… so I’m going to see if I can make a bounding info for a single point (doesn’t matter if it’s tiny cube to me)

Pinging @belfortk

Going to add a bit more here, ultimately I’m making a game editing tool, and I’m working on /experimenting with our implementation of a skybox using babylonjs. I want to detect when the camera (or player) is intersecting with a bounding box, that the player/editor had designated with a “skybox” within our framework.

I have naive bounds checking working, so when the player enters a bounding area, that cube gets “promoted” to the skybox by setting infiniteDistance and turning off lighting for it etc… so far that bit works. This way “in theory” I can have multiple (non overlapping) skyboxes. in an area for an intersting effect.

Once I get some help on the vector3 bounds detection, I can address the next one.

One more “funny bit” when outisde my two skyboxes, I can see the cubes as distinct cubes as one would expect - Then I enter one… and I can see the other’s plain “skybox cube” rendered in the distance… lol can’t win them all… why didn’t I realize this would happen. Anyway, I can detect the player entering into each bounding box with the naive unrotated bit… but is there a way to limit rendering to “everything inside this box only?”

I can post a different question if necessary

I’m sorry I’m having a hard time following your questions. Could you provide playground for more context?

You have a cube mesh and you want to tell if a vector3 is inside or intersects with the mesh? Is that correct?

I created this scene in the playground.

https://playground.babylonjs.com/#U2DD8T#46

There is a set of cubes rotated arbitrarily, black, cyan, green and blue. A light, and a tiny sphere.

The light is moving around based on a circular path defined in the before render event.

The light and sphere are here - only to show feedback, these do not exist in my real example.

For this rig I used mesh collision to show an example, that fails the “test” but portrays the problem domain somewhat.

I would like to know when a Vector3 is within those cubes. For that purpose I defined a function at the top of the page if that function was uncommented and its contract fulfilled returning truthy on a vector3 collision and a mesh, then the test would run similarly, except the blue mesh should never get highlighted

a successful run is all meshes except for the blue mesh get highlighted.

(I think the blue mesh is low enough in the example that mesh collisions detect it, but point collisions would not)

P.S. I only care about cubes, arbitrary meshes are not important. The reason I mentioned “unit cube” and scale - is because the dimensions of the scale are the dimensions of my bounding box.

If you translate the point into the unit cube’s local coordinates, you just have to check if the point’s absolute x, y, and z values are < (or <=) than 0.5:

https://playground.babylonjs.com/#U2DD8T#48

Or with non-unit cubes:

https://playground.babylonjs.com/#U2DD8T#49

1 Like

@Gijs thanks! I wasn’t sure how to do the rotations/scaleing of the point in BJS’s vector library, didn’t know about the world matrix or invert - that all makes sense.

I tried to fix it to work with scaling too… this seems to be the right mechanism to do it, but it didn’t work right? It’s almost like it’s testing the wrong dimensions… wierd.

https://playground.babylonjs.com/#U2DD8T#59

This would solve the entirety of my question.

EDIT: I think I see whats happening here, the scaling affects the world matrix, and I’m converting the point into scaled space…

Got it … okay then the unit cube solutions works for me

https://playground.babylonjs.com/#U2DD8T#61

with scaling, the cube’s space is still a “unit cube”

1 Like