User Tools

Site Tools


Collision Objects

SpeedTree uses capsules as collision objects, providing a cruder representation of a tree object's shape than its detailed geometry. They have no polygonal resolution associated with them. Rather, they are mathematical representations, free to be passed into the collision-handling system in your application.

The following code shows how to access the collision objects embedded in a CCore object, assuming it has been loaded.

using namespace SpeedTree;
 
///////////////////////////////////////////////////////////////////////  
//  PrintCollisionObjects
 
void PrintCollisionObjects(const CCore* pTree)
{
    if (pTree)
    {
        st_int32 nNumObjects = 0;
        const SCollisionObject* pObjects = pTree->GetCollisionObjects(nNumObjects);
        if (pObjects && nNumObjects > 0)
        {
            for (st_int32 i = 0; i < nNumObjects; ++i)
            {
                const SCollisionObject* pObject = pObjects + i;
 
                printf("collision object [%d of %d]\n", i + 1, nNumObjects);
 
                // identify object type by checking center values
                if (pObject->m_vCenter1 == pObject->m_vCenter2)
                    printf("  [Sphere]\n");
                else
                    printf("  [Capsule]\n");
 
                printf("  center 1: (%g, %g, %g)\n", 
                    pObject->m_vCenter1.x, pObject->m_vCenter1.y, pObject->m_vCenter1.z);
                printf("  center 2: (%g, %g, %g)\n", 
                    pObject->m_vCenter2.x, pObject->m_vCenter2.y, pObject->m_vCenter2.z);
                printf("  radius: %g\n", pObject->m_fRadius);
 
                // note the necessary (const char*) typecast
                printf("  user data: [%s]\n", (const char*) pObject->m_pUserString);
            }
        }
    }
}

The CForest object (in the Forest library) has a function, CollisionAdjust(), that will provide the functionality necessary to avoid running through a series of tree models. It works only within the SpeedTree framework but could serve as a useful start for your own application.