how to rotate camera around object???

Started by hamedf_hamedf, November 29, 2012, 02:09:08 PM

Previous topic - Next topic

hamedf_hamedf

Dear All,
i want to rotate camera around object that camera is look at it.
how?

Thanks

EgonOlsen

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:



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();
    }
}

hamedf_hamedf

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.

EgonOlsen


KittenKoder

Quote from: hamedf_hamedf 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.

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.

hamedf_hamedf

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)