Пример #1
0
    HeightMapCollider::HeightMapCollider (csBulletDynamicsSystem* dynSys,
                                          iBody* csBody,
                                          csLockedHeightData gridData,
                                          int gridWidth, int gridHeight,
                                          csVector3 gridSize,
                                          csOrthoTransform transform,
                                          float minimumHeight, float maximumHeight)
        : dynSys (dynSys)
    {
        // Check if the min/max have to be computed
        bool needExtremum = minimumHeight == 0.0f && maximumHeight == 0.0f;
        if (needExtremum)
            minimumHeight = maximumHeight = gridData.data[0];

        // Initialize the terrain height data
        heightData = new float[gridHeight * gridWidth];
        for (int i = 0; i < gridWidth; i++)
            for (int j = 0; j < gridHeight; j++)
            {
                float height = heightData[(gridWidth - i - 1) * gridWidth + j]
                               = gridData.data[i * gridWidth + j];

                if (needExtremum)
                {
                    minimumHeight = MIN (minimumHeight, height);
                    maximumHeight = MAX (maximumHeight, height);
                }
            }

        // Create the terrain shape
        shape = new btHeightfieldTerrainShape (gridWidth, gridHeight,
                                               heightData, 1.0f, minimumHeight, maximumHeight,
                                               1, PHY_FLOAT, false);

        // Apply the local scaling on the shape
        btVector3 localScale (gridSize[0] * dynSys->internalScale / (gridWidth - 1),
                              dynSys->internalScale,
                              gridSize[2] * dynSys->internalScale / (gridHeight - 1));
        shape->setLocalScaling (localScale);

        // Set the origin to the middle of the heightfield
        csVector3 offset (gridSize[0] * 0.5f,
                          (maximumHeight - minimumHeight) * 0.5f + minimumHeight,
                          gridSize[2] * 0.5f);
        transform.SetOrigin (transform.GetOrigin () + transform.This2OtherRelative (offset));
        btTransform tr = CSToBullet (transform, dynSys->internalScale);

        // Create the rigid body and add it to the world
        body = new btRigidBody (0, 0, shape, btVector3 (0, 0, 0));
        body->setWorldTransform (tr);
        body->setUserPointer (csBody);
        dynSys->bulletWorld->addRigidBody (body);
    }
Пример #2
0
void CollisionMesh::FullUpdate()
{
    assert(m_parent);

    // Modify the scale of the collision mesh
    // depending on the parent scale
    D3DXVECTOR3 scale(FindLocalScale());
    D3DXVECTOR3 localScale(m_localWorld.GetScale());
    if(scale != localScale)
    {
        LoadCollisionModel(scale);
    }

    //DirectX: World = LocalWorld * ParentWorld
    m_positionDelta += m_parent->Position() - m_world.Position();
    m_world.Set(m_localWorld.GetMatrix()*m_parent->GetMatrix());
    m_position = m_world.Position();
    m_requiresFullUpdate = true;
}