[Solved] Class that extends RigidBodyControl and gravity issues

I honestly think I’m starting to lose my mind here.

So I made a nice class a bit ago that had the node and physics stuff wrapped up into a class that extends a node.

I wanted to make a split in that logic and have a node class that has the part you see and a physics class that handles ship movement. This seems logical to me but again I may have lost my mind.

Here’s the class as I have it now:

public class ShipPhysics extends RigidBodyControl implements PhysicsTickListener {

    private final Geometry colShape;
    
    ShipPhysics(Geometry collisionGeometry) {
        this.colShape = collisionGeometry;
        
        initPhysics();
    }
    
    private void initPhysics() {
        
        this.setCollisionShape(CollisionShapeFactory.createBoxShape(colShape));
        this.setMass(2f);
        this.setKinematic(false);
        this.setGravity(new Vector3f(0,0,0));
        this.setDamping(0.9f, 0.9f);
        this.setFriction(0.9f);
        
        
    }
    
    @Override
    public void update(float tpf) {
        System.out.println(this.getPhysicsLocation().toString());
    }

    @Override
    public void prePhysicsTick(PhysicsSpace space, float tpf) {
        System.out.println("Pre Tick");
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void physicsTick(PhysicsSpace space, float tpf) {
        System.out.println("Tick");
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

The class that calls this does this:

        shipPhy = new ShipPhysics(shipNode.getCollisionGeometry());
        
        bas.getPhysicsSpace().add(shipPhy);

bas is the BulletAppState and shipNode is the class that returns the ship geometry to use for the collision shape.

The issues that I’m having is… well… a few…

  1. Setting gravity in my class via this.setGravity or super.setGravity to a new Vector3f(0,0,0) doesn’t appear to change anything gravity wise. During the update loop the position just continues to fall endlessly.
  2. The pre and post ticks don’t tick.

I went through the tutorial regarding physics listeners but clearly I’ve either overlooked something or I’m thick (probably the later).

The global gravity of the physics space is set on single physics objects when they‘re added - a quirk in bullet. So you either have to add the physics object first and then set the gravity on the object or you have to set the physics space gravity before you add the object.

2 Likes

That did it. Now my physics isn’t falling through the floor. That is a bit of a weirdness with it I must say. Easy enough to fix though!

Thanks again!

1 Like

I think I mentioned it in the javadoc of the gravity methods. I agree though it‘s not something that‘d jump to my mind if I had issues.

1 Like

I’m glad you guys figured out the gravity of the situation.

2 Likes

/me slow claps.

1 Like