# [Solved] Finding out how deep spatial a is in spatial b

I am building a drag and drop space ship builder. You build ships with ship parts. I am using collision shapes and bullet to detect physics collisions so that I know if the parts are attached or not. My problem is that when they are colliding and part a (shown in green in the picture) is moved into part b, I need to know how deep part a is in part b because I want to restrict how much they can overlap.

Anyone know how I can get this information to restrict transform movement?

EDIT: The answer must be shape and rotation accurate. I am using a HullCollisionShape

3 Likes

You mean āmoved into Bā in the sense that it is no longer attaching to the surface but is just āfree formā in space.

It sounds like you want to know how much two arbitrary volumes intersectā¦ which I think is a really non-trivial problem.

Yes and yes ā¦ BUT what I really want to know ā¦ since the result has to be shape and rotation acurate is ā¦ based in the picture ā¦ for example, is the cockpit deepest spot in the hull more than 10 percent of the cockpitās volume ā¦ but accurate volume. It might be trivial but my noob brain canāt get my head around the math.

No, it is NOT trivial.

Probably your best bet will be to voxelize the volumes and even that is going to be hard for arbitrary non-convex shapes.

Edit: but here you can think about how hard the problem is. Given just a point in space, how would you tell if it was inside or outside the volume?

Solve that and you can voxelize both shapes on some grid and see how much they overlap.

I think I figured it out by just staring at my picture. Please shoot this theory to hell is it will not work.

Basically I do have the ORIGINAL collision point saved with the direction of object b from the physics engine so ā¦ for each axis, I send a ray down both directions of the axis and each ray will collide with the exterior of object A since the collision point will be inside object A. I then can divide the distance of the collision point from ray 1 and ray 2 with the distance of the ray pointing towards object b and presto magico I now know how deep each axis is inside object B ā¦ if I have the distance of each axis I can derive at least a basic cube volume.

Itās not super actuate but should be accurate enough so that I can configure the cockpit part to prevent the user from sticking the cockpit so far up the hull the you canāt even see the windows.

I got a much easier suggestion for youā¦
What about using 2 collision shapes?
The first one: bigger one - is used just to detect stuff and can overlap
The second one: smaller one - is used just to ensure things dont pass thru.

1 Like

Holly crapnutz batman!!! Why did I not think of that??
Never did that before though ā¦ Can we add two rigid body controls to a single spatial??

2 Likes

Probably best avoidedā¦ but you could add whatever ghost objects you want to.

1 Like

Ghost objects are AABB so they are of no use to me

? Weird, I use spheres and capsules for them all the time. Not sure why you think they are limited to a bounding box.

ā¦but anyway, you can make fake ghosts if you want by having rigid bodies that arenāt controls and just position them where you like.

Nothing requires you to use controls and spatials.

As the javadoc for PhysicsObjectControl says, the ghost object collision detection uses AABB (Axis-aligned bounding box) collision only, regardless of the collision shape it has been assigned.

Hm. Maybe it was because I was using jbullet.

Anyway, you can have whatever physics objects you want to without any visuals at all.

1 Like

Yes I believe itās a native bullet limitation

Ghost objects can use any collision shape, not just AABB. Iām very sure of this for native Bullet and reasonably sure for jBullet.

Bullet uses AABB for broadphase collision detection, to find candidate pairs of objects for collisions. However, thereās a 2nd phase in the collision-detection process, one that uses the actual collision shapes. The exact algorithm thatās dispatched depends on the shapes involved. All this is documented in Chapter 4 of the Bullet manual:
bullet3/Bullet_User_Manual.pdf at master Ā· bulletphysics/bullet3 Ā· GitHub

When Bullet detects a collision, it calculates the depth of penetration. Unfortunately, this information doesnāt seem to be accessible through the current `jme3-bullet` API.

So out of curiosity, if I reduce a volume by x% whatās the reduced surface area % ? Iāve never come across a need for that but my brain wants to know. Sounds exponential.

Are you actually reducing the volume by x% or the dimensions (ie: scale)?

Surface area is proportional to the 2/3 power of the volume. Reducing the volume x% gives you

`````` newVolume = (1f-x/100f)*originalVolume;
newArea = Math.pow(1f-x/100f, 2/3f)*originalArea;
``````
2 Likes

Reason being that change in surface area is the square of the change in āsizeāā¦ and change in volume is the cube of the change in size.

ā¦which is why I asked what the original x% was. Usually people think āI want to make this half the sizeāā¦ which is a function of size and not volume.

In graphics, anywayā¦ itās super weird to change somethingās size based on its volume.

1 Like

Yes, you can add 2 rigid-body controls to a single `Spatial`. To access them, youād need to keep references (since `Spatial.getControl()` will only find one of them). And of course, if both RBCs were in dynamic mode, theyād interfere with each other, so I recommend that at least one of the controls be `setKinematic(true)`.

In this case, however, I think a `GhostControl` for the outer envelope makes more sense than an RBC.

1 Like

I was thinking if I altered the scale. So a 10% reduction in size would result in a volume reduction % of x

Itās more of a brain excercise than anything else, but I guess if the OP decreased the size by 10% or whatever then that would result in 10% wiggle room they desire.