jME Physics 2 addForce() and character controls

I'm modeling my game character as a simple cube, and am moving him with calls to setLinearVelocity(), and turning only the geometry with setLocalRotation() (so the physics cube just dangles around the Y axis however it pleases). To avoid the cube from falling over, every update cycle I'm doing



                Quaternion rotcur = avatarPhysics.getLocalRotation();
      float curang = rotcur.toAngleAxis(rotAxis);
      rotAxis.z = 0; rotAxis.x = 0;
      rotcur.fromAngleAxis(curang, rotAxis);



I wanted to try and have the box behave like a stretched string, so that it would remain vertical naturally. To do this, I wanted to add a vertical force at each end of the box, something like this:


      final Vector3f upForce = new Vector3f(0,100,0);
      final Vector3f downForce = new Vector3f(0,-100,0);
      final Vector3f to1 = new Vector3f(0,50,0);
      final Vector3f to2 = new Vector3f(0,-50,0);
      PhysicsUpdateCallback forceAdder = new PhysicsUpdateCallback() {
         public void afterStep(PhysicsSpace space, float time){            
            
            avatarPhysics.addForce(upForce, to1);
            avatarPhysics.addForce(downForce, to2);
         }
         public void beforeStep(PhysicsSpace space, float time){
         }         
      };      
      physicsSpace.addToUpdateCallbacks(forceAdder);



However, the addForce(force, to) call seems to do the same thing as the addForce(force) call - applies the force to the center of mass. The call apparently goes down to ODE's native call, so I wonder if I'm doing something wrong.

So I was hoping I might be able to get some help on this. Any suggestions would be very welcome.

Here's a small working example using addForce(force,to) which can help check what I'm talking about:




import com.jme.app.SimpleGame;
import com.jme.input.KeyBindingManager;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;
import com.jme.system.DisplaySystem;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsDebugger;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.PhysicsUpdateCallback;
import com.jmex.physics.geometry.PhysicsBox;

public class TestApplyForce extends SimpleGame
{
   PhysicsSpace p;
   boolean debugPhysics = true;
   DynamicPhysicsNode avatar;
   @Override
   protected void simpleInitGame()
   {
      p = PhysicsSpace.create();
      rootNode.attachChild(new Box("My box", Vector3f.ZERO, 10,10, 10));
      
      final DynamicPhysicsNode avatarPhysics = p.createDynamicNode();
      avatar = avatarPhysics;
      avatarPhysics.setAffectedByGravity(false);
      PhysicsBox mybox = avatarPhysics.createBox("my physics box");
      mybox.setLocalScale(20);
      
      
      
      final Vector3f upForce = new Vector3f(0,40,0);
      final Vector3f downForce = new Vector3f(0,-40,0);
      final Vector3f to1 = new Vector3f(0,5,0);
      final Vector3f to2 = new Vector3f(0,-5,0);
      
      PhysicsUpdateCallback forceAdder = new PhysicsUpdateCallback() {
         public void afterStep(PhysicsSpace space, float time){            
            avatarPhysics.addForce(upForce, to1);
            avatarPhysics.addForce(downForce, to2);
         }
         public void beforeStep(PhysicsSpace space, float time){
            
         }         
      };      
      p.addToUpdateCallbacks(forceAdder);

   }
   
   public void simpleUpdate()
   {
      if(KeyBindingManager.getKeyBindingManager().isValidCommand("Debug Physics", false))     debugPhysics = !debugPhysics;
      p.update(tpf);   
   }
   
   public void simpleRender()
   {
      if(debugPhysics) PhysicsDebugger.drawPhysics(p, DisplaySystem.getDisplaySystem().getRenderer());
   }
   
   public static void main(String[] args)
   {
      TestApplyForce tester = new TestApplyForce();
      tester.start();      
   }   
}

i tried to play a bit with the code, but i dodnt get anything usable out of it.

Moving a character with phyiscs seems very hard.

Maybe there are some pure ODE tutorials on that topic which could be convertet to jmephyiscs, i don't know.

I am not sure about your 'stretched string' approach, but maybe this thread will help:

http://www.jmonkeyengine.com/jmeforum/index.php?topic=9945.0

Hi all (new guy).  :mrgreen:



I am trying to learn the physics engine as well. I am doing some proves of concept for a spaceship controller (third person, big spaceship, simplified newtonian physics). So far I got a brick going wild  ;). I have not decided yet if I will use physics or not for the game.



So, I have been playing with the code as well and I found a few things that might be worth mentioning:



The dynamic node should be attached to the root node.

The box (avatar model) should be attached to the dynamic node.

If you create a model, and then attach it to the dynamic node, and generate the physics geometry you don't need the physics box (and viceversa, I think).



