Author Topic: Camera path Follow  (Read 2368 times)

Offline lapusneanugabi

  • byte
  • *
  • Posts: 5
    • View Profile
Camera path Follow
« on: February 26, 2012, 06:36:25 am »
Hi!

I want to create a track race and i don't know how to set my camera to follow the position of car.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Camera path Follow
« Reply #1 on: February 26, 2012, 02:42:22 pm »
You can get the car's translation/transformed center as well as the rotated axes by using get?Axis(). With that, you should be able to locate the camera behind the car. You can look at the car example for desktop jPCT, which does something similar.

Offline lapusneanugabi

  • byte
  • *
  • Posts: 5
    • View Profile
Re: Camera path Follow
« Reply #2 on: February 26, 2012, 07:48:52 pm »
Thanks!
This code with some adjustment at some values  works perfectly.

 private void moveCamera() {
      SimpleVector oldCamPos=camera.getPosition();
      SimpleVector oldestCamPos=new SimpleVector(oldCamPos);
      oldCamPos.scalarMul(9f);

      SimpleVector carCenter=car.getTransformedCenter();
      SimpleVector camPos=new SimpleVector(carCenter);
      SimpleVector zOffset=car.getZAxis();
      SimpleVector yOffset=new SimpleVector(0, -100, 0);
      zOffset.scalarMul(-250f);

      camPos.add(zOffset);
      camPos.add(yOffset);

      camPos.add(oldCamPos);
      camPos.scalarMul(0.1f);

      SimpleVector delta=camPos.calcSub(oldestCamPos);
      float len=delta.length();

      if (len!=0) {
         /**
          * Do a collision detection between the camera and the ground to prevent the camera from
          * moving into the ground.
          */
         theWorld.checkCameraCollisionEllipsoid(delta.normalize(), new SimpleVector(20, 20, 20), len, 3);
      }

      /**
       * And finally: Look at the car
       */
      camera.lookAt(car.getTransformedCenter());
   }