Greetings,
I have a problem for a few weeks with a movement and I want to help from you please. (sorry for my bad english, I’m czech)
I have model from Blender, tank. I want to move with tank forwards, backwards and except move I want rorate left and right.
My code is rorating the tank left and right but only with camera, when I put line flyCam.setEnabled(false); camera will not be able to move but tank too.
So I want to solve that problem to move tank and rorate him, without camera – camera will be able to move with some chars like “w”, “s”. “a”. “d” and tank will be able to move and rotate for example with “j”, “k”, “i”, “l”.
That’s the problem that I cant solve, I was looking for many topics and talking to some peoples, but in our school nobody is programming in jME.
After that, I want to create shooting with tank and with fire effect to other tanks, that enemy tanks will be creating randomly and I want to create player base, that can be damaged and destroyed and after destroy the player will loose. Something like game Tank 1980 Battalion.
If someone can help me with the first problem the movement and get me some hints, how I can create the shooting and the other things, I will be very happy.
Here is my code:
package mygame;
[java]import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.post.FilterPostProcessor;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.texture.Texture2D;
import com.jme3.water.WaterFilter;
public class Main extends SimpleApplication implements ActionListener {
private Spatial sceneModel;
private WaterFilter water;
private Vector3f lightDir = new Vector3f (-4f,-1f,5f);
private BulletAppState bulletAppState;
private CharacterControl physicsCharacter;
private Node characterNode;
private Vector3f walkDirection = new Vector3f(0,0,0);
private Vector3f viewDirection = new Vector3f(0,0,0);
boolean leftStrafe = false, rightStrafe = false, forward = false, backward = false,
leftRotate = false, rightRotate = false;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
protected Geometry player;
Boolean isRunning=true;
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
physicsCharacter = new CharacterControl(new CapsuleCollisionShape(0.5f, 1.8f), .1f);
physicsCharacter.setPhysicsLocation(new Vector3f(0, 1, 0));
characterNode = new Node("character node");
Spatial model = assetManager.loadModel("Models/Tank/tank.scene");
characterNode.addControl(physicsCharacter);
// getPhysicsSpace().add(physicsCharacter);
rootNode.attachChild(characterNode);
characterNode.attachChild(model);
flyCam.setMoveSpeed(100f);
initScene();
initLight();
// flyCam.setEnabled(false);
}
private void setupKeys() {
inputManager.addMapping("Strafe Left",
new KeyTrigger(KeyInput.KEY_Q),
new KeyTrigger(KeyInput.KEY_Z));
inputManager.addMapping("Strafe Right",
new KeyTrigger(KeyInput.KEY_E),
new KeyTrigger(KeyInput.KEY_X));
inputManager.addMapping("Rotate Left",
new KeyTrigger(KeyInput.KEY_Z),
new KeyTrigger(KeyInput.KEY_LEFT));
inputManager.addMapping("Rotate Right",
new KeyTrigger(KeyInput.KEY_D),
new KeyTrigger(KeyInput.KEY_RIGHT));
inputManager.addMapping("Walk Forward",
new KeyTrigger(KeyInput.KEY_W),
new KeyTrigger(KeyInput.KEY_UP));
inputManager.addMapping("Walk Backward",
new KeyTrigger(KeyInput.KEY_S),
new KeyTrigger(KeyInput.KEY_DOWN));
inputManager.addMapping("Jump",
new KeyTrigger(KeyInput.KEY_SPACE),
new KeyTrigger(KeyInput.KEY_RETURN));
inputManager.addMapping("Shoot",
new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(this, "Strafe Left", "Strafe Right");
inputManager.addListener(this, "Rotate Left", "Rotate Right");
inputManager.addListener(this, "Walk Forward", "Walk Backward");
inputManager.addListener(this, "Jump", "Shoot");
}
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().mult(0.2f);
Vector3f camLeft = cam.getLeft().mult(0.2f);
camDir.y = 0;
camLeft.y = 0;
viewDirection.set(camDir);
walkDirection.set(0, 0, 0);
if (leftStrafe) {
walkDirection.addLocal(camLeft);
} else
if (rightStrafe) {
walkDirection.addLocal(camLeft.negate());
}
if (leftRotate) {
viewDirection.addLocal(camLeft.mult(0.02f));
} else
if (rightRotate) {
viewDirection.addLocal(camLeft.mult(0.02f).negate());
}
if (forward) {
walkDirection.addLocal(camDir);
} else
if (backward) {
walkDirection.addLocal(camDir.negate());
}
physicsCharacter.setWalkDirection(walkDirection);
physicsCharacter.setViewDirection(viewDirection);
}
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("Strafe Left")) {
if (value) {
leftStrafe = true;
} else {
leftStrafe = false;
}
} else if (binding.equals("Strafe Right")) {
if (value) {
rightStrafe = true;
} else {
rightStrafe = false;
}
} else if (binding.equals("Rotate Left")) {
if (value) {
leftRotate = true;
} else {
leftRotate = false;
}
} else if (binding.equals("Rotate Right")) {
if (value) {
rightRotate = true;
} else {
rightRotate = false;
}
} else if (binding.equals("Walk Forward")) {
if (value) {
forward = true;
} else {
forward = false;
}
} else if (binding.equals("Walk Backward")) {
if (value) {
backward = true;
} else {
backward = false;
}
} else if (binding.equals("Jump")) {
physicsCharacter.jump();
}
}
@Override
public void simpleRender(RenderManager rm) {
}
private PhysicsSpace getPhysicsSpace() {
return bulletAppState.getPhysicsSpace();
}
private void initScene(){
sceneModel = assetManager.loadModel("Scenes/Map01.j3o");
rootNode.attachChild(sceneModel);
}
private void initLight(){
AmbientLight ambient = new AmbientLight();
ambient.setColor(ColorRGBA.White);
rootNode.addLight(ambient);
}
}
[/java]
Some code is from jME tests.
The Main problems are the movement, then the shooting with fire effect on the base.
Second problem not that much important is the enemy tanks generating with primitive UI.
I was looking for everything I have found every tutorials for it and topics but I have not get it.
Very please, help me with the problem of movement and what program is best to create the base and put it on the scene in jME, maybe the Blender? And how to do the shootings.
Thanks!