Got problems applying directional velocity (jME physics)

Hai!

I got the physics working by downloading eclipse and taking the tutorial for it.

Too some hours, but now its working :slight_smile:

And I made this car!

It got wheels attached to some boxed, that is attached to the car.



But I got a problem.

I can't make the car go forward by applying force in one direction, because I turn the car



So I was woundering, how do I apply velocity and torque in according to the

direction that the car is facing?





You would probebly not need this, but if u wana fool around with it 4 fun, then heres my code :slight_smile:


import java.nio.FloatBuffer;
import java.util.Vector;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Cylinder;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.RenderState;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.Joint;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.PhysicsUpdateCallback;
import com.jmex.physics.RotationalJointAxis;
import com.jmex.physics.StaticPhysicsNode;
import com.jmex.physics.TranslationalJointAxis;
import com.jmex.physics.geometry.PhysicsBox;
import com.jmex.physics.material.Material;
import com.jmex.physics.util.SimplePhysicsGame;


public class MainClass extends SimplePhysicsGame {
   public static DynamicPhysicsNode Car;
   public static MaterialState state;
   
   public static Vector<DynamicPhysicsNode> wheels = new Vector<DynamicPhysicsNode>();
   protected void simpleInitGame() {
      
      MaterialState materialState = display.getRenderer().createMaterialState();
        materialState.setAmbient(ColorRGBA.black);
        materialState.setDiffuse(ColorRGBA.blue);
        materialState.setSpecular(ColorRGBA.black);
        materialState.setShininess(0f);
        materialState.setEmissive(ColorRGBA.black);
        materialState.setEnabled(true);
        state = materialState;
      
      
      StaticPhysicsNode staticNode = getPhysicsSpace().createStaticNode();
        rootNode.attachChild( staticNode );
        PhysicsBox floorBox = staticNode.createBox( "floor" );
        floorBox.getLocalScale().set( 200, 0.5f, 200 );
        Box Box = new Box( "Box", new Vector3f(), 100f, 0.25f, 100f );
        rootNode.attachChild(Box);

        MaterialState newMat = display.getRenderer().createMaterialState();
        newMat.setAmbient(ColorRGBA.black);
        newMat.setDiffuse(ColorRGBA.red);
        newMat.setSpecular(ColorRGBA.black);
        newMat.setShininess(0f);
        newMat.setEmissive(ColorRGBA.black);
        newMat.setEnabled(true);
       
        //final RotationalJointAxis rotationalAxis = jointForSphere.createRotationalAxis();
        //rotationalAxis.setDirection(new Vector3f(0f,0f,1));
        //.attach(rotationalAxis);
        //Back!
       
        DynamicPhysicsNode CarBack = getPhysicsSpace().createDynamicNode();
        Box Body = new Box( "Box", new Vector3f(), 2f, 1f, 2f );
        Body.setRenderState(newMat);
       
        CarBack.attachChild( Body );
        CarBack.generatePhysicsGeometry();
        CarBack.getLocalTranslation().set(new Vector3f(0,2.5f,-2));
        rootNode.attachChild(CarBack);
      
        DynamicPhysicsNode part1 = getWheel(0,1,0,rootNode,getPhysicsSpace().createDynamicNode(),getPhysicsSpace().createDynamicNode(),getPhysicsSpace().createJoint());
        final Joint CarW1 = getPhysicsSpace().createJoint();
        CarW1.attach( CarBack, part1);
       
        DynamicPhysicsNode part2 = getWheel(0,-5,-4,rootNode,getPhysicsSpace().createDynamicNode(),getPhysicsSpace().createDynamicNode(),getPhysicsSpace().createJoint());
        final Joint CarW2 = getPhysicsSpace().createJoint();
        CarW2.attach( CarBack, part2);
        //Back end
       
        //Front!
       
        DynamicPhysicsNode CarFront = getPhysicsSpace().createDynamicNode();
        Box Body2 = new Box( "Box", new Vector3f(), 2f, 1f, 2f );
        Body2.setRenderState(newMat);
       
        CarFront.attachChild( Body2 );
        CarFront.generatePhysicsGeometry();
        int forwardPush = 5;
        CarFront.getLocalTranslation().set(new Vector3f(forwardPush,2.5f,-2));
        rootNode.attachChild(CarFront);
      
        DynamicPhysicsNode part3 = getWheel(forwardPush,1,0,rootNode,getPhysicsSpace().createDynamicNode(),getPhysicsSpace().createDynamicNode(),getPhysicsSpace().createJoint());
        final Joint CarW3 = getPhysicsSpace().createJoint();
        CarW3.attach( CarFront, part3);
       
        DynamicPhysicsNode part4 = getWheel(forwardPush,-5,-4,rootNode,getPhysicsSpace().createDynamicNode(),getPhysicsSpace().createDynamicNode(),getPhysicsSpace().createJoint());
        final Joint CarW4 = getPhysicsSpace().createJoint();
        CarW4.attach( CarFront, part4);
       
        //Front end
       
        //CarMainPart!

        Car = getPhysicsSpace().createDynamicNode();
        Box Body3 = new Box( "Box", new Vector3f(), 3f, 1f, 2f );
        Body3.setRenderState(newMat);
        Body3.getLocalTranslation().set(new Vector3f(2.5f,4,-2));
        Car.attachChild(Body3);
        Car.generatePhysicsGeometry();
        final Joint CarPartAdd1 = getPhysicsSpace().createJoint();
        CarPartAdd1.setCollisionEnabled(false);
        CarPartAdd1.attach( Car, CarFront);
        final Joint CarPartAdd2 = getPhysicsSpace().createJoint();
        CarPartAdd2.setCollisionEnabled(false);
        CarPartAdd2.attach( Car, CarBack);
        rootNode.attachChild(Car);
       
        showPhysics = true;
        pause = true;
        KeyBindingManager.getKeyBindingManager().set("MoveIt", KeyInput.KEY_U);
        KeyBindingManager.getKeyBindingManager().set("MoveItb", KeyInput.KEY_J);
        KeyBindingManager.getKeyBindingManager().set("MoveSide2", KeyInput.KEY_H);
        KeyBindingManager.getKeyBindingManager().set("MoveSide", KeyInput.KEY_K);
        KeyBindingManager.getKeyBindingManager().set("MoveUp", KeyInput.KEY_I);

   }
   
