Hi, I just got in JM3 since a week and facing a simple problem, I guess.
I have 2 players and each of them has a camera attached as a child (within a CameraNode), but I don’t know how I switch between those cameras
and I don’t know what I need to move to follow those cameras. The idea is to switch between those players after each shot.
I worked with one camera yet and move its location, but that didn’t worked out that well in some cases.
- I created 2 players (CharacterControl + Models)
- I created 2 CameraNode and attached to each of them Cameras
- I attached the 2 CameraNodes to the player
As far as I understand, now the cameras should move automatically with the players node, but what do I need to move and how do I
switch the focus between those 2 cameras?
I would appreciate any help. Here's a hacked example code:
[java]
class {
private BulletAppState bulletAppState;
private CharacterControl player, player2;
private Node modelPlayer1, modelPlayer2, currentPlayer;
private Camera cam1, cam2;
private CameraNode camNode1, camNode2;
private boolean left = false, right = false, up = false, down = false;
...
public void simpleInitApp() {
...
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
...
// creating floors and so forth...
...
this.initCameras();
this.addTwoTestPlayers();
...
}
private void initCameras() {
this.camNode1 = new CameraNode("cam1", cam);
this.camNode2 = new CameraNode("cam2", cam);
camNode1.setControlDir(ControlDirection.SpatialToCamera);
camNode2.setControlDir(ControlDirection.SpatialToCamera);
}
private void addTwoTestPlayers() {
// Add 1st player
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, .01f);
player.setJumpSpeed(20);
player.setFallSpeed(60);
player.setGravity(100);
DirectionalLight sun = new DirectionalLight();
sun.setDirection(<startpoint player 1>);
this.getRootNode().addLight(sun);
this.modelPlayer1 = (Node) assetManager.loadModel("Models/Ninja/Ninja.mesh.xml");
this.modelPlayer1.scale(0.05f, 0.05f, 0.05f);
this.modelPlayer1.setName("Model_1");
this.modelPlayer1.addControl(player);
this.modelPlayer1.attachChild(this.camNode1);
this.getRootNode().attachChild(modelPlayer1);
this.getPhysicsSpace().add(modelPlayer1);
this.modelPlayer1.setLocalTranslation(<startpoint player 1>);
// Add 2nd player
capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player2 = new CharacterControl(capsuleShape, .01f);
player2.setJumpSpeed(20);
player2.setFallSpeed(60);
player2.setGravity(100);
sun = new DirectionalLight();
sun.setDirection(<startpoint player 2>);
this.getRootNode().addLight(sun);
this.modelPlayer2 = (Node) assetManager.loadModel("Models/Ninja/Ninja.mesh.xml");
this.modelPlayer2.scale(0.05f, 0.05f, 0.05f);
this.modelPlayer2.setName("Model_2");
this.modelPlayer2.addControl(player2);
this.modelPlayer2.attachChild(this.camNode2);
this.getRootNode().attachChild(modelPlayer2);
this.getPhysicsSpace().add(modelPlayer2);
this.modelPlayer2.setLocalTranslation(<startpoint player 2>);
}
@Override
public Camera getCamera() {
return <which camera shall be currently focused>;
}
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = this.getCamera().getDirection().clone()
.multLocal(0.3f);
Vector3f camLeft = this.getCamera().getLeft().clone().multLocal(0.2f);
Vector3f walkDirection = new Vector3f(Vector3f.ZERO);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
// So I can now move modelPlayer1/2 and the camera will follow right?
// And how do I focus the one of those cameras?
// With one camera I do currently this: (but that doesn't work out that well in some cases:
// this.currentPlayer.setWalkDirection(walkDirection);
// this.currentPlayer.setViewDirection(cam.getDirection());
// cam.setLocation(this.currentPlayer.getPhysicsLocation());
}
}
[/java]
The question is what do I move in SimpleUpdate? And how do I switch the focus between those cameras?