Hallo, I’ve a problem with my oto character : I 'm not able to move it very good in a space, becouse it just turn around itself
Someone can halp me or know a similar test with BETTERCHARACTERCONTROL ?
thanks in advances
[java]
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
public class TotalControlChar extends SimpleApplication implements ActionListener, AnimEventListener {
private BulletAppState bulletAppState;
//for character
//private CharacterControl character;
//private Node model;
//other character
Node model_2;
BetterCharacterControl character_2 ;
//animation
private AnimControl animationControl;
private AnimChannel animationChannel;
private AnimChannel attackChannelRight;
private AnimChannel attackChannelLeft;
private AnimChannel footChannelRight;
//direction walk
private boolean left = false, right = false, up = false, down = false;
private Vector3f walkDirection = new Vector3f(0,0,0);
private float airTime = 0;
public static void main(String[] args) {
TotalControlChar app = new TotalControlChar();
app.start();
}
@Override
public void simpleInitApp(){
//activation phisycs in the scene by addyng a bulletUpState
activatPhisycs();
//initialize the scene and give it the meshCollisionShape
//in this sample, helper class creates a flat floor and drops some cubes and spheres on it.
initializeScene();
//turn on the light
lightOn();
//load character
//Create several AnimC1hannels, one for each animation that can happen simultaneously
load_AnotherCharacter();
initChannel();
//mapping the keys for moving/attack
mappingKeys();
}
public void activatPhisycs(){
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
}
public void initializeScene(){
PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());
}
public void lightOn(){
AmbientLight light = new AmbientLight();
light.setColor(ColorRGBA.White.mult(2));
rootNode.addLight(light);
}
public void PhysicsControl(){
CapsuleCollisionShape capsule = new CapsuleCollisionShape(3f, 4f);
}
//-----------------------------------------------------------------
public void load_AnotherCharacter(){
character_2 = new BetterCharacterControl(0,0,0);
model_2 = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
model_2.addControl(character_2);
character_2.setApplyPhysicsLocal(true);
character_2.warp(new Vector3f(0, 0,0));
bulletAppState.getPhysicsSpace().add(character_2);
rootNode.attachChild(model_2);
}
public void initChannel(){
//ANIMATION CONTROL
animationControl = model_2.getControl(AnimControl.class);
animationControl.addListener(this);
//with an animationControl I can create a channel for each animation character, that be happens simultaneously
animationChannel = animationControl.createChannel();
attackChannelRight = animationControl.createChannel();
attackChannelRight.addBone(animationControl.getSkeleton().getBone("uparm.right"));
attackChannelRight.addBone(animationControl.getSkeleton().getBone("arm.right"));
attackChannelRight.addBone(animationControl.getSkeleton().getBone("hand.right"));
attackChannelLeft = animationControl.createChannel();
attackChannelLeft.addBone(animationControl.getSkeleton().getBone("uparm.left"));
attackChannelLeft.addBone(animationControl.getSkeleton().getBone("arm.left"));
attackChannelLeft.addBone(animationControl.getSkeleton().getBone("hand.left"));
}
public void mappingKeys(){
inputManager.addMapping("CharLeft", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("CharRight", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("CharForward", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("CharBackward", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("CharJump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("CharAttackRight", new KeyTrigger(KeyInput.KEY_F));
inputManager.addMapping("CharAttackLeft", new KeyTrigger(KeyInput.KEY_G));
inputManager.addMapping("lower", new KeyTrigger(KeyInput.KEY_V));
inputManager.addListener(this, "CharLeft", "CharRight");
inputManager.addListener(this, "CharForward", "CharBackward");
inputManager.addListener(this, "CharRight","CharLeft");
inputManager.addListener(this, "CharBackWard", "CharForward");
inputManager.addListener(this, "CharJump", "CharJump");
inputManager.addListener(this, "CharAttackRight", "CharAttackRight");
inputManager.addListener(this, "CharAttackLeft", "CharAttackLeft");
inputManager.addListener(this, "footRight");
}
@Override
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("CharLeft")) {
if (value) left = true;
else left = false;
} else if (binding.equals("CharRight")) {
if (value) right = true;
else right = false;
} else if (binding.equals("CharForward")) {
if (value) up = true;
else up = false;
} else if (binding.equals("CharBackward")) {
if (value) down = true;
else down = false;
} else if (binding.equals("CharJump"))
character_2.jump();
else if (binding.equals("CharAttackRight")){
attack();
} else if(binding.equals("CharAttackLeft"))
attackLeft();
else if (binding.equals("lower")){
if (!animationChannel.getAnimationName().equals("Dodge")){
animationChannel.setAnim("Dodge", 1f);
// animationChannel.setLoopMode(LoopMode.Cycle);
// animationChannel.setSpeed(0.10f);
}
}
}
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().clone().multLocal(0.25f);
Vector3f camLeft = cam.getLeft().clone().multLocal(0.25f);
camDir.y = 0;
camLeft.y = 0;
walkDirection.set(0, 0, 0);
if (left) walkDirection.addLocal(camLeft);
if (right) walkDirection.addLocal(camLeft.negate());
if (up) walkDirection.addLocal(camDir);
if (down) walkDirection.addLocal(camDir.negate());
if (!character_2.isOnGround()) {
airTime = airTime + tpf;
} else {
airTime = 0;
}
if (walkDirection.length() == 0) {
if (!"stand".equals(animationChannel.getAnimationName())) {
animationChannel.setAnim("stand", 1f);
}
} else {
character_2.setViewDirection(walkDirection);
if (airTime > .3f) {
if (!"stand".equals(animationChannel.getAnimationName())) {
animationChannel.setAnim("stand");
}
} else if (!"Walk".equals(animationChannel.getAnimationName())) {
animationChannel.setAnim("Walk", 0.7f);
}
}
character_2.setWalkDirection(walkDirection); // THIS IS WHERE THE WALKING HAPPENS
}
public void attack() {
attackChannelRight.setAnim("push", 1f);
attackChannelRight.setLoopMode(LoopMode.DontLoop);
}
public void attackLeft(){
attackChannelLeft.setAnim("push",1f);
attackChannelRight.setLoopMode(LoopMode.DontLoop);
}
@Override
public void onAnimChange(AnimControl arg0, AnimChannel arg1, String arg2){
// TODO Auto-generated method stub
}
@Override
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName){
if (channel == attackChannelRight)
channel.setAnim("stand");
if (channel == attackChannelLeft)
channel.setAnim("stand");
}
}
[/java]