www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: Kaiidyn on February 12, 2011, 04:21:26 pm

Title: Rotate camera around point
Post by: Kaiidyn on February 12, 2011, 04:21:26 pm
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
Code: [Select]
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.
Title: Re: Rotate camera around point
Post by: EgonOlsen on February 12, 2011, 08:26:27 pm
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.
Title: Re: Rotate camera around point
Post by: Kaiidyn on February 12, 2011, 09:11:37 pm
I'm probably being stupid, but I don't get it...  ???
Title: Re: Rotate camera around point
Post by: icarusfactor on February 13, 2011, 09:08:48 pm
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

Title: Re: Rotate camera around point
Post by: EgonOlsen on February 13, 2011, 09:45:30 pm
That's another option. What i meant was something like this:

Code: [Select]
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);
Title: Re: Rotate camera around point
Post by: Kaiidyn on February 13, 2011, 10:44:34 pm
yay, its working now.. thanks you two :)
Title: Re: Rotate camera around point
Post by: Socke on November 11, 2012, 07:31:28 pm
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)


 (http://www.bilder-hochladen.net/files/thumbs/45u7-34-2723.png) (http://www.bilder-hochladen.net/files/45u7-34-2723-png.html)
Title: Re: Rotate camera around point
Post by: EgonOlsen on November 11, 2012, 08:58:29 pm
Replacing the cam.rotateY(...); parts with something like

Code: [Select]
cam rotateAxis(cam.getYAxis(), ...) ;

might work in that case.