Author Topic: Rotate camera around point  (Read 4578 times)

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Rotate camera around point
« 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.
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Rotate camera around point
« Reply #1 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.

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Re: Rotate camera around point
« Reply #2 on: February 12, 2011, 09:11:37 pm »
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

Offline icarusfactor

  • int
  • **
  • Posts: 58
    • View Profile
Re: Rotate camera around point
« Reply #3 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


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Rotate camera around point
« Reply #4 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);

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Re: Rotate camera around point
« Reply #5 on: February 13, 2011, 10:44:34 pm »
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

Offline Socke

  • byte
  • *
  • Posts: 17
    • View Profile
Re: Rotate camera around point
« Reply #6 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)


 

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Rotate camera around point
« Reply #7 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.