Ok. So you get a static box without any forces displaying because you are applying two equal and opposite forces along the same axis (y), and then the resulting force at the center of mass is zero. You can try the following changes to see some effect:





   protected void simpleInitGame()
   {
      p = PhysicsSpace.create();
      
      final DynamicPhysicsNode avatarPhysics = p.createDynamicNode();
      avatar = avatarPhysics;
      avatarPhysics.setAffectedByGravity(false);
      Box model = new Box("My box", Vector3f.ZERO, 10,10, 10);
      model.setLocalScale(.5f);
      avatar.attachChild(model);
      avatar.generatePhysicsGeometry();
      rootNode.attachChild(avatar);
      
      final Vector3f upForce = new Vector3f(0,1,0);
      final Vector3f downForce = new Vector3f(0,0/*-1*/,0);
      final Vector3f to1 = new Vector3f(0,5,10); // some offset on the Z axis so both forces are not aligned but parallel, causing rotation
      final Vector3f to2 = new Vector3f(0,-5,-10);




I am not sure if this is of any use to you. Maybe by playing with the forces and their position vectors can be helpfull to understand how it works.

By the way, if anyone has any suggestion for modelling a thrust/roll/pitch/yaw control they will be very welcome :).
Core-Dump said:

i tried to play a bit with the code, but i dodnt get anything usable out of it.
Moving a character with phyiscs seems very hard.
Maybe there are some pure ODE tutorials on that topic which could be convertet to jmephyiscs, i don't know.


I think I should have elaborated on what I was trying to demonstrate with my code a little bit more. It's supposed to apply forces to different points on the box, and it doesn't work (the forces are applied to the mass center, cancelling each other out).

I've actually already successfully used physics for movement in another project using jme, with a first person view (It's labyrinth walking project, for psychological research. I still need to add some things, but for the most part everything is working).

I wanted to try this approach for my current project because it seemed like it could be better, and I'm trying to do some more difficult things.


jiyarza said:

Hi all (new guy).  :D

Hello :)

jiyarza said:

So you get a static box without any forces displaying because you are applying two equal and opposite forces along the same axis (y), and then the resulting force at the center of mass is zero

Yes, I know, that was exactly what I was trying to demonstrate - that the forces are not being applied to different points of the physical body, but instead to a single point, and thus cancel each other out. Sorry I gave the wrong impression on what I was asking.

I'm really not sure if I should have posted all the replies in a single post, please excuse me if I should have. Wasn't expecting this many replies at once :).


basixs said:

I am not sure about your 'stretched string' approach, but maybe this thread will help:
http://www.jmonkeyengine.com/jmeforum/index.php?topic=9945.0

I've actually read that post, though I haven't tried your code yet. I was actually quite interested how the approach of adding forces at two different points would work out, since it seemed to me that in real life it could have been rather decent (if in real life velocity could magically appear for tight strings  ).

The article on ogre3d kind of gave me the idea to try out the two-force thing, by the way :).

So I'm still kind of looking forward to trying my approach out, if anyone knows how to get the forces to apply to the points I'd like them to. If for some reason this is not possible, that's ok too - like I said, I haven't tried basixs's code out yet, and have some backup ideas of my own.

The forces in the example are being applied at the correct positions. To ilustrate, is like pulling from the extremes of a rope with two forces of the same magnitude, they are not applied at the center of mass, but the resulting force is still zero.

jiyarza said:

The forces in the example are being applied at the correct positions. To ilustrate, is like pulling from the extremes of a rope with two forces of the same magnitude, they are not applied at the center of mass, but the resulting force is still zero.

That's exactly what I'm trying to do - have the box be like a rope with forces applied at to extremes. Such a rope would be very stable vertically (if you apply a force perpendicular to the to collinear forces applied at it's ends, it will either not fall down at all, or it will restore it's vertical position as soon as the perpendicular force is removed). So, if the box were to be a walking avatar (say, of a skinny and very tall person), and that avatar were to hit something with it's head (and have a perpendicular force applied), it would still not fall down.

At the moment, I'm manually removing any rotations that occur if the avatar bangs it's head, and it's not really ideal.

To verify that the forces in my example are in fact being applied to the center of mass, comment out one of the two forces I'm applying. I have physics visualization enabled, and you'll see the force being applied to the center of mass.
Wizem said:

To verify that the forces in my example are in fact being applied to the center of mass, comment out one of the two forces I'm applying. I have physics visualization enabled, and you'll see the force being applied to the center of mass.

Huh. No. I'm very sorry, I'm wrong here. It's not the forces that are being applied incorrectly, it's just that they aren't being visualized in the place they are being applied.


      final Vector3f to1 = new Vector3f(3,5,3);
      final Vector3f to2 = new Vector3f(-3,-5,-3);


This results in rotation, as expected. For some reason, it completely missed me that visualization could be a little off, and I didn't think to try this. My apologies :(. I'll go try this in my actual application then  ://

Np. The visualization can be confusing because the force it displays is just the linear velocity of the body, which results from the sum of all the forces, not the forces themselves. Since angular velocity is not displayed, forces that result in angular velocity have not any visualization.



But, for instance, in the jmephysics tutorial "Lesson8.java" you see the forces themselves, but I don't know how or why.