Hello!
I'm working on a school project and I'm new to JME.
I'm trying to achieve this type of camera handling using the mouse:
http://pv3d.org/2008/11/19/dragging-mouse-for-camera-orbit/
(click on the picture to see the example in runtime)
I've tried to port the code from actionscript to java but it did not work and the camera went bananas
Would appreciate if anyone could help me out. Also tried chase camera but i dont really know how to set it up properly and size my objects i want to rotate around is static, its not what i need.
I'm using a pivot and CameraNode to rotate the camera.
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.input.MouseInput;
import com.jme.input.MouseInputListener;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
public class TestOrbitingCamera extends SimpleGame {
boolean isOrbiting = false;
float camX = 0;
float camY = 0;
protected int camDistance = 10;
private Node pivotNode;
@Override
protected void simpleInitGame() {
// remove existing SimpleGame input handler
input.removeAllFromAttachedHandlers();
Sphere center = new Sphere("center", 15, 15, 3);
center.setModelBound(new BoundingSphere());
center.updateModelBound();
rootNode.attachChild(center);
Sphere s1 = new Sphere("center", 15, 15, 1);
s1.setLocalTranslation(-5, 4, 0);
s1.setModelBound(new BoundingSphere());
s1.updateModelBound();
rootNode.attachChild(s1);
Sphere s2 = new Sphere("center", 15, 15, 1);
s2.setLocalTranslation(7, 5, 5);
s2.setModelBound(new BoundingSphere());
s2.updateModelBound();
rootNode.attachChild(s2);
// to have a reference
Box b = new Box("floor", new Vector3f(-5, -10, -5), 10, 0.3f, 10);
b.setModelBound(new BoundingBox());
b.updateModelBound();
rootNode.attachChild(b);
MouseInput.get().addListener(new MouseInputListener() {
@Override
public void onWheel(int wheelDelta, int x, int y) {
}
@Override
public void onMove(int xDelta, int yDelta, int newX, int newY) {
if (isOrbiting) {
float speed = 100f;
camX += xDelta*tpf*speed;
camY += yDelta*tpf*speed;
// rotate the pivot node
pivotNode.getLocalRotation().fromAngles(camY*FastMath.DEG_TO_RAD, camX*FastMath.DEG_TO_RAD, 0);
}
}
@Override
public void onButton(int button, boolean pressed, int x, int y) {
if (button == 0 && pressed) {
isOrbiting = true;
}
if (button == 0 && pressed == false) {
isOrbiting = false;
}
}
});
// create a pivot Node which will be rotated by the mouse input
pivotNode = new Node("pivot");
rootNode.attachChild(pivotNode);
// create a camera node which is attached to the pivot node and translated by the viewing distance
CameraNode camNode = new CameraNode("camNode", cam);
camNode.setLocalTranslation(0, 0, -30);
pivotNode.attachChild(camNode);
}
public static void main(String[] args) {
SimpleGame game = new TestOrbitingCamera();
game.setConfigShowMode(ConfigShowMode.AlwaysShow);
game.start();
}
}
Just as a small improvement, you can implement the onWheel method with something like:
public void onWheel(int wheelDelta, int x, int y) {
if(wheelDelta > 0) {
camNode.setLocalTranslation(camNode.getLocalTranslation().mult(aproxFactor));
}
else {
camNode.setLocalTranslation(camNode.getLocalTranslation().mult(moveAwayFactor));
}
}
You can use aproxFactor = 0.9f and moveAwayFactor = 1.1f to try it out :D
Thanks alot you guys!
Exactly what i was looking for? :mrgreen:
YOU ROCK!
hi,
is it possible to zoom with the mousewheel and to move the whole scene with the right mouse button?
thanks
Dca said:
hi,
is it possible to zoom with the mousewheel and to move the whole scene with the right mouse button?
thanks
Isn't that what the code by TaviO allready does? :)