First Person Class

Hey guys!



I’m trying to develop a first person class, that could handle the keys, etc. I looked at some jme3 examples and at the wiki, but I wasn’t able to get all that information and put all in a single class that once in the main class, use a method to build a simple first-person object.



Does someone knows how to do this?



Thanks in advance, borba.

I honestly dont know what better examples than TestQ3 and TestWalkingChar we could have made except writing your class. I am available for 50€/hour if you’re interested.

Thanks for your reply, normen!



What I mean, is that I don’t know how to use the inputmanager and other stuff in class that isn’t the main without creating a simpleApplication. So normen, just a simple beginner question, how can I deal with the inputmanger in a class that isn’t the main?

This is beyond the scope of jME support, read up on java coding, objects, classes or look at some simple java examples.

Thanks for your tip, normen. But I was taking a look at TestQ3 and I tried to use compile it, but it sems that there is no class named:

com.jme3.input.FirstPersonCamera; What’s wrong?

You are missing an import or you messed up your installation. I will stop now, can be reactivated by a video with a jme nuke.

Haha, you still remember! :smiley: - aww, that wasn’t a nuke, and i got a virus and all my files got deleted, sorry normen, but I’m happy you remember.

I’m still trying, already reinstalled jme3, but no changes I don’t have com.jme3.input.FirstPersonCamera;

I offer 20€/hour xD



Here is a small code example. You can copy 99% in your own class. I used originally TestQ3



[java]public class Game extends XeroAppScene implements ActionListener {



private boolean left=false,right=false,up=false,down=false;

private Vector3f walkDirection = new Vector3f();

private PhysicsCharacterNode physicsCharacter;





public Game(String name, Xero app) {

super(name, app);

}



public void buffer() {

this.attachChild(SkyFactory.createSky(assetManager, “data/texture/sky/space.png”, false));

// sunset light

DirectionalLight dl = new DirectionalLight();

dl.setDirection(new Vector3f(-0.1f, -0.7f, 1).normalizeLocal());

dl.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));

this.addLight(dl);



DirectionalLight d2 = new DirectionalLight();

d2.setDirection(new Vector3f(0.1f, 0.7f, -1).normalizeLocal());

d2.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));

this.addLight(d2);



setupCharacter();





Node human = (Node) assetManager.loadModel(“data/mesh/level/spaceship/level_spaceship.mesh.xml”);

human.setLocalTranslation(new Vector3f(0,0,0));

//Node human = (Node) assetManager.loadModel(“data/mesh/test/test_human.mesh.xml”);

//human.setLocalScale(1f);

this.attachChild(human);



Node tank = (Node) assetManager.loadModel(“Models/HoverTank/Tank2.mesh.xml”);

tank.setLocalScale(1f);

this.attachChild(tank);



//CompoundCollisionShape shape = CollisionShapeFactory.createMeshCompoundShape((Node) tank);

CollisionShape shape_tank = CollisionShapeFactory.createMeshShape(tank.getChild(0));

PhysicsNode tank_PhysicsNode = new PhysicsNode(tank, shape_tank,(float) 1.5);//1.5

tank_PhysicsNode.setLocalTranslation(new Vector3f(-20, 1, -20));

tank_PhysicsNode.attachDebugShape(assetManager);

tank_PhysicsNode.setMass(0f);

this.attachChild(tank_PhysicsNode);

app.getPhysicsSpace().add(tank_PhysicsNode);





Node crate = (Node) assetManager.loadModel(“data/mesh/object/crate/crate01.mesh.xml”);

this.attachChild(crate);



//CompoundCollisionShape shape = CollisionShapeFactory.createMeshCompoundShape((Node) tank);

CollisionShape shape = CollisionShapeFactory.createMeshShape(crate.getChild(0));

PhysicsNode crate_PhysicsNode = new PhysicsNode(crate, shape,(float) 1.5);//1.5

crate_PhysicsNode.setLocalTranslation(new Vector3f(0, 1, 0));

crate_PhysicsNode.attachDebugShape(assetManager);

crate_PhysicsNode.setMass(0.1f);

this.attachChild(crate_PhysicsNode);

app.getPhysicsSpace().add(crate_PhysicsNode);





/Node map = (Node) assetManager.loadModel(“data/mesh/level/spaceship/level_spaceship.mesh.xml”);

map.setLocalScale(1f);

this.attachChild(map);
/



