Physic force with analog input

Hi,

I recently played around with jme3 and started with a PhysicsNode. The node represent a spaceship in my simulation. I chose a PhyicsNode for this, because I want a inertia behaviour for my simulation and I think I can acquire it best this way.

Now to my problems.

I implemented an analog input with AnalogListener. This way i want to scale the forces that affect my ship. Here is my code for this:

[java]

public class Spaceship extends PhysicsNode implements AnalogListener {

private float currentSpeed;

private float maxSpeed;

private float acceleration;

private float agility;



public Spaceship() {

super(new BoxCollisionShape(Vector3f.UNIT_XYZ), 15f);

this.setFriction(0f);



//Ship settings

currentSpeed = 0;

maxSpeed = 100;

acceleration = 1f;

agility = 1f;

}



public void setupKeys(InputManager inputManager) {

this.inputManager = inputManager;

String[] mappings = new String[]{

"rotateLeft",

"rotateRight",

"rotateUp",

"rotateDown",

"rollLeft",

"rollRight",



"strafeLeft",

"strafeRight",

"strafeUp",

"strafeDown",



"accelerate",

"decelerate"

};



//mouse and button rotation of ship

inputManager.addMapping("rotateLeft", new MouseAxisTrigger(0, true), new KeyTrigger(KeyInput.KEY_LEFT));

inputManager.addMapping("rotateRight", new MouseAxisTrigger(0, false), new KeyTrigger(KeyInput.KEY_RIGHT));

inputManager.addMapping("rotateUp", new MouseAxisTrigger(1, false), new KeyTrigger(KeyInput.KEY_UP));

inputManager.addMapping("rotateDown", new MouseAxisTrigger(1, true), new KeyTrigger(KeyInput.KEY_DOWN));

inputManager.addMapping("rollLeft", new KeyTrigger(KeyInput.KEY_Q));

inputManager.addMapping("rollRight", new KeyTrigger(KeyInput.KEY_E));



// accelerate/decelerate with wheel, and buttons

inputManager.addMapping("accelerate", new MouseAxisTrigger(2, false), new KeyTrigger(KeyInput.KEY_R));

inputManager.addMapping("decelerate", new MouseAxisTrigger(2, true), new KeyTrigger(KeyInput.KEY_F));



// keyboard WASD for strafeing

inputManager.addMapping("strafeLeft", new KeyTrigger(KeyInput.KEY_A));

inputManager.addMapping("strafeRight", new KeyTrigger(KeyInput.KEY_D));

inputManager.addMapping("strafeUp", new KeyTrigger(KeyInput.KEY_W));

inputManager.addMapping("strafeDown", new KeyTrigger(KeyInput.KEY_S));



inputManager.addListener(this, mappings);

}



public void onAnalog(String name, float value, float tpf) {

if (name.equals("rotateLeft")){

rotateShip(value, Vector3f.UNIT_Y);

}else if (name.equals("rotateRight")){

rotateShip(-value, Vector3f.UNIT_Y);

}else if (name.equals("rotateUp")){

rotateShip(value, Vector3f.UNIT_X);

}else if (name.equals("rotateDown")){

rotateShip(-value, Vector3f.UNIT_X);

}else if (name.equals("rollLeft")){

rotateShip(value, Vector3f.UNIT_Z);

}else if (name.equals("rollRight")){

rotateShip(-value, Vector3f.UNIT_Z);

}



else if(name.equals("strafeLeft")){

strafeShip(value, Vector3f.UNIT_Y);

}else if (name.equals("strafeRight")){

strafeShip(-value, Vector3f.UNIT_Y);

}else if (name.equals("strafeUp")){

strafeShip(value, Vector3f.UNIT_X);

}else if (name.equals("strafeDown")){

strafeShip(-value, Vector3f.UNIT_X);

}



else if(name.equals("accelerate")){

accelerate(value);

}else if (name.equals("decelerate")){

decelerate(value);

}

}



//Raumschiff beschleunigen

public void accelerate(float value){

}



//Raumschiff abbremsen

public void decelerate(float value) {

}



private void rotateShip(float value, Vector3f axis){

Vector3f force = axis.mult(value * agility);

applyTorque(force);

}



private void strafeShip(float value, Vector3f axis){

//setAngularVelocity(axis.multLocal(value * agility));

applyCentralForce(axis.mult(value * agility));

}

}

[/java]



The forces I want to apply this way (in method rotateShip) seem too small (force (0.0015312501, 0, 0) for example) and there is no change in the simulation that I can see. So I tried to scale the Vector3f force up with an agility of 10 but still nothing happens and 100 and the ship starts to spin like crazy.

So my questions are How exactly works the return value from AnalogListener in the onAnalog method (I read that the value is between 0 and 1 in the javadoc)? What are reasonable bounds for forces in a physics simulation? And have you tips, to improve my code? :slight_smile:



The second problem Im facing right now is the acceleration. I want to simulate a inertia behaviour here too. Means, I want to accelerate in the direction the ship is facing, causing the ship to fly a curve, if it has alredy an accelertion to another direction, instead of flying in a straight line.

But I need a direction for this, when I call PhysicsNode.applyContinousForce() right? Do I do this with PhysicsNode.getPhysicsRotation() or am I on the wrong way to do this?



meddins

What forces you use depends on your physics objects, their mass etc. The current rotation of the ship is in getLocalRotation()

Hey,



ty for the hints. I use now the tpf value combined with the value from the AnalogInput and reduced the mass. Now the steering bahavior is like I want it.



[java]

if (name.equals(“rotateLeft”)){

rotateShip(value/tpf, Vector3f.UNIT_Y);

}

[/java]



Im still at implementing my acceleration. But I have a offtopic question. Is it normal, that i can see the replys from some topics very late? I saw in the overview that somone answered my post. But I could see the answer first 6h later. Is this because of the explorer cache? I also have some problems with some buttons that I cant see.



meddins

When you are not logged into the site you are served a cached version to reduce server strain.

I had the same problem, but only if i’m not logged in. However it only appeard to me once so I thought it was fixed.