Ignore:
Timestamp:
Sep 7, 2012, 5:00:31 PM (9 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/BulletCharacterController.cpp

    r1870 r1888  
    4646bool BulletKinematicCharacterController::RecoverFromPenetration(btCollisionWorld* CollisionWorld)
    4747{
    48         bool HasPenetration = false;
    49 
    50         //Retrieve all pair with us colliding.
    51         CollisionWorld->getDispatcher()->dispatchAllCollisionPairs(m_ghost_object->getOverlappingPairCache(), CollisionWorld->getDispatchInfo(), CollisionWorld->getDispatcher());
    52         m_current_position = BT2LOLU_VEC3(m_ghost_object->getWorldTransform().getOrigin());
    53        
    54         float MaxPen = .0f;
    55         for (int i = 0; i < m_ghost_object->getOverlappingPairCache()->getNumOverlappingPairs(); i++)
    56         {
    57                 m_manifold_array.resize(0);
    58 
    59                 //this is the equivalent of the "Touch algorithm". Maybe refactor ?
    60                 btBroadphasePair* CollisionPair = &m_ghost_object->getOverlappingPairCache()->getOverlappingPairArray()[i];
    61                 if (CollisionPair->m_algorithm)
    62                         CollisionPair->m_algorithm->getAllContactManifolds(m_manifold_array);
    63                
    64                 for (int j = 0; j < m_manifold_array.size(); ++j)
    65                 {
    66                         btPersistentManifold* CurMfold = m_manifold_array[j];
    67                         //Normal direction differs if we're Body0
    68                         float DirSign = CurMfold->getBody0() == m_ghost_object ? -1.f : 1.f;
    69 
    70                         for (int k = 0; k < CurMfold->getNumContacts(); k++)
    71                         {
    72                                 const btManifoldPoint& MfPoint = CurMfold->getContactPoint(k);
    73                                 float Dist = MfPoint.getDistance();
    74                                 if (Dist < .0f)
    75                                 {
    76                                         if (Dist < MaxPen)
    77                                         {
    78                                                 MaxPen = Dist;
    79                                                 m_touching_normal = BT2LOL_VEC3(MfPoint.m_normalWorldOnB) * DirSign;
    80                                         }
    81                                         m_current_position += BT2LOL_VEC3(MfPoint.m_normalWorldOnB) * DirSign * Dist * .2f;
    82                                         HasPenetration = true;
    83                                 }
    84                         }
    85                 }
    86         }
    87 
    88         btTransform GObjMx = m_ghost_object->getWorldTransform();
    89         GObjMx.setOrigin(LOL2BTU_VEC3(m_current_position));
    90         m_ghost_object->setWorldTransform(GObjMx);
    91 
    92         return HasPenetration;
     48    bool HasPenetration = false;
     49
     50    //Retrieve all pair with us colliding.
     51    CollisionWorld->getDispatcher()->dispatchAllCollisionPairs(m_ghost_object->getOverlappingPairCache(), CollisionWorld->getDispatchInfo(), CollisionWorld->getDispatcher());
     52    m_current_position = BT2LOLU_VEC3(m_ghost_object->getWorldTransform().getOrigin());
     53
     54    float MaxPen = .0f;
     55    for (int i = 0; i < m_ghost_object->getOverlappingPairCache()->getNumOverlappingPairs(); i++)
     56    {
     57        m_manifold_array.resize(0);
     58
     59        //this is the equivalent of the "Touch algorithm". Maybe refactor ?
     60        btBroadphasePair* CollisionPair = &m_ghost_object->getOverlappingPairCache()->getOverlappingPairArray()[i];
     61        if (CollisionPair->m_algorithm)
     62            CollisionPair->m_algorithm->getAllContactManifolds(m_manifold_array);
     63
     64        for (int j = 0; j < m_manifold_array.size(); ++j)
     65        {
     66            btPersistentManifold* CurMfold = m_manifold_array[j];
     67            //Normal direction differs if we're Body0
     68            float DirSign = CurMfold->getBody0() == m_ghost_object ? -1.f : 1.f;
     69
     70            for (int k = 0; k < CurMfold->getNumContacts(); k++)
     71            {
     72                const btManifoldPoint& MfPoint = CurMfold->getContactPoint(k);
     73                float Dist = MfPoint.getDistance();
     74                if (Dist < .0f)
     75                {
     76                    if (Dist < MaxPen)
     77                    {
     78                        MaxPen = Dist;
     79                        m_touching_normal = BT2LOL_VEC3(MfPoint.m_normalWorldOnB) * DirSign;
     80                    }
     81                    m_current_position += BT2LOL_VEC3(MfPoint.m_normalWorldOnB) * DirSign * Dist * .2f;
     82                    HasPenetration = true;
     83                }
     84            }
     85        }
     86    }
     87
     88    btTransform GObjMx = m_ghost_object->getWorldTransform();
     89    GObjMx.setOrigin(LOL2BTU_VEC3(m_current_position));
     90    m_ghost_object->setWorldTransform(GObjMx);
     91
     92    return HasPenetration;
    9393}
    9494
     
    9696void BulletKinematicCharacterController::UpdateTargetOnHit(const vec3& HitNormal, float TangentMag, float NormalMag)
    9797{
    98         vec3 Movedir = m_target_position - m_current_position;
    99         float MoveLength = (float)length(Movedir);
    100 
    101         if (MoveLength > SIMD_EPSILON)
    102         {
    103                 Movedir = normalize(Movedir);
    104 
    105                 vec3 ReflectDir = normalize(GetReflectedDir(Movedir, HitNormal));
    106                 vec3 ParallelDir = ProjectDirOnNorm(ReflectDir, HitNormal);
    107                 vec3 PerpindicularDir = ProjectDirOnNormPerpindicular(ReflectDir, HitNormal);
    108 
    109                 m_target_position = m_current_position;
    110 
    111                 if (NormalMag != .0f)
    112                         m_target_position += PerpindicularDir * NormalMag * MoveLength;
    113         }
     98    vec3 Movedir = m_target_position - m_current_position;
     99    float MoveLength = (float)length(Movedir);
     100
     101    if (MoveLength > SIMD_EPSILON)
     102    {
     103        Movedir = normalize(Movedir);
     104
     105        vec3 ReflectDir = normalize(GetReflectedDir(Movedir, HitNormal));
     106        vec3 ParallelDir = ProjectDirOnNorm(ReflectDir, HitNormal);
     107        vec3 PerpindicularDir = ProjectDirOnNormPerpindicular(ReflectDir, HitNormal);
     108
     109        m_target_position = m_current_position;
     110
     111        if (NormalMag != .0f)
     112            m_target_position += PerpindicularDir * NormalMag * MoveLength;
     113    }
    114114}
    115115
     
    117117void BulletKinematicCharacterController::DoMove(btCollisionWorld* CollisionWorld, const vec3& MoveStep, float DeltaTime)
    118118{
    119         // phase 2: forward and strafe
    120         m_target_position = m_current_position + MoveStep;
    121         btTransform SweepStart, SweepEnd;
    122         SweepStart.setIdentity();
    123         SweepEnd.setIdentity();
    124 
    125         float Fraction = 1.f;
    126         float SqDist = .0f;
    127 
    128         if (m_touching_contact && dot(m_normalized_direction, m_touching_normal) > .0f)
    129                 UpdateTargetOnHit(m_touching_normal);
    130 
    131         //Let's loop on movement, until Movement fraction if below 0.01, which means we've reached our destination.
    132         //Or until we'tried 10 times.
    133         int MaxMoveLoop = 10;
    134         while (Fraction > .01f && MaxMoveLoop-- > 0)
    135         {
    136                 SweepStart.setOrigin(LOL2BTU_VEC3(m_current_position));
    137                 SweepEnd.setOrigin(LOL2BTU_VEC3(m_target_position));
    138                 vec3 SweepDirNeg(m_current_position - m_target_position);
    139 
    140                 ClosestNotMeConvexResultCallback SweepCallback(m_ghost_object, SweepDirNeg, .0f);
    141                 SweepCallback.m_collisionFilterGroup = GetGhostObject()->getBroadphaseHandle()->m_collisionFilterGroup;
    142                 SweepCallback.m_collisionFilterMask = GetGhostObject()->getBroadphaseHandle()->m_collisionFilterMask;
    143 
    144                 //The sweep test is done with an added margin, so we use it and then discard it
    145                 float SavedMargin = m_convex_shape->getMargin();
    146                 m_convex_shape->setMargin(SavedMargin + m_added_margin); //Apply Added Margin
    147                 if (m_do_gobject_sweep_test)
    148                         m_ghost_object->convexSweepTest (m_convex_shape, SweepStart, SweepEnd, SweepCallback, CollisionWorld->getDispatchInfo().m_allowedCcdPenetration);
    149                 else
    150                         CollisionWorld->convexSweepTest (m_convex_shape, SweepStart, SweepEnd, SweepCallback, CollisionWorld->getDispatchInfo().m_allowedCcdPenetration);
    151                 m_convex_shape->setMargin(SavedMargin); //Restore saved margin
    152 
    153                 Fraction -= SweepCallback.m_closestHitFraction;
    154 
    155                 if (SweepCallback.hasHit())
    156                 {       
    157                         //We moved only a Fraction
    158                         float HitDist = (float)length(BT2LOLU_VEC3(SweepCallback.m_hitPointWorld) - m_current_position);
    159 
    160                         UpdateTargetOnHit(BT2LOL_VEC3(SweepCallback.m_hitNormalWorld));
    161                         vec3 NewDir = m_target_position - m_current_position;
    162                         SqDist = sqlength(NewDir);
    163                         if (SqDist > SIMD_EPSILON)
    164                         {
    165                                 NewDir = normalize(NewDir);
    166                                 //See Quake2: "If velocity is against original velocity, stop ead to avoid tiny oscilations in sloping corners."
    167                                 if (dot(NewDir, m_normalized_direction) <= .0f)
    168                                         break;
    169                         }
    170                         else
    171                                 break;
    172                 }
    173                 else //We moved whole way
    174                         m_current_position = m_target_position;
    175         }
     119    // phase 2: forward and strafe
     120    m_target_position = m_current_position + MoveStep;
     121    btTransform SweepStart, SweepEnd;
     122    SweepStart.setIdentity();
     123    SweepEnd.setIdentity();
     124
     125    float Fraction = 1.f;
     126    float SqDist = .0f;
     127
     128    if (m_touching_contact && dot(m_normalized_direction, m_touching_normal) > .0f)
     129        UpdateTargetOnHit(m_touching_normal);
     130
     131    //Let's loop on movement, until Movement fraction if below 0.01, which means we've reached our destination.
     132    //Or until we'tried 10 times.
     133    int MaxMoveLoop = 10;
     134    while (Fraction > .01f && MaxMoveLoop-- > 0)
     135    {
     136        SweepStart.setOrigin(LOL2BTU_VEC3(m_current_position));
     137        SweepEnd.setOrigin(LOL2BTU_VEC3(m_target_position));
     138        vec3 SweepDirNeg(m_current_position - m_target_position);
     139
     140        ClosestNotMeConvexResultCallback SweepCallback(m_ghost_object, SweepDirNeg, .0f);
     141        SweepCallback.m_collisionFilterGroup = GetGhostObject()->getBroadphaseHandle()->m_collisionFilterGroup;
     142        SweepCallback.m_collisionFilterMask = GetGhostObject()->getBroadphaseHandle()->m_collisionFilterMask;
     143
     144        //The sweep test is done with an added margin, so we use it and then discard it
     145        float SavedMargin = m_convex_shape->getMargin();
     146        m_convex_shape->setMargin(SavedMargin + m_added_margin); //Apply Added Margin
     147        if (m_do_gobject_sweep_test)
     148            m_ghost_object->convexSweepTest (m_convex_shape, SweepStart, SweepEnd, SweepCallback, CollisionWorld->getDispatchInfo().m_allowedCcdPenetration);
     149        else
     150            CollisionWorld->convexSweepTest (m_convex_shape, SweepStart, SweepEnd, SweepCallback, CollisionWorld->getDispatchInfo().m_allowedCcdPenetration);
     151        m_convex_shape->setMargin(SavedMargin); //Restore saved margin
     152
     153        Fraction -= SweepCallback.m_closestHitFraction;
     154
     155        if (SweepCallback.hasHit())
     156        {
     157            //We moved only a Fraction
     158            float HitDist = (float)length(BT2LOLU_VEC3(SweepCallback.m_hitPointWorld) - m_current_position);
     159
     160            UpdateTargetOnHit(BT2LOL_VEC3(SweepCallback.m_hitNormalWorld));
     161            vec3 NewDir = m_target_position - m_current_position;
     162            SqDist = sqlength(NewDir);
     163            if (SqDist > SIMD_EPSILON)
     164            {
     165                NewDir = normalize(NewDir);
     166                //See Quake2: "If velocity is against original velocity, stop ead to avoid tiny oscilations in sloping corners."
     167                if (dot(NewDir, m_normalized_direction) <= .0f)
     168                    break;
     169            }
     170            else
     171                break;
     172        }
     173        else //We moved whole way
     174            m_current_position = m_target_position;
     175    }
    176176}
    177177
     
    179179void BulletKinematicCharacterController::PreStep(btCollisionWorld* CollisionWorld)
    180180{
    181         int MaxPenetrationLoop = 0;
    182         m_touching_contact = false;
    183 
    184         while (RecoverFromPenetration(CollisionWorld))
    185         {
    186                 MaxPenetrationLoop++;
    187                 m_touching_contact = true;
    188                 if (MaxPenetrationLoop > 4)
    189                         break;
    190         }
    191 
    192         m_current_position = BT2LOLU_VEC3(m_ghost_object->getWorldTransform().getOrigin());
    193         m_target_position = m_current_position;
     181    int MaxPenetrationLoop = 0;
     182    m_touching_contact = false;
     183
     184    while (RecoverFromPenetration(CollisionWorld))
     185    {
     186        MaxPenetrationLoop++;
     187        m_touching_contact = true;
     188        if (MaxPenetrationLoop > 4)
     189            break;
     190    }
     191
     192    m_current_position = BT2LOLU_VEC3(m_ghost_object->getWorldTransform().getOrigin());
     193    m_target_position = m_current_position;
    194194}
    195195
     
    198198void BulletKinematicCharacterController::PlayerStep(btCollisionWorld* CollisionWorld, float DeltaTime)
    199199{
    200         // quick check...
    201         if (!m_use_walk_direction && m_velocity_time_interval <= .0f)
    202                 return;         // no motion
    203 
    204         // Update fall velocity.
    205         //m_velocity -= m_gravity * DeltaTime;
    206 
    207         btTransform NewTransform;
    208         NewTransform = m_ghost_object->getWorldTransform();
    209 
    210         vec3 MoveStep(.0f);
    211         if (m_use_walk_direction)
    212                 MoveStep = m_walk_direction;
    213         else
    214         {
    215                 //Still have some time left for moving!
    216                 float dtMoving = (DeltaTime < m_velocity_time_interval) ? DeltaTime : m_velocity_time_interval;
    217                 m_velocity_time_interval -= DeltaTime;
    218 
    219                 // how far will we MoveStep while we are moving?
    220                 MoveStep = m_walk_direction * dtMoving;
    221         }
    222 
    223         //Okay, step !
    224         DoMove(CollisionWorld, MoveStep, DeltaTime);
    225 
    226         //Movement finished, update World transform
    227         NewTransform.setOrigin(LOL2BTU_VEC3(m_current_position));
    228         m_ghost_object->setWorldTransform(NewTransform);
     200    // quick check...
     201    if (!m_use_walk_direction && m_velocity_time_interval <= .0f)
     202        return;        // no motion
     203
     204    // Update fall velocity.
     205    //m_velocity -= m_gravity * DeltaTime;
     206
     207    btTransform NewTransform;
     208    NewTransform = m_ghost_object->getWorldTransform();
     209
     210    vec3 MoveStep(.0f);
     211    if (m_use_walk_direction)
     212        MoveStep = m_walk_direction;
     213    else
     214    {
     215        //Still have some time left for moving!
     216        float dtMoving = (DeltaTime < m_velocity_time_interval) ? DeltaTime : m_velocity_time_interval;
     217        m_velocity_time_interval -= DeltaTime;
     218
     219        // how far will we MoveStep while we are moving?
     220        MoveStep = m_walk_direction * dtMoving;
     221    }
     222
     223    //Okay, step !
     224    DoMove(CollisionWorld, MoveStep, DeltaTime);
     225
     226    //Movement finished, update World transform
     227    NewTransform.setOrigin(LOL2BTU_VEC3(m_current_position));
     228    m_ghost_object->setWorldTransform(NewTransform);
    229229}
    230230
     
    232232void BulletKinematicCharacterController::Jump()
    233233{
    234         if (!CanJump())
    235                 return;
    236 
    237         m_vertical_velocity = m_jump_speed;
    238         m_was_jumping = true;
     234    if (!CanJump())
     235        return;
     236
     237    m_vertical_velocity = m_jump_speed;
     238    m_was_jumping = true;
    239239}
    240240
     
    242242#endif // USE_LOL_CTRLR_CHARAC
    243243
    244         } /* namespace phys */
     244    } /* namespace phys */
    245245
    246246} /* namespace lol */
Note: See TracChangeset for help on using the changeset viewer.