// The floor, does not move (mass=0)

PhysicsNode node3 = new PhysicsNode(new BoxCollisionShape(new Vector3f(40, 1, 40)), 0);

node3.setLocalTranslation(new Vector3f(0f, -1.0f, 0f));

node3.attachDebugShape(assetManager);

this.attachChild(node3);

app.getPhysicsSpace().add(node3);

}



public void init() {

app.getFlyByCamera().setMoveSpeed(100);

app.getFlyByCamera().setEnabled(true);

app.getFlyByCamera().setDragToRotate(false);



setupKeys();

}



private void setupCharacter(){

physicsCharacter = new PhysicsCharacterNode(new SphereCollisionShape(0.2f), .1f);

physicsCharacter.setLocalTranslation(new Vector3f(0, 12, 0));

physicsCharacter.setJumpSpeed(10);

physicsCharacter.setFallSpeed(30);

physicsCharacter.setGravity(30);

//physicsCharacter.setMaxSlope(0.02f);

Material mat = app.getAssetManager().loadMaterial(“Common/Materials/RedColor.j3m”);

physicsCharacter.attachDebugShape(mat);

this.attachChild(physicsCharacter);

app.getPhysicsSpace().add(physicsCharacter);

}



private void setupKeys() {

inputManager.addMapping(“left”, new KeyTrigger(KeyInput.KEY_A));

inputManager.addMapping(“right”, new KeyTrigger(KeyInput.KEY_D));

inputManager.addMapping(“up”, new KeyTrigger(KeyInput.KEY_W));

inputManager.addMapping(“down”, new KeyTrigger(KeyInput.KEY_S));

inputManager.addMapping(“jump”, new KeyTrigger(KeyInput.KEY_SPACE));

inputManager.addMapping(“shoot”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

inputManager.addListener(this,“left”);

inputManager.addListener(this,“right”);

inputManager.addListener(this,“up”);

inputManager.addListener(this,“down”);

inputManager.addListener(this,“jump”);

inputManager.addListener(this,“shoot”);

}



public void onAction(String binding, boolean value, float tpf) {



if (binding.equals(“left”)) {

if(value)

left=true;

else

left=false;

} else if (binding.equals(“right”)) {

if(value)

right=true;

else

right=false;

} else if (binding.equals(“up”)) {

if(value)

up=true;

else

up=false;

} else if (binding.equals(“down”)) {

if(value)

down=true;

else

down=false;

}



if (binding.equals(“shoot”) && !value) {



Sphere bullet = new Sphere(32, 32, 0.4f, true, false);

bullet.setTextureMode(TextureMode.Projected);

SphereCollisionShape bulletCollisionShape = new SphereCollisionShape(0.4f);



Material mat = app.getAssetManager().loadMaterial(“Common/Materials/RedColor.j3m”);



Geometry bulletg = new Geometry(“bullet”, bullet);

bulletg.setMaterial(mat);

PhysicsNode bulletNode = new PhysicsNode(bulletg, bulletCollisionShape, 1f);



//Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);

bulletNode.setLocalTranslation(cam.getLocation());



//bulletNode.setGravity(new Vector3f(0,-9f,0));



bulletNode.setShadowMode(ShadowMode.CastAndReceive);



bulletNode.setLinearVelocity(cam.getDirection().mult(25));

this.attachChild(bulletNode);

app.getPhysicsSpace().add(bulletNode);

bulletNode.setGravity(Vector3f.ZERO);

}



if (binding.equals(“jump”)) {

physicsCharacter.jump();

}

}



public void update() {

walkDirection.set(0f,0,0);

Vector3f camDir = cam.getDirection().clone().multLocal(0.06f);



Vector3f camLeft = cam.getLeft().clone().multLocal(0.04f);



if(left){

walkDirection.addLocal(camLeft);

}

if(right){

walkDirection.addLocal(camLeft.negate());

}

if(up){

walkDirection.addLocal(camDir);

}

if(down){

walkDirection.addLocal(camDir.negate());

}





physicsCharacter.setWalkDirection(walkDirection);

cam.setLocation(physicsCharacter.getLocalTranslation().add(new Vector3f(0,1.7f,0)));



}[/java]

1 Like

Thanks DarkPhoenix!



A final question, i don’t this isn’t related with the topic, but how to make cylinders in jme3?

borba said:
(...) A final question, i don't this isn't related with the topic, but how to make cylinders in jme3?

Start a new thread for it, and be more specific. A cylinder could very well be a model that you made in Blender and imported into jME3, but I'm guessing that's not what you're after.

Thanks for your tip, erlend_sh.



I made my own class but i’m having some problems with it.


  1. If I look to the floor and press “S” (Down) my character flies, this also happens when I look to the roof and press “W” (UP)
  2. The character, sometimes, keep giving little jumps, very small jumps.



    The code:



    [java]public class Player implements ActionListener {



    private PhysicsCharacterNode player;

    private InputManager inputManager;

    public boolean left = false,

    right = false,

    up = false,

    down = false;

    private Vector3f walkDirection = new Vector3f();



    private String username;



    Camera cam;

    FlyByCamera flyCam;



    public Player(String username, InputManager inputManager, Camera cam, FlyByCamera flyCam)

    {

    this.username = username;

    this.inputManager = inputManager;

    this.cam = cam;

    this.flyCam = flyCam;

    setupPlayer();

    setupKeys();

    }



    private void setupPlayer()

    {

    flyCam.setMoveSpeed(100);

    cam.setFrustumFar(2000);



    player = new PhysicsCharacterNode(new SphereCollisionShape(5), .01f);

    player.setJumpSpeed(20);

    player.setFallSpeed(30);

    player.setGravity(30);

    }



    private void setupKeys()

    {

    inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_A));

    inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));

    inputManager.addMapping(“Up”, new KeyTrigger(KeyInput.KEY_W));

    inputManager.addMapping(“Down”, new KeyTrigger(KeyInput.KEY_S));

    inputManager.addListener(this,“Left”);

    inputManager.addListener(this,“Right”);

    inputManager.addListener(this,“Up”);

    inputManager.addListener(this,“Down”);

    }



    ///


