Author Topic: how to rotate camera around object???  (Read 5477 times)

Offline hamedf_hamedf

  • byte
  • *
  • Posts: 19
    • View Profile
how to rotate camera around object???
« on: November 29, 2012, 02:09:08 pm »
Dear All,
i want to rotate camera around object that camera is look at it.
how?

Thanks

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: how to rotate camera around object???
« Reply #1 on: November 29, 2012, 03:07:07 pm »
Maybe this little example taken from an older thread helps. It's for desktop jPCT and deals with "grabbing" an object like in google earth (hence the class name). But it might help to get you started:

Code: [Select]

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;

import com.threed.jpct.*;
import com.threed.jpct.util.Light;


/**
 * A Simple demo for mouse-powered object viewing. It offers two modes: Camera around object (default) and object
 * "around" camera.
 */
public class GoogleCube
{

  private static float distance = 50;

  // If true, the object transforms related to the camera. If false, the camera to the object.
  private static boolean affectObject = false;


  public static void main(String[] args)
    throws Exception
  {
    World world = new World();
    FrameBuffer buffer = new FrameBuffer(640, 480, FrameBuffer.SAMPLINGMODE_HARDWARE_ONLY);
    buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
    buffer.enableRenderer(IRenderer.RENDERER_OPENGL);
    Object3D cube = Primitives.getSphere(15);
    world.addObject(cube);
    cube.build();
    Camera cam = world.getCamera();
    cam.moveCamera(Camera.CAMERA_MOVEUP, distance);
    cam.rotateCameraX((float) Math.PI / 2f);
    world.setAmbientLight(100, 100, 100);
    Light light = new Light(world);
    light.setIntensity(0, 0, 255);
    light.setPosition(new SimpleVector(-100, 0, 0));

    Mouse.create();

    while (!Display.isCloseRequested() && !Mouse.isButtonDown(1))
    {
      buffer.clear();
      world.renderScene(buffer);
      world.draw(buffer);
      buffer.update();
      buffer.displayGLOnly();

      int x = Mouse.getDX();
      int y = Mouse.getDY();
      int w = Mouse.getDWheel();

      if (Mouse.isButtonDown(0))
      {
        SimpleVector line = new SimpleVector(x, 0, y);
        Matrix m = line.normalize().getRotationMatrix();
        if (affectObject)
        {
          // Object "around" camera
          cube.rotateAxis(m.getXAxis(), line.length() / 200f);
        }
        else
        {
          // Camera around object
          m.rotateAxis(m.getXAxis(), (float) -Math.PI / 2f);
          cam.moveCamera(Camera.CAMERA_MOVEIN, distance);
          cam.rotateAxis(m.invert3x3().getXAxis(), line.length() / 200f);
          cam.moveCamera(Camera.CAMERA_MOVEOUT, distance);
        }
      }
      if (w != 0)
      {
        float d = w / 200f;
        if (affectObject)
        {
          cube.translate(0, -d, 0);
        }
        else
        {
          distance -= d;
          cam.moveCamera(Camera.CAMERA_MOVEIN, d);
        }
      }

      Thread.sleep(10);
    }
    Mouse.setGrabbed(false);
    Mouse.destroy();

    buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
    buffer.dispose();
    }
}

Offline hamedf_hamedf

  • byte
  • *
  • Posts: 19
    • View Profile
Re: how to rotate camera around object???
« Reply #2 on: November 29, 2012, 03:26:24 pm »
Dear Egonolsen,

this code rotate camera around itself (cam.rotateAxis) :(
but i want to rotate camera by center of object, like earh(camera) around Sun(object). and camera look at it.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: how to rotate camera around object???
« Reply #3 on: November 29, 2012, 04:21:46 pm »
I don't get the difference... ???

Offline KittenKoder

  • int
  • **
  • Posts: 66
  • I Am No One Else
    • View Profile
    • Kitt Games Wiki
Re: how to rotate camera around object???
« Reply #4 on: November 29, 2012, 05:07:22 pm »
Dear Egonolsen,

this code rotate camera around itself (cam.rotateAxis) :(
but i want to rotate camera by center of object, like earh(camera) around Sun(object). and camera look at it.

A radial camera, or that's what I call it. Now, here's the method that works in jPCT:

1. Translate the camera to the location the object is, matching centers.
2. Rotate the camera's y by the angle you want it to be, and x if you're doing that as well.
3. Get the camera's x axis.
4. Translate the camera along that axis the distance you want it to remain.

Done.
When life throws you lemons, make lemon juice, then drop life into a pile of razors and pour the lemon juice over it.

Offline hamedf_hamedf

  • byte
  • *
  • Posts: 19
    • View Profile
Re: how to rotate camera around object???
« Reply #5 on: December 02, 2012, 08:32:39 am »
my exactly question is:

I have one point with X, Y and Z coordinates. (camera position in World space)
And now I want to rotate it, by specifying the rotation in three constraint:
1-By user-defined degree
2-By user-defined axis of rotation (in world space)
3-Around (relative to) user-defined point
(if you can imagine this rotation is: a point rotate on circle of the cone, the head of the cone is number3 and normal vector of the cone is number2)

Offline hamedf_hamedf

  • byte
  • *
  • Posts: 19
    • View Profile
Re: how to rotate camera around object???
« Reply #6 on: December 02, 2012, 01:55:13 pm »
This site is good, i solved it  :)
http://en.wikipedia.org/wiki/Rotation_matrix