Hello, am trying to get my camera to rotate around the middle of the screen but… lest just say the rotation don’t work so well.
I have been seeking on the forum but never realy found what i was looking for. Right now my code look like this,
if(name.equals("TurnLeft"))
{
CollisionResults results = new CollisionResults();
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
app.getRootNode().collideWith(ray, results);
System.out.println("----- Collisions? " + results.size() + "-----");
CollisionResult closest ;
if (results.size() > 0) {
closest = results.getClosestCollision();
closest.getContactPoint() ;
Vector3f forward = new Vector3f(0,1,1);
cam.setLocation(cam.getLocation().add(0,(float)Math.sin(turn),(float)Math.cos(turn)));
cam.lookAt(new Vector3f(closest.getContactPoint()),cam.getRotation().mult(forward));
turn += 0.01 ;
// System.out.println(new Vector3f(turn,1,turn)) ;
} else {
}
}
I can already shot my ray in the middle of the screen to make sure i will still look at the same point but the camera rotation is realy buggy… any suggestion?
Hi. Can you elaborate on what exactly it is you want to achieve? Perhaps talk us through the code above and reflect on where you think the problem might be? Eg. when you say “get my camera to rotate around the middle of the screen”, what does that mean in more detail?
What i want is to be able to rotate around the middle of the screen, so i can turn the camera BUT i will still look at the same point, all the time. I did try to use the look at and then move the camera around but… it don’t work so well. Here is the code am using right now, but it don’t do the job i realy want, because it will rotate the camera around itself, and not a certain point
Well, when the camera moves around at point, the operative word is “moves”… ie: you will have to move the camera and not just rotate it. You will have to inverse rotate the camera position relative to whatever you are rotating around and then set the new location.
By the way, this:
[java]
Matrix3f mat = new Matrix3f();
mat.fromAngleNormalAxis(rotationSpeed * value * tpf, axis);
Quaternion q = new Quaternion();
q.fromAxes(leftDir, upDir, dir);
q.normalizeLocal();
[/java]
Is the same as:
[java]
Quaternion rotate = new Quanternion().mat.fromAngleNormalAxis(rotationSpeed * value * tpf, axis);
Quaternion q = rotate.mult(camera.getRotation());
camera.setRotation(q);
[/java]
I took most of this code from the forum, so this is not from me and this is the only code i could find on the site. I could correct it on my front so that people can take it now but ya, it should probably be corrected there to : http://hub.jmonkeyengine.org/forum/topic/rotate-camera-around-point/
Plus, the basic cam from the simpleApplication (app.getCamera) don’t have a move option. That would actully make my life way easier, but as i said the only way to move it is the set position and it don’t work so well for me, probably cause my calculation is wrong but still, this is the point of this post.
All i want to do is to maintaint the lookAt on the middle of the screen yet move the camera around, like in circle, but it won’t work, my sin and cos don’t do anything right, my lookat is flipping around and i don’t underthand what the hell is going on.
@SimonBedard said:
I took most of this code from the forum, so this is not from me and this is the only code i could find on the site. I could correct it on my front so that people can take it now but ya, it should probably be corrected there to :
http://hub.jmonkeyengine.org/forum/topic/rotate-camera-around-point/
Plus, the basic cam from the simpleApplication (app.getCamera) don’t have a move option. That would actully make my life way easier, but as i said the only way to move it is the set position and it don’t work so well for me, probably cause my calculation is wrong but still, this is the point of this post.
All i want to do is to maintaint the lookAt on the middle of the screen yet move the camera around, like in circle, but it won’t work, my sin and cos don’t do anything right, my lookat is flipping around and i don’t underthand what the hell is going on.
I can’t see that code so I can’t say.
The basic gist I already said. Calculate the camera position relative to what you are rotating around. Rotate that camera position. Then turn that back into world space and set it to the camera.