Ignore:
Timestamp:
Sep 7, 2012, 5:00:31 PM (8 years ago)
Author:
lolbot
Message:

fixed 23 files out of 277:

  • fixed 1270 CR characters
  • fixed 56 trailing spaces
  • fixed 5085 tabs
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/Physics/Src/EasyPhysics.cpp

    r1782 r1888  
    3030//--
    3131
    32 EasyPhysic::EasyPhysic(WorldEntity* NewOwnerEntity) : 
    33         m_collision_object(NULL),
    34         m_ghost_object(NULL),
    35         m_rigid_body(NULL),
    36         m_local_inertia(btVector3(.0f, .0f, .0f)),
    37         m_collision_shape(NULL),
    38         m_convex_shape(NULL),
    39         m_motion_state(NULL),
    40         m_mass(.0f),
    41         m_collision_group(1),
    42         m_collision_mask(1),
    43         m_owner_entity(NewOwnerEntity),
    44         m_owner_simulation(NULL),
    45         m_base_physic(NULL)
     32EasyPhysic::EasyPhysic(WorldEntity* NewOwnerEntity) :
     33    m_collision_object(NULL),
     34    m_ghost_object(NULL),
     35    m_rigid_body(NULL),
     36    m_local_inertia(btVector3(.0f, .0f, .0f)),
     37    m_collision_shape(NULL),
     38    m_convex_shape(NULL),
     39    m_motion_state(NULL),
     40    m_mass(.0f),
     41    m_collision_group(1),
     42    m_collision_mask(1),
     43    m_owner_entity(NewOwnerEntity),
     44    m_owner_simulation(NULL),
     45    m_base_physic(NULL)
    4646{
    4747}
     
    4949EasyPhysic::~EasyPhysic()
    5050{
    51         m_rigid_body = NULL;
    52         delete m_collision_object;
    53         delete m_collision_shape;
    54         delete m_motion_state;
     51    m_rigid_body = NULL;
     52    delete m_collision_object;
     53    delete m_collision_shape;
     54    delete m_motion_state;
    5555}
    5656
     
    6161void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape)
    6262{
    63         bool bReinitToRigidBody = false;
    64         if (m_rigid_body)
    65         {
    66                 bReinitToRigidBody = true;
    67                 delete m_rigid_body;
    68         }
    69         if (m_collision_shape)
    70                 delete m_collision_shape;
    71 
    72         m_collision_shape = collision_shape;
    73 
    74         if (bReinitToRigidBody)
    75                 InitBodyToRigid();
     63    bool bReinitToRigidBody = false;
     64    if (m_rigid_body)
     65    {
     66        bReinitToRigidBody = true;
     67        delete m_rigid_body;
     68    }
     69    if (m_collision_shape)
     70        delete m_collision_shape;
     71
     72    m_collision_shape = collision_shape;
     73
     74    if (bReinitToRigidBody)
     75        InitBodyToRigid();
    7676}
    7777
     
    7979void EasyPhysic::SetShapeToBox(lol::vec3& box_size)
    8080{
    81         vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
    82         m_convex_shape = new btBoxShape(LOL2BT_VEC3(new_box_size));
    83         SetShapeTo(m_convex_shape);
     81    vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
     82    m_convex_shape = new btBoxShape(LOL2BT_VEC3(new_box_size));
     83    SetShapeTo(m_convex_shape);
    8484}
    8585
    8686void EasyPhysic::SetShapeToSphere(float radius)
    8787{
    88         m_convex_shape = new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE);
    89         SetShapeTo(m_convex_shape);
     88    m_convex_shape = new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE);
     89    SetShapeTo(m_convex_shape);
    9090}
    9191
    9292void EasyPhysic::SetShapeToCone(float radius, float height)
    9393{
    94         m_convex_shape = new btConeShape(       radius * LOL2BT_UNIT,
    95                                                                                 height * LOL2BT_UNIT);
    96         SetShapeTo(m_convex_shape);
     94    m_convex_shape = new btConeShape(    radius * LOL2BT_UNIT,
     95                                        height * LOL2BT_UNIT);
     96    SetShapeTo(m_convex_shape);
    9797}
    9898
    9999void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size)
    100100{
    101         vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
    102         new_cyl_size.y *= LOL2BT_SIZE;
    103         m_convex_shape = new btCylinderShape(LOL2BT_VEC3(new_cyl_size));
    104         SetShapeTo(m_convex_shape);
     101    vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
     102    new_cyl_size.y *= LOL2BT_SIZE;
     103    m_convex_shape = new btCylinderShape(LOL2BT_VEC3(new_cyl_size));
     104    SetShapeTo(m_convex_shape);
    105105}
    106106
    107107void EasyPhysic::SetShapeToCapsule(float radius, float height)
    108108{
    109         m_convex_shape = new btCapsuleShape(radius * LOL2BT_UNIT * LOL2BT_SIZE,
    110                                                                                 height * LOL2BT_UNIT * LOL2BT_SIZE);
    111         SetShapeTo(m_convex_shape);
     109    m_convex_shape = new btCapsuleShape(radius * LOL2BT_UNIT * LOL2BT_SIZE,
     110                                        height * LOL2BT_UNIT * LOL2BT_SIZE);
     111    SetShapeTo(m_convex_shape);
    112112}
    113113
     
    119119mat4 EasyPhysic::GetTransform()
    120120{
    121         m_local_to_world = lol::mat4(1.0f);
    122         if (m_rigid_body && m_motion_state)
    123         {
    124                 btTransform CurTransform;
    125                 m_motion_state->getWorldTransform(CurTransform);
    126                 CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
    127         }
    128         else if (m_collision_object)
    129                 m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
    130         return m_local_to_world;
     121    m_local_to_world = lol::mat4(1.0f);
     122    if (m_rigid_body && m_motion_state)
     123    {
     124        btTransform CurTransform;
     125        m_motion_state->getWorldTransform(CurTransform);
     126        CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
     127    }
     128    else if (m_collision_object)
     129        m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
     130    return m_local_to_world;
    131131}
    132132
     
    134134void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
    135135{
    136         lol::mat4 PreviousMatrix = m_local_to_world;
    137         m_local_to_world = lol::mat4::translate(base_location) * lol::mat4(base_rotation);
    138 
    139         if (m_ghost_object)
    140                 m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
    141         else
    142         {
    143                 if (m_motion_state)
    144                         m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
    145                 else
    146                         m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
    147         }
    148 
    149         for (int i = 0; i < m_based_physic_list.Count(); i++)
    150         {
    151                 if (m_based_physic_list[i])
    152                         m_based_physic_list[i]->BaseTransformChanged(PreviousMatrix, m_local_to_world);
    153                 else
    154                         m_based_physic_list.Remove(i--);
    155         }
     136    lol::mat4 PreviousMatrix = m_local_to_world;
     137    m_local_to_world = lol::mat4::translate(base_location) * lol::mat4(base_rotation);
     138
     139    if (m_ghost_object)
     140        m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
     141    else
     142    {
     143        if (m_motion_state)
     144            m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
     145        else
     146            m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
     147    }
     148
     149    for (int i = 0; i < m_based_physic_list.Count(); i++)
     150    {
     151        if (m_based_physic_list[i])
     152            m_based_physic_list[i]->BaseTransformChanged(PreviousMatrix, m_local_to_world);
     153        else
     154            m_based_physic_list.Remove(i--);
     155    }
    156156}
    157157
     
    159159void EasyPhysic::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
    160160{
    161         mat4 PreviousMatrixLoc = ((m_base_lock_location)?(PreviousMatrix):(lol::mat4::translate(PreviousMatrix.v3.xyz)));
    162         mat4 PreviousMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(PreviousMatrix))):(lol::mat4(1.f)));
    163         mat4 NewMatrixLoc = ((m_base_lock_location)?(NewMatrix):(lol::mat4::translate(NewMatrix.v3.xyz)));
    164         mat4 NewMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(NewMatrix))):(lol::mat4(1.f)));
    165        
    166         if (m_ghost_object || (m_rigid_body->getCollisionFlags() & btCollisionObject::CF_KINEMATIC_OBJECT))
    167         {
    168                 mat4 ThisMatrixLoc = NewMatrixLoc * inverse(PreviousMatrixLoc) * lol::mat4::translate(m_local_to_world.v3.xyz);
    169                 mat4 ThisMatrixRot = NewMatrixRot * inverse(PreviousMatrixRot) * lol::mat4(lol::quat(m_local_to_world));
    170                 SetTransform(ThisMatrixLoc.v3.xyz, lol::mat4(lol::quat(ThisMatrixRot)));
    171         }
     161    mat4 PreviousMatrixLoc = ((m_base_lock_location)?(PreviousMatrix):(lol::mat4::translate(PreviousMatrix.v3.xyz)));
     162    mat4 PreviousMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(PreviousMatrix))):(lol::mat4(1.f)));
     163    mat4 NewMatrixLoc = ((m_base_lock_location)?(NewMatrix):(lol::mat4::translate(NewMatrix.v3.xyz)));
     164    mat4 NewMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(NewMatrix))):(lol::mat4(1.f)));
     165
     166    if (m_ghost_object || (m_rigid_body->getCollisionFlags() & btCollisionObject::CF_KINEMATIC_OBJECT))
     167    {
     168        mat4 ThisMatrixLoc = NewMatrixLoc * inverse(PreviousMatrixLoc) * lol::mat4::translate(m_local_to_world.v3.xyz);
     169        mat4 ThisMatrixRot = NewMatrixRot * inverse(PreviousMatrixRot) * lol::mat4(lol::quat(m_local_to_world));
     170        SetTransform(ThisMatrixLoc.v3.xyz, lol::mat4(lol::quat(ThisMatrixRot)));
     171    }
    172172}
    173173
     
    178178void EasyPhysic::SetMass(float mass)
    179179{
    180         m_mass = mass;
    181 
    182         if (m_rigid_body)
    183         {
    184                 SetLocalInertia(m_mass);
    185                 m_rigid_body->setMassProps(mass, m_local_inertia);
    186         }
     180    m_mass = mass;
     181
     182    if (m_rigid_body)
     183    {
     184        SetLocalInertia(m_mass);
     185        m_rigid_body->setMassProps(mass, m_local_inertia);
     186    }
    187187}
    188188
     
    194194void EasyPhysic::InitBodyToRigid(bool SetToKinematic)
    195195{
    196         if (m_collision_object)
    197                 delete m_collision_object;
    198 
    199         if (!m_motion_state)
    200                 SetTransform(vec3(.0f));
    201 
    202         btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
    203         m_rigid_body = new btRigidBody(NewInfos);
    204         m_collision_object = m_rigid_body;
    205         m_collision_object->setUserPointer(this);
    206 
    207         if (m_mass == .0f)
    208         {
    209                 if (SetToKinematic)
    210                 {
    211                         m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
    212                         m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
    213                 }
    214         }
    215         else
    216                 SetMass(m_mass);
     196    if (m_collision_object)
     197        delete m_collision_object;
     198
     199    if (!m_motion_state)
     200        SetTransform(vec3(.0f));
     201
     202    btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
     203    m_rigid_body = new btRigidBody(NewInfos);
     204    m_collision_object = m_rigid_body;
     205    m_collision_object->setUserPointer(this);
     206
     207    if (m_mass == .0f)
     208    {
     209        if (SetToKinematic)
     210        {
     211            m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
     212            m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
     213        }
     214    }
     215    else
     216        SetMass(m_mass);
    217217}
    218218
     
    220220btGhostObject* EasyPhysic::GetGhostObjectInstance()
    221221{
    222         return new btGhostObject();
     222    return new btGhostObject();
    223223}
    224224
     
    226226void EasyPhysic::InitBodyToGhost()
    227227{
    228         if (m_collision_object)
    229                 delete m_collision_object;
    230 
    231         m_ghost_object = GetGhostObjectInstance();
    232         m_ghost_object->setCollisionShape(m_collision_shape);
    233         m_collision_object = m_ghost_object;
    234         m_collision_object->setUserPointer(this);
    235 
    236         SetTransform(m_local_to_world.v3.xyz, lol::quat(m_local_to_world));
    237 
    238         m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags());
     228    if (m_collision_object)
     229        delete m_collision_object;
     230
     231    m_ghost_object = GetGhostObjectInstance();
     232    m_ghost_object->setCollisionShape(m_collision_shape);
     233    m_collision_object = m_ghost_object;
     234    m_collision_object->setUserPointer(this);
     235
     236    SetTransform(m_local_to_world.v3.xyz, lol::quat(m_local_to_world));
     237
     238    m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags());
    239239}
    240240
     
    251251
    252252  //       const btBroadphasePair& pair = pairArray[i];
    253   //       
     253  //
    254254  //       //unless we manually perform collision detection on this pair, the contacts are in the dynamics world paircache:
    255255  //       btBroadphasePair* collisionPair = dynamicsWorld->getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1);
     
    266266  //          for (int p=0;p<manifold->getNumContacts();p++)
    267267  //          {
    268   //            const btManifoldPoint&pt = manifold->getContactPoint(p);
     268  //               const btManifoldPoint&pt = manifold->getContactPoint(p);
    269269  //              if (pt.getDistance()<0.f)
    270                 //{
    271                 //      const btVector3& ptA = pt.getPositionWorldOnA();
    272                 //      const btVector3& ptB = pt.getPositionWorldOnB();
    273                 //      const btVector3& normalOnB = pt.m_normalWorldOnB;
    274                 //      /// work here
    275                 //}
     270        //{
     271        //    const btVector3& ptA = pt.getPositionWorldOnA();
     272        //    const btVector3& ptB = pt.getPositionWorldOnB();
     273        //    const btVector3& normalOnB = pt.m_normalWorldOnB;
     274        //    /// work here
     275        //}
    276276  //          }
    277277  //       }
     
    282282void EasyPhysic::AddToSimulation(class Simulation* current_simulation)
    283283{
    284         btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
    285         if (dynamics_world)
    286         {
    287                 if (m_ghost_object)
    288                 {
    289                         dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
    290                         current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Ghost);
    291                 }
    292                 else if (m_rigid_body)
    293                 {
    294                         dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
    295                         if (m_mass != .0f)
    296                                 current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Dynamic);
    297                         else
    298                                 current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Static);
    299                 }
    300                 else
    301                 {
    302                         dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
    303                         current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CollisionObject);
    304                 }
    305         }
     284    btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
     285    if (dynamics_world)
     286    {
     287        if (m_ghost_object)
     288        {
     289            dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
     290            current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Ghost);
     291        }
     292        else if (m_rigid_body)
     293        {
     294            dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
     295            if (m_mass != .0f)
     296                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Dynamic);
     297            else
     298                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Static);
     299        }
     300        else
     301        {
     302            dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
     303            current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CollisionObject);
     304        }
     305    }
    306306}
    307307
     
    309309void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation)
    310310{
    311         btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
    312         if (dynamics_world)
    313         {
    314                 if (m_rigid_body)
    315                 {
    316                         dynamics_world->removeRigidBody(m_rigid_body);
    317                         if (m_mass != .0f)
    318                                 current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Dynamic);
    319                         else
    320                                 current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Static);
    321                 }
    322                 else
    323                 {
    324                         dynamics_world->removeCollisionObject(m_collision_object);
    325                         if (m_ghost_object)
    326                                 current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Ghost);
    327                         current_simulation->ObjectRegistration(false, this, Simulation::EEPT_CollisionObject);
    328                 }
    329         }
     311    btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
     312    if (dynamics_world)
     313    {
     314        if (m_rigid_body)
     315        {
     316            dynamics_world->removeRigidBody(m_rigid_body);
     317            if (m_mass != .0f)
     318                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Dynamic);
     319            else
     320                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Static);
     321        }
     322        else
     323        {
     324            dynamics_world->removeCollisionObject(m_collision_object);
     325            if (m_ghost_object)
     326                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Ghost);
     327            current_simulation->ObjectRegistration(false, this, Simulation::EEPT_CollisionObject);
     328        }
     329    }
    330330}
    331331
     
    333333void EasyPhysic::SetLocalInertia(float mass)
    334334{
    335         if (mass != .0f)
    336                 m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
    337         else
    338                 m_local_inertia = btVector3(.0f, .0f, .0f);
     335    if (mass != .0f)
     336        m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
     337    else
     338        m_local_inertia = btVector3(.0f, .0f, .0f);
    339339}
    340340
Note: See TracChangeset for help on using the changeset viewer.