Rotate camera around point

Started by Kaiidyn, February 12, 2011, 04:21:26 PM

Previous topic - Next topic

Kaiidyn

How do I rotate my camera around the player when moving my finger over the screen? (on all axis)
what I currently have is this
camPos.x = (float) (Math.sin(me.getX()%360 * (Math.PI/180)) * -1.5f);
camPos.z = (float) (Math.cos(me.getX() * (Math.PI/180)) * 2.25f);


but this does not take the player position into account.. and is probably plain bad maths =p (i suck at them lols)

Kind regards,
Kaiidyn.
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer's intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch

EgonOlsen

Using sin and cos yourself usually isn't needed. That's what the rotate methods are meant for. What should work: Rotate the camera around y, get the player position, get the z axis from the camera (or maybe x....depends on your scene), multiply the axis' length with some value, add it to the player's position and set the result as the new camera position. Depending on your scene swtup, it might be needed to do some additional rotations but the basic idea should work.

Kaiidyn

I'm probably being stupid, but I don't get it...  ???
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer's intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch

icarusfactor

I had the same problem, but looking through the forum I found "paulscode" had a great answer and fix for my problem, you have to setup a dummy object and attach your object(s) you want to pivot around to it or variation of. Here is the forum link with the basic code to work off of.

http://www.jpct.net/forum2/index.php/topic,1278.msg8728.html#msg8728


EgonOlsen

That's another option. What i meant was something like this:


Camera cam=world.getCamera();
SimpleVector cPos=cam.getPosition();
SimpleVector pos=box.getTransformedCenter();
cam.rotateY(0.01f);
SimpleVector dir=cam.getDirection();
dir.scalarMul(-60);
pos.add(dir);
pos.y=cPos.y; // Restore the height...might be not needed depending on the setup.
cam.setPosition(pos);

Kaiidyn

yay, its working now.. thanks you two :)
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer's intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch

Socke

It work only with no cam rotate in the X axis(image 2). But what todo if the cam look down to (getCamera().lookAt) a Object (image 1)?

(i am not use the android version  ;D)




EgonOlsen

Replacing the cam.rotateY(...); parts with something like


cam rotateAxis(cam.getYAxis(), ...) ;


might work in that case.