Set heightfield origin while accounting for local scaling?

DestroyerOfCities
Posts: 20
Joined: Fri Dec 10, 2010 3:39 am

Set heightfield origin while accounting for local scaling?

Post by DestroyerOfCities »

I'm having some success using the btHeightfieldTerrainShape. I've been able to create patches that match my terrain, but I've had some issues with scaling/placing them.

I call setLocalScaling on the heightfield shape with a btVector3 that looks like (scale, 1.0, scale) to scale the terrain patch by the desired amount. I also create a btTransform, call setOrigin with the desired offset, and use it in a MotionState for creating the RigidBody that correlates to my heightfield shape in order to offset the patch.

The problem is, it seems that the offset is affected by the localScaling, in a way that isn't immediately obvious to me. I had to square the scale and then cut that in half to get a value that looked good, a formula I stumbled upon entirely by accident and which is only accurate at 0,0. I really need to understand how the scaling and rigid body origin stuff works with the heightfield shape, especially with the patches needing to be a power of 2 + 1 in size. I have to be able to place them in exactly the right place, and as is I'm just blindly trying to find some rhyme or reason to the scaling and offset.

I've studied the terrain demo, but it barely even sets an offset and doesn't shed any light on the inner workings of the heightfield. Can anyone offer me any insight into how to scale/offset a heightfield shape properly?
DestroyerOfCities
Posts: 20
Joined: Fri Dec 10, 2010 3:39 am

Re: Set heightfield origin while accounting for local scalin

Post by DestroyerOfCities »

Oh, I was confused! The heightfield gets centered and then scaled by the local scaling. So if I'm going to set the local scaling to (10, 1, 10), then I have to offset by 5 units. Simple as that.