camera following entity and staying directly behind it


I have a marker which I move along a pre-defined GPS route. Updating every second.

My aim is to follow the marker by looking at it so the camera stays behind and looks in he direction it is travelling.

I am calculating the heading of the marker and then using this as the heading of the camera using the following:

//markerHeading is a bearing in degrees.

var heading = Cesium.Math.toRadians(markerHeading);

camera.lookAt(center, new Cesium.HeadingPitchRange(heading, pitch, range));

It is not giving the results I would expect. The entity is centered fine, but sometimes the camera is in front of the marker i.e. the marker is moving towards it.

From the docs I read this " If the offset is heading/pitch/range, then the heading and the pitch angles are defined in the reference frame defined by the transformation matrix"

I am not 100% sure how I should interpret things from that. Any help/pointers would be great.


How are you calculating markerHeading? Are you certain that it matches the marker movement direction? Is there a SandCastle mockup to try?

Hi Thanks

I will get one added.

I checked the heading of the marker (330) ish degress.

When I fed this into the camera, i then immediately checked the camera heading and it was 5 degrees, so I am fairly certain I need to use something as a reference point (the transition matrix?).

330 degrees is about 5.76 radians, just wondering if you might be confusing 5.76 radians as 5.76 degrees?

Easy ways to convert between deg and rad:

rad/Math.PI*180 = deg

deg/180*Math.PI = rad

Also it’s possible that 330 degrees doesn’t match the velocity vector direction, so it’s possible that the camera is facing the direction you tell it, but perhaps you’re telling it the wrong direction in some cases?


I have solved this. Embarrassingly I had mixed up latitude and longitude! I have now got it following directly behind my marker.

One thing I noticed though is its not that smooth. I re-position the camera every second (when the marker moves). So it feels a bit sticky. Is there a way to make this smoother and more fluid?


You can move the camera target each frame. If the markers are not far from eachother linear motion should work well. Here’s some psuedo code that might work (once converted to real code of course)



//get unit direction from previous marker to next marker

//get distance from camera target to next marker

//get time_left from now to when the camera target needs to be at next marker

//speed is distance/time_left (meters / second)

//get frametime, convert from ms to seconds

//distance to move this frame is speed * frametime

//move camera target in the direction_from_previous_marker_to_next_marker through that distance


Cesium provides some functions for following a target as well, complete with fancy interpolations. Here’s cool demo that Matt posted

Though the camera angle is relative to the ENU frame. I don’t know if there’s an option to make it relative to the model rotation frame.