Camera rotates and facing an object by key input

Hello guys,



I´m facing a problem about the best way to rotate a camera around one object. Before had the idea to post here, I checked out the post “Camera woes.” from madjack and I analyzed the TestLightRadius example.



My goal is just to rotate the camera facing always one object. So, when I keep A key pressed, for example, the camera rotates to left, D key instead of A key its rotates to right side. It´s sounds pretty simple, but it´s burning my mind away!



The first picture shows up how the problem is today from perception:



http://img.photobucket.com/albums/v413/Grinvon/cam_issues01.png



This picture shows up how my intention about the cameras behavior should be.



http://img.photobucket.com/albums/v413/Grinvon/cam_issues02.png



I don´t realize if chase camera could be a good point at all. I had tried using Quaterion:



[java] //turn the waypoints to left side

Quaternion right = new Quaternion();

right.fromAngleAxis( - (FastMath.HALF_PI / 30) * tpf * 64, Vector3f.UNIT_Y);

Vector3f rotationResult = right.mult(cam.getLocation());

cam.setLocation(rotationResult);

cam.lookAt(central.getLocalTranslation(), new Vector3f(0, 1, 0));[/java]



and using cos and sin, like as the following example:



[java] //turn the waypoints to left side

angle += tpf;

angle %= FastMath.TWO_PI;



cam.setLocation(new Vector3f(FastMath.cos(angle) * 9f, 7, FastMath.sin(angle) * 9f));

cam.lookAt(central.getLocalTranslation(), new Vector3f(0, 1, 0));

[/java]

But the problem still remains, I think as the second picture figures out the problem can be solved. Maybe put the focused object exactly in the middle of camera circle I can rotate and facing the camera as desired with precision.



I know where the object are, but I´m missing something…



Any hints will be very welcome. Thanks in advance.

did you export this from blender? if so make sure it is in the middle

Also try it with a simple shape to see if you experience the same problem.

http://hub.jmonkeyengine.org/groups/graphics/forum/topic/rotating-geometry-towards-a-point



http://hub.jmonkeyengine.org/groups/general-2/forum/topic/rotate-camera-with-fixed-lookat





and some of my testcodes, maybe will help(this one work for me rotating around object and looking at it all time)







[java] @Override

public boolean keyAnalog(String key) {

if (viewType.equals("lock")) {

if (key.equals("w")) {

System.out.println("kea w analog");

} else if (key.equals("s")) {

System.out.println("kea s analog");

} else if (key.equals("a")) {

System.out.println("kea a analog");

rotation++;

} else if (key.equals("d")) {

System.out.println("kea d analog");

rotation–;

}

}

return false;

}[/java]



[java] private class FollowCamera {

private float distance;

private float bok;

private float slope;

FollowCamera(float distance, float slope){

setDistance(distance);

setSlope(slope);

}

public final void setDistance(float distance){

this.distance = distance;

bok = (float)Math.sqrt(distancedistance/2);

}

public final float getDistance(){

return distance;

}

public final void setSlope(float slope){

this.slope = slope;

}

public final float getSlope(){

return slope;

}

public Vector3f getDistanceVector() {

return (new Vector3f(0f, bok
slope, bok/slope));

}

}[/java]





[java] @Override

public void update(float tpf) {

//////////////////////////////

if (!getFollowingObject().equals("")) {



Vector3f loc = world.getObject(getFollowingObject()).ObjectList.get(0).node.getLocalTranslation();



camera.setLocation(loc);

Quaternion leftRotation = new Quaternion();

leftRotation.fromAngleAxis((-FastMath.HALF_PI / 20) * rotation, Vector3f.UNIT_Y);

Vector3f rotationResult = loc.add(leftRotation.mult(followCamera.getDistanceVector()));

camera.setLocation(rotationResult);

camera.lookAt(loc, Vector3f.UNIT_Y);

camNode.getControlDir() + " loc: " + camNode.getLocalTranslation());

rotation = 0;



}





}[/java]





edit: if this will not help then you have diffrent Center vector. Just be sure to have object in 0.0.0 point in blender

If you want to check that where the models middle point is, do alt+g and reset the position.

Then it will move the selected object to its center. If your object isn’t centerd after that, then go in edit mode, ctrl+a and move all verts into

the center of blenders grid.



But for this, you should really use chasecam. It’s incredible easy for this actually.

Just do:



Node ObjectToCenterAround;// Your object that you wish to rotate around.

ChaseCamera chaseCam = new ChaseCamera(app.getCamera(), ObjectToCenterAround ,app.getInputManager());

App is your main game that extends SimpleApplication.



Gl.

Thank you guys for the reply.



wezrule



To be honest I did a map with some boxes. One of them is in the middle of the map, So when I rotate the camere horizontally all the map suppose to rotate. I wonder to make a similar camera from Disgaea like game, pressing R1 or L1 button (PS2 control) its rotate.



oxplay2



Your code is clean and easy to understand, but the problem is to facing only an object from 0,0,0 coordinate. I have a map and each “#” is an object and “@” is the object focused by the camera: e.g:



######

######

##@##

######

######



Maybe if I get your code and try to develop a path to camera follows without a specific point from 0,0,0 could be work.



Addez



Yeah I tried, but I guess chase cam is good to use when your focused object can moves around some floor or something like that.



Thanks guys!