Camaera attached to model shakes when far away from origin

I have an application where the user drives a car on a network of roads in a 3D environment which can sometimes stretch over several kilometers. At the start of a simulation, the camera is attached to a 3D model of the vehicle the user is driving in, and the camera is positioned such that the user looks through the front window from behind the steering wheel. Screens on the left and right have the cameras positioned such that the out of the window view is to left and to right. This is the code for the forward view :

base.camera.reparentTo( self.MainTarget.MainCarModel )
base.camera.setPos(self.dlat, self.dlon, self.height)

So this is done only once.
During the simulation, the 3D model of the vehicle is moved by updating the position and angles (heading etc) each frame:
self.MainTarget.MainCarModel.setPosHpr( x, y, z, h, p, r )

This works fine, however, when the vehicle is at a large distance (more than 5 km) from the origin [0,0] the model is shaking, first a little bit and hardly noticable, but the shaking becomes worse as the distance from the origin increases. The out of the window environment is moving very smoothly, and framerate is always 60 Hz. Only the model of the vehicle the user is sitting in is shaking, and this seems to be clearly related to the distance from the origin.

Does anyone have an idea what could be the cause of this? Maybe something to do with rounding errors?

I’m guessing that it’s the result of floating-point accuracy issues: beyond a certain range, floating point numbers lose small-scale precision, I gather. Put another way, the larger the floating-point number, the less space it has to represent smaller elements of the number.

I haven’t worked with this myself, but one solution that I’ve seen mentioned, as I recall, is to shift your world around such that the camera can remain always relatively-near to the zero-position.

I don’t know the specifics over-well, but I’ve seen it discussed here and elsewhere, I believe, so if no-one here has an answer to hand some searching should provide one!

1 Like

You can also compile Panda in double-precision floating-point mode, with --override STDFLOAT_DOUBLE=1 to makepanda.py, but this will have an impact on performance.

Otherwise, you need to make sure that the scene graph path from your camera to the models you’re currently near doesn’t make huge jumps in position. You need to structure your scene graph to allow for this. An easy way to do this is to organise your scene into “zones” and have all objects and also the camera parented to that zone. Then you can move around your zones relative to render such that the currently active zone is nearest to the world origin.

1 Like