public PhysicsCharacterNode getPhysicsCharacterNode()
{
return player;
}

public String getPlayerName()
{
return username;
}

public Vector3f getWalkDirection()
{
return walkDirection;
}

public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("Left")) {
if(isPressed)
left = true;
else
left = false;
} else if(name.equals("Right")) {
if(isPressed)
right = true;
else
right = false;
} else if(name.equals("Up")) {
if(isPressed)
up = true;
else
up = false;
} else if(name.equals("Down")) {
if(isPressed)
down = true;
else
down = false;
}
}
}[/java]

In the AppState Main class:

[java]protected void setupPlayer()
{
player = new Player("Gustavo", inputManager, cam, flyCam);
player.getPhysicsCharacterNode().setLocalTranslation(new Vector3f(0, 4.5f, 0));
rootNode.attachChild(player.getPhysicsCharacterNode());
bulletAppState.getPhysicsSpace().add(player.getPhysicsCharacterNode());
}[/java]

[java]@Override
public void simpleUpdate(float tpf) {
rootNode.updateGeometricState();
Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
player.getWalkDirection().set(0, 0, 0);
if(player.left)
player.getWalkDirection().addLocal(camLeft);
if(player.right)
player.getWalkDirection().addLocal(camLeft.negate());
if(player.up)
player.getWalkDirection().addLocal(camDir);
if(player.down)
player.getWalkDirection().addLocal(camDir.negate());
player.getPhysicsCharacterNode().setWalkDirection(player.getWalkDirection());
cam.setLocation(player.getPhysicsCharacterNode().getLocalTranslation());
}[/java]

Thanks in advance, borba.

Increase to 0.4f

player = new PhysicsCharacterNode(new SphereCollisionShape(5), 0.4f);



That should reduce the hopping.





To stop flying:

if(player.left)

player.getWalkDirection().addLocal(camLeft);

if(player.right)

player.getWalkDirection().addLocal(camLeft.negate());

if(player.up)

player.getWalkDirection().addLocal(camDir);

if(player.down)

player.getWalkDirection().addLocal(camDir.negate());



set the local value y to 0

Y is the high!



Should look like that:

player.getWalkDirection().setY(0f);



That fix the problem, but jumping will get the new problem xD

I fixed somehow the problem, but can’t remember. You could set the Cam.Y to 0. That shouldn’t affect jumping

Thanks DarkPohenix, I implemented the jump now, so I can’t use player.getWalkDirection().setY(0f);

Does anybody else know how to stop this?

I told you…



1)Set CamDir value y → 0

That doesn’t affect jump



2)You can increase the mass



Otherwise you have to improvise! xD