www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: lapusneanugabi on February 26, 2012, 06:36:25 AM

Title: Camera path Follow
Post by: lapusneanugabi 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.
Title: Re: Camera path Follow
Post by: EgonOlsen 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.
Title: Re: Camera path Follow
Post by: lapusneanugabi 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());
   }