I created a camera that floats around a target. When I test it object in the scene disappear and reappear when i move around the object.
Video: http://www.sharebigfile.com/file/191885/javaw-2007-07-11-21-15-09-93-avi.html
package entropy.input.camera;
import com.jme.math.FastMath;
import com.jme.renderer.Camera;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
public class ChaseCamera implements FocusedCamera {
Camera cam;
Node target;
float elevation = 0;
float rotation = 0;
float distance = 0;
public ChaseCamera(Camera cam, Node target){
this.cam = cam;
this.target = target;
}
public float getDistance(){
return distance;
}
public void setDistance(float distance){
this.distance = distance;
}
public void changeDistance(float delta){
distance += delta;
}
public float getElevation(){
return elevation;
}
public void setElevation(float elevationAngle){
this.elevation = elevationAngle;
}
public void changeElevation(float deltaAngle){
elevation += deltaAngle;
}
public float getRotation(){
return rotation;
}
public void setRotation(float rotationAngle){
this.rotation = rotationAngle;
}
public void changeRotation(float deltaAngle){
rotation += deltaAngle;
}
public void update(){
System.out.println("updating");
elevation = clamp(elevation,MAX_ROT,MIN_ROT);
distance = clamp(distance,MAX_DIST,MIN_DIST);
tempVa.set(0,0,-distance);
//Elevation above target
tempVb.x = tempVa.x;
tempVb.y = FastMath.cos(elevation)*tempVa.y - FastMath.sin(elevation)*tempVa.z;
tempVb.z = FastMath.sin(elevation)*tempVa.y + FastMath.cos(elevation)*tempVa.z;
tempVa.set(tempVb);
//Rotation around target
tempVb.x = FastMath.sin(rotation)*tempVa.z + FastMath.cos(rotation)*tempVa.x;
tempVb.y = tempVa.y;
tempVb.z = FastMath.cos(rotation)*tempVa.z - FastMath.sin(rotation)*tempVa.x;
tempVa.set(tempVb);
tempVa.addLocal(target.getWorldTranslation());
cam.setLocation(tempVa);
cam.setDirection(tempVb.negateLocal().normalize());
cam.update();
}
public static float clamp(float value, float max, float min){
if(value>max)
value = max;
if(value<min)
value = min;
return value;
}
}
Test code is a modification of TestObjectWalking
I chopped out the most boring parts, parts that were unchanged from TestObjectWalking.
...........................................
Node maggieNode = new Node("maggie");
maggieNode.setLocalTranslation(new Vector3f(100,terrain.getHeight(100, 100),100));
maggieNode.attachChild(maggie);
// Attach Children
pickNode = new Node("Pick");
pickNode.attachChild(maggieNode);
....................................
//camResults.setCheckDistance(true);
//cam.setLocation(new Vector3f(50, terrain.getHeight(50,50)+10, 50));
//cam.setDirection(new Vector3f(0.5f,0,0.5f));
//cam.setLeft(new Vector3f(0.5f,0,-0.5f));
//cam.update();
//oldCamLoc = new Vector3f(cam.getLocation());
//pickNode.lockBounds();
//pickNode.lockTransforms();
//OnBoardCamera obc = new OnBoardCamera(cam,maggieNode);
obc = new ChaseCamera(cam,maggieNode);
input = new KeyCameraHandler(obc,null);
//input = new MouseCameraHandler(obc,null);
}
.......................
// This is called every frame. Do changing of values here.
protected void simpleUpdate() {
obc.update();
//lock camera to objects
//camRay.getOrigin().set(cam.getLocation());
//camResults.clear();
//pickNode.calculatePick(camRay, camResults);
}
}