How to determine does a RigidBodyControl contain a point?

Good day!

I need to determine - does a point lay inside a RigidBodyControl?
I didn’t found any methods like: isPointInside(Vector 3f point) inside either

RigidBodyControl 

or

RigidBodyControl.getCollisionShape()

I used LibGDX before and I could determine the containment using:

    public boolean testPoint(Vector3f testPoint){
        var fixtures = body.getFixtureList();
        var testCoordinate2D = new Vector2(new Vector2(testPoint.x, testPoint.y));
        for (var fixture : fixtures){
            if (fixture.testPoint(testCoordinate2D)) return true;
        }
        return false;
    }

I write a wrapper for my videogame and want to implement the same method in my RigidBodyControlWrapper for JME3.

1 Like

What is it that you do with this information?

I think physics bodies need not be closed shapes so “inside” might be an interesting point of conversation in the general case.

1 Like

you can test if a point is inside the boundingbox of the spatial:

BoundingBox bbox = (BoundingBox)spatial.getWorldBound();
boolean inside = bbox.contains(point);

you can create a boundingbox for the rigidbody.

But if you need anything more precise using bullet, you might need to do a ray test.

It looks like you are working on a 2d game, i’ll assume z is the axis going from the camera forward into your sprites, something like this might work:

Vector2f point = new Vector2f(0.5f, 0.5f);
float spriteZ = 1f; // the z coordinate where your sprites live
float range = 0.4f; // the range to check (spriteZ +- range)

List<PhysicsRayTestResult> res = physicsSpace.rayTest(
    new Vector3f(point.x, spriteZ-range, point.y), // from 
    new Vector3f(point.x, spriteZ+range, point.y) // to
);


@RiccardoBlb hmm, I think the bounding box is enough for my game. Thanks!

Right now I try to write a partial wrapper (interface) to port a game from 2D to 3D (top down perspective).
I have investigated my code and found that I used this function to make not very accurate calculations. For example: when a chest is opened - it must throw its content to some free place in front of the chest. The chest asks the physical engine - are there some solid objects in front of the chest? When there are some walls - the chest starts to find free places along an increasing flat spiral around the theoretical content release place until it founds a free place.

That is why I don’t need to have accurate calculations and will use your advise - I will use bounding box. Thanks again!

Yes. Convex shapes have a well-defined “inside”, but compound and concave shapes do not.

If a collision object has a convex shape, then you can approximately test whether a particular point is inside it using CollisionSpace.pairTest() where the other collision object is very small and located at the point in question.

@sgold
You are right. In 2D I created solid bodies using simple geometric forms manually. If the physic representation is complex - it contains many single fixtures inside the body. There was an another way - create many bodies and weld-constrains between them.

In 3D this can be a problem, when I create automatically the collision body using the 3D-mesh of a complex object (for example - castle or tower). Either I need to divide complex shapes in single primitives manually or I will use double tests. At the first iteration I will use the BoundingBox test. And If it returns - “collisionExists” - I will cast a ray in the mesh in the target point.

1 Like