Hai!
I made this car game with jME and jME physics.
Now I set so the camera should look backwards, onto the car.
It does that, but like every 1.5 seconds it shakes and gives a totaly diffrent angle
Why?
Heres my whole code, it's just copy and run incase you wana see/ play for urself
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.CameraNode;
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 int speed= 10;
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);
//Car.setCenterOfMass(new Vector3f(2.75f,2.5f,0));
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(){
Vector3f extraVec = Car.getLocalRotation().getRotationColumn(0).mult(30);
cam.setLocation(Car.getWorldTranslation().add(new Vector3f(0,10,0)).add(extraVec));
cam.lookAt(Car.getLocalTranslation(), new Vector3f(0,0,0));
if (KeyBindingManager.getKeyBindingManager().isValidCommand("MoveIt",true)){
//Car.addForce(new Vector3f(0f,10f,0f));
//wheels.elementAt(0).addTorque(new Vector3f(0f,0f,5f));
//Car.setLinearVelocity();
Vector3f force = new Vector3f();
Car.getForce(force);
force = force.addLocal(Car.getLocalRotation().getRotationColumn(0).multLocal(speed));
force.x = -force.x;
force.z = -force.z;
wheels.elementAt(2).addForce(force);
wheels.elementAt(3).addForce(force);
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand("MoveItb",true)){
Vector3f force = new Vector3f();
Car.getForce(force);
force = force.addLocal(Car.getLocalRotation().getRotationColumn(0).multLocal(speed));
wheels.elementAt(2).addForce(force);
wheels.elementAt(3).addForce(force);
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand("MoveSide",true)){
//Car.addForce(new Vector3f(0f,10f,0f));
Car.addTorque(new Vector3f(0f,-100f,0f));
//wheels.elementAt(0).addTorque(new Vector3f(0f,0f,5f));
}
if (KeyBindingManager.getKeyBindingManager().isValidCommand("MoveSide2",true)){
Car.addTorque(new Vector3f(0f,100f,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 );
}
}
Thanks for taking time trying to help me :)
I really would like to get help with this!