static duk_ret_t Terrain_GetPoint_uint_uint(duk_context* ctx)
{
    Terrain* thisObj = GetThisWeakObject<Terrain>(ctx);
    uint x = (uint)duk_require_number(ctx, 0);
    uint y = (uint)duk_require_number(ctx, 1);
    float ret = thisObj->GetPoint(x, y);
    duk_push_number(ctx, ret);
    return 1;
}
Beispiel #2
0
void RigidBody::CreateHeightFieldFromTerrain()
{
    CheckForPlaceableAndTerrain();
    
    Terrain* terrain = impl->terrain;
    if (!terrain)
        return;
    
    uint width = terrain->PatchWidth() * Terrain::cPatchSize;
    uint height = terrain->PatchHeight() * Terrain::cPatchSize;
    if (!width || !height)
        return;
    
    impl->heightValues.Resize(width * height);
    
    float xzSpacing = 1.0f;
    float ySpacing = 1.0f;
    float minY = 1000000000;
    float maxY = -1000000000;
    for(uint z = 0; z < height; ++z)
        for(uint x = 0; x < width; ++x)
        {
            float value = terrain->GetPoint(x, z);
            if (value < minY)
                minY = value;
            if (value > maxY)
                maxY = value;
            impl->heightValues[z * width + x] = value;
        }

    float3 scale = terrain->nodeTransformation.Get().scale;
    float3 bbMin(0, minY, 0);
    float3 bbMax(xzSpacing * (width - 1), maxY, xzSpacing * (height - 1));
    float3 bbCenter = scale.Mul((bbMin + bbMax) * 0.5f);
    
    impl->heightField = new btHeightfieldTerrainShape(width, height, &impl->heightValues[0], ySpacing, minY, maxY, 1, PHY_FLOAT, false);
    
    /** \todo Terrain uses its own transform that is independent of the placeable. It is not nice to support, since rest of RigidBody assumes
        the transform is in the placeable. Right now, we only support position & scaling. Here, we also counteract Bullet's nasty habit to center 
        the heightfield on its own. Also, Bullet's collisionshapes generally do not support arbitrary transforms, so we must construct a "compound shape"
        and add the heightfield as its child, to be able to specify the transform.
     */
    impl->heightField->setLocalScaling(scale);
    
    float3 positionAdjust = terrain->nodeTransformation.Get().pos;
    positionAdjust += bbCenter;
    
    btCompoundShape* compound = new btCompoundShape();
    impl->shape = compound;
    compound->addChildShape(btTransform(btQuaternion(0,0,0,1), positionAdjust), impl->heightField);
}