I use the jme3 game engine and I have this update code for a space scenario.
public void simpleUpdate(float tpf) {
playtime = System.currentTimeMillis() - starttime;
int speed = 25 * 80000;
Vector3f camDir = cam.getDirection().clone().multLocal(speed * tpf);
Vector3f camUp = cam.getUp().clone().mult(speed * tpf);
Quaternion roll = new Quaternion();
Quaternion yaw = new Quaternion();
Quaternion pitch = new Quaternion();
camDir.y = 0;
Vector3f xRotation = ufoControl.getPhysicsRotation()
.getRotationColumn(0).normalize();
Vector3f yRotation = ufoControl.getPhysicsRotation()
.getRotationColumn(1).normalize();
Vector3f zRotation = ufoControl.getPhysicsRotation()
.getRotationColumn(2).normalize();
Vector3f rotate = new Vector3f();
if (fx) {
/*
AudioNode audioSource = new AudioNode(assetManager, "Sound/fx.ogg",
false);
audioSource.setVolume(3);
audioSource.setPositional(false);
// audioSource.setLooping(true);
audioSource.play();
*/
effect.setLocalTranslation(ufoControl.getPhysicsLocation());
effect.emitAllParticles();
}
if (left) {
roll.fromAngleAxis(-FastMath.QUARTER_PI / 3, cam.getDirection());
}
if (right) {
roll.fromAngleAxis(FastMath.QUARTER_PI / 3, cam.getDirection());
}
if (up) {
roll.fromAngleAxis(0, cam.getDirection());
}
if (down) {
roll.fromAngleAxis(0, cam.getDirection());
}
if (forward) {
roll.fromAngleAxis(0, cam.getDirection());
}
if (backward) {
roll.fromAngleAxis(0, cam.getDirection());
}
if (pitch1) {
System.out.println("PITCH1");
roll.fromAngleAxis(-FastMath.QUARTER_PI / 2, cam.getDirection()
.cross(Vector3f.UNIT_Y));
}
if (yaw1) {
System.out.println("yaw1");
roll.fromAngles(0, FastMath.QUARTER_PI / 2, 0);
}
if (halt) {
System.out.println("a halt");
ufoControl.clearForces();
//roll.fromAngles(0, FastMath.QUARTER_PI / 2, 0);
}
if (roll1) {
System.out.println("roll1");
roll.fromAngleAxis(-FastMath.QUARTER_PI / 2, cam.getDirection());
}
CollisionResults results = new CollisionResults();
// System.out.println("1 #Collisions between" + ufoNode.getName()
// + " and " + jumpgateSpatial.getName() + ": " + results.size());
ufoNode.collideWith((BoundingBox) jumpgateSpatial.getWorldBound(),
results);
// System.out.println("2 #Collisions between" + ufoNode.getName()
// + " and " + jumpgateSpatial.getName() + ": " + results.size());
CollisionResults results2 = new CollisionResults();
// Use the results
if (results.size() > 0 && playtime > 50000) {
System.out.println("playtime" + playtime);
System.out.println("#Collisions between" + ufoNode.getName()
+ " and " + jumpgateSpatial.getName() + ": "
+ results.size());
// how to react when a collision was detected
CollisionResult closest = results.getClosestCollision();
System.out.println("What was hit? "
+ closest.getGeometry().getName());
System.out
.println("Where was it hit? " + closest.getContactPoint());
System.out.println("Distance? " + closest.getDistance());
ufoControl
.setPhysicsLocation(jumpGateControl2.getPhysicsLocation());
System.out.println("Warped");
} else {
// how to react when no collision occured
}
if (results2.size() > 0) {
System.out.println("Number of Collisions between"
+ ufoNode.getName() + " and " + moon.getName() + ": "
+ results2.size());
// how to react when a collision was detected
CollisionResult closest2 = results2.getClosestCollision();
System.out.println("What was hit? "
+ closest2.getGeometry().getName());
System.out.println("Where was it hit? "
+ closest2.getContactPoint());
System.out.println("Distance? " + closest2.getDistance());
}
if (myClient != null) {
Message message = new ActionMessage(1, myClient.getId(), right, 1);
Message message2 = new ActionMessage(2, myClient.getId(), left, 2);
Message message3 = new ActionMessage(2, myClient.getId(), up, 3);
Message message4 = new ActionMessage(2, myClient.getId(), down, 4);
Message message5 = new ActionMessage(2, myClient.getId(), forward,
5);
Message message6 = new ActionMessage(2, myClient.getId(), backward,
6);
if (myClient != null) {
myClient.send(message);
myClient.send(message2);
myClient.send(message3);
myClient.send(message4);
myClient.send(message5);
myClient.send(message6);
}
if (player2update == true) {
System.out.println("simpleUpdatePlayer2 player 2 "
+ message.toString());
time = System.currentTimeMillis();
simpleUpdatePlayer2(tpf);
}
}
}
We can ignore the multiplayer code for now.
The UFO doesn't move well and it seems that the steering and control of the UFO is somewhat erratic. It moves "right" and "left" almost correctly and "forward" and "backward" seems completely correct. I wonder if you can see what can be improved with the control of the UFO.
The complete code is available from my repository https://github.com/montao/spaceworld3d