Problem Integrating Physics and GameEntity/GameSystem

I’ve been trying to get the physics system Per and DarkProphet wrote working with the GameSystem DarkProphet created in the UserCode section. So far I’ve been unable to get even the simplest demo working and I have the feeling it’s something so simple I’ll never figure it out.

Right now I’m using a GameEntity for each spatial. The GameEntity includes a PhysicsObject that gets created when you create the Spatial. The following method gets called upon creation of a GameEntity.

   private void createPhysicsObject(){
      //if the spatial is not null we already have a PhysicsObject and need to remove it first
      if(spatialObj != null){
       System.out.println("PhysicsObject created for " + this.spatial.getName());
       spatialObj = new PhysicsObject((TriMesh)this.spatial);

I've set up my PhysicsWorld like so when I create a GameSystem

PhysicsWorld.getInstance().setGravity(new Vector3f(0,0,0));

Then in my main update loop I have

GameSystem.forceObtainInstance().getEntityWithName("my ship").getPhysicsObj().addForce(new Vector3f(10,0,0));
System.out.println(GameSystem.forceObtainInstance().getEntityWithName("my ship").getPhysicsObj().getLinearVelocity());

As you can see from the above code snippet I'm adding a force of 10 every update. This should make the ship I created go flying off the screen but unfortunately it doesn't move at all. The printout I have still prints out (0,0,0) for the linear velocity despite the force I manually added.

I know the GameEntity is being created, and the PhysicsObject exists otherwise I'd be getting errors. Furthermore I can get the Spatial from the GameEntity and rotate it using an InputHandler. It just seems for some reason that the force isn't being applied to the proper PhysicsObject or something.

Any ideas what I might be doing wrong?? Thank you for all the help.

yer, the solution to your problem is pretty easy…

spatialObj = new PhysicsObject((TriMesh)this.spatial);

This creates a Static object if you dont give it a mass. So just do this instead:

spatialObj = new PhysicsObject((TriMesh)this.spatial, 100);

This makes the object dynamic and dynamic objects respond to a change in Force.

Btw, this is changing in 0.4. We will have two classes, StaticPhysicsObject and DynamicPhysicsObject. This will stop people from doing this by accident.

Hope this solves the problem.



Thanks DarkProphet.