   public static void main(String[] args){
      MainClass app = new MainClass();
      app.start();
   }
   public void simpleUpdate(){
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("MoveIt",true)){
         //Car.addForce(new Vector3f(0f,10f,0f));
         //wheels.elementAt(0).addTorque(new Vector3f(0f,0f,5f));
         //Car.setLinearVelocity();
         Car.addForce(Car.);

      }
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("MoveItb",true)){
         //Car.addForce(new Vector3f(10f,0f,0f));
         Car.setLinearVelocity(new Vector3f(10,0,0));
      }
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("MoveSide",true)){
         //Car.addForce(new Vector3f(0f,10f,0f));
         Car.addTorque(new Vector3f(0f,-1000f,0f));
         //wheels.elementAt(0).addTorque(new Vector3f(0f,0f,5f));
      }
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("MoveSide2",true)){
         Car.addTorque(new Vector3f(0f,1000f,0f));
      }
      if (KeyBindingManager.getKeyBindingManager().isValidCommand("MoveUp",true)){
           if (showPhysics == true){
              showPhysics = false;
           } else {
              showPhysics = true;
           }
        }
   }
   
   public static DynamicPhysicsNode createBox(DynamicPhysicsNode dynamicNode){
      final Box Box = new Box( "Box", new Vector3f(), 0.5f, 0.5f, 0.5f );
        dynamicNode.attachChild( Box );
        dynamicNode.generatePhysicsGeometry();
        //dynamicNode.getLocalTranslation().set( 0, 50, 0 );
      return dynamicNode;
   }
   public static DynamicPhysicsNode createCylinder(DynamicPhysicsNode dynamicNode){
      final Cylinder Box = new Cylinder("Cylinder", 10, 10, 1f, 1f,true);
      dynamicNode.attachChild(Box);
        dynamicNode.generatePhysicsGeometry();
       
        Box.setRenderState(state);
        //dynamicNode.getLocalTranslation().set( 0, 50, 0 );
      return dynamicNode;
   }
   
   public static DynamicPhysicsNode getWheel(int forwardPush, int way, int dir, Node rootNode,DynamicPhysicsNode dynamicBoxNode1,DynamicPhysicsNode weel1,Joint jointForCar){
        rootNode.attachChild( dynamicBoxNode1 );
        dynamicBoxNode1.createBox( "box1" );
        rootNode.attachChild( weel1 );
        weel1.createCylinder("Weel1");
       
        dynamicBoxNode1.getLocalTranslation().set( forwardPush, 2, dir );
        weel1.getLocalTranslation().set( forwardPush, 2, way );
        createBox(dynamicBoxNode1);
        createCylinder(weel1);
       
       
        wheels.add(weel1);
        RotationalJointAxis rotationalAxis = jointForCar.createRotationalAxis();
        rotationalAxis.setDirection(new Vector3f(0f,0f,way));
        jointForCar.setCollisionEnabled(true);
        jointForCar.attach( dynamicBoxNode1, weel1 );
       
        return dynamicBoxNode1;
        //iweel1.setMaterial( Material.ICE );
   }

}

Okej, after alot of searching I found out that you could do this:

Vector3f force = new Vector3f();
           Car.getForce(force);
           force = force.addLocal(Car.getLocalRotation().getRotationColumn(0).multLocal(speed));   
         Car.setLinearVelocity(force);


But the problem is that the point that the velocity is applied on is not in the middle of my "Car" node!
I tried to do:
Car.setCenterOfMass(new Vector3f(2.75f,2.5f,0));
But that messed it up alot and the point still didnt move :(
That point Im talking about is the green small cross with 3 axis..
How do I move that one?

Hi,



there is a car sample you can find in the forums or the jme2 phisics wiki. In that sample the forces were applied to the wheels intead of the car itself.  Give it a try.

Thanks but I got it working :slight_smile:

Heres a preview: http://www.youtube.com/watch?v=kEyB5UGhDvg

Nice