Small Improvement for PhysicsSpace

Hello everyone,

long story short:
I had a Spatial with a RigidBodyControl and a GhostControl.
However, I could not correctly add the Spatial to the PhysicsSpace (using physicsSpace.add(spatial)), so I took a look at the code I found on Github:

    public void add(Object obj) {
        if (obj instanceof PhysicsControl) {
            ((PhysicsControl) obj).setPhysicsSpace(this);
        } else if (obj instanceof Spatial) {
            Spatial node = (Spatial) obj;
            PhysicsControl control = node.getControl(PhysicsControl.class);
            control.setPhysicsSpace(this);
        } else if (obj instanceof PhysicsCollisionObject) {
            addCollisionObject((PhysicsCollisionObject) obj);
        } else if (obj instanceof PhysicsJoint) {
            addJoint((PhysicsJoint) obj);
        } else {
            throw (new UnsupportedOperationException("Cannot add this kind of object to the physics space."));
        }
    }

I think it’d probably be a good idea to iterate through every Control of the Spatial and check if it’s instanceof PhysicsControl instead of only looking for one.
That’s it, should be a small change :slight_smile:

Cheers!

I decided to write the change too. Here you go:

[java]
for (int i=0; i<node.getNumControls(); i++) {
if (node.getControl(i) instanceof PhysicsControl) {
((PhysicsControl) node.getControl(i)).setPhysicsSpace(this);
}
}
[/java]

Edit: Don’t forget to modfiy the code in remove(…) as well :slight_smile:

Thanks but a pull request would be simpler… Don’t forget the native PhysicsSpace :slight_smile:

I’m looking at the PR, what do you need to implement it for native? @normen could you assist him?

@nehon
It’s simple: I didn’t find an ‘add(…)’ function that I could modify in there. :slight_smile:

Line 403:

@normen

Errr…
That was the thing I changed…

I fixed the .java PhysicsSpace, but not the native one, because I couldn’t find the method in the native one.

Then you’ll have to do it in the jme3-jbullet project as well (the java jbullet one), this is the native bullet implementation.

1 Like

@nehon

After I was away for some time, I finally figured everything out. The pull request is now complete.

I saw that someone else edited physics space in the meantime, so there are merge conflicts.
But it shouldn’t be too much of a problem since I only modified a couple lines. :slight_smile: