Camera path Follow

Started by lapusneanugabi, February 26, 2012, 06:36:25 AM

Previous topic - Next topic

lapusneanugabi

Hi!

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

EgonOlsen

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.

lapusneanugabi

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