jPCT-AE - a 3d engine for Android > Support

how to rotate camera around object???

(1/2) > >>

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:


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

--- End code ---

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:
I don't get the difference... ???

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.

--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version