Changeset 1782


Ignore:
Timestamp:
Aug 20, 2012, 11:21:01 PM (11 years ago)
Author:
touky
Message:

Small physic refactor.

Location:
trunk/test/Physics
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/Physics/Include/EasyCharacterController.h

    r1768 r1782  
    1111
    1212//
    13 // The EasyPhysic class
     13// The EasyCharacterController class
    1414// ------------------
     15//
     16
     17//Should try to to make a btKinematicCharacterController for real.
    1518//
    1619
     
    3437{
    3538
     39        friend class Simulation;
     40        friend class EasyPhysic;
     41
    3642#ifdef HAVE_PHYS_USE_BULLET
    3743
     
    4854                m_gamegroup = GAMEGROUP_EZP_CHAR_CTRLR;
    4955                m_up_axis = 1;
     56                m_gravity = vec3(.0f, -9.81f, .0f);
    5057        }
    5158        ~EasyCharacterController()
     
    7885        vec3                                                    m_base_cached_movement;
    7986        vec3                                                    m_frame_cached_movement;
     87        vec3                                                    m_gravity;
     88        vec3                                                    m_velocity;
    8089
    8190#else  // NO PHYSIC IMPLEMENTATION
  • trunk/test/Physics/Include/EasyConstraint.h

    r1749 r1782  
    3131class EasyConstraint
    3232{
     33
     34        friend class Simulation;
     35        friend class EasyPhysic;
     36
    3337#ifdef HAVE_PHYS_USE_BULLET
    3438
     
    4145                m_cone_twist_constraint(NULL),
    4246                m_6dof_constraint(NULL),
     47                m_owner_simulation(NULL),
    4348                m_a_physobj(NULL),
    4449                m_b_physobj(NULL),
     
    194199
    195200private:
     201        Simulation*                                     m_owner_simulation;
    196202        EasyPhysic*                                     m_a_physobj;
    197203        EasyPhysic*                                     m_b_physobj;
  • trunk/test/Physics/Include/EasyPhysics.h

    r1768 r1782  
    3434{
    3535
     36        friend class Simulation;
    3637        friend class EasyConstraint;
    37         friend class Simulation;
    3838
    3939#ifdef HAVE_PHYS_USE_BULLET
     
    154154        int                                                                                     m_collision_mask;
    155155        WorldEntity*                                                            m_owner_entity;
     156        Simulation*                                                                     m_owner_simulation;
    156157
    157158        //Base/Attachment logic
  • trunk/test/Physics/Include/LolPhysics.h

    r1768 r1782  
    321321
    322322private:
     323
    323324        friend class EasyPhysic;
     325        friend class EasyCharacterController;
    324326        friend class EasyConstraint;
    325327
     328        enum eEasyPhysicType
     329        {
     330                EEPT_Dynamic,
     331                EEPT_Static,
     332                EEPT_Ghost,
     333                EEPT_CollisionObject,
     334                EEPT_CharacterController,
     335
     336                EEPT_MAX
     337        };
     338
     339        //m_owner_simulation
    326340        //Adds the given EasyPhysic to the correct list.
    327         void AddToDynamic(EasyPhysic* NewEPDynamic)     { m_dynamic_list << NewEPDynamic; }
    328         void AddToStatic(EasyPhysic* NewEPStatic)       { m_static_list << NewEPStatic; }
    329         void AddToGhost(EasyPhysic* NewEPGhost)         { m_ghost_list << NewEPGhost; }
    330         void AddToConstraint(EasyConstraint* NewEC)     { m_constraint_list     << NewEC; }
     341        void ObjectRegistration(bool AddObject, EasyPhysic* NewEP, eEasyPhysicType CurType)
     342        {
     343                Array<EasyPhysic*>* SearchList = NULL;
     344                switch(CurType)
     345                {
     346                        case EEPT_Dynamic:
     347                        {
     348                                SearchList = &m_dynamic_list;
     349                                break;
     350                        }
     351                        case EEPT_Static:
     352                        {
     353                                SearchList = &m_static_list;
     354                                break;
     355                        }
     356                        case EEPT_Ghost:
     357                        {
     358                                SearchList = &m_ghost_list;
     359                                break;
     360                        }
     361                        case EEPT_CollisionObject:
     362                        {
     363                                SearchList = &m_collision_object_list;
     364                                break;
     365                        }
     366                        case EEPT_CharacterController:
     367                        {
     368                                SearchList = &m_character_controller_list;
     369                                break;
     370                        }
     371                }
     372
     373                if (AddObject)
     374                {
     375                        NewEP->m_owner_simulation = this;
     376                        (*SearchList) << NewEP;
     377                }
     378                else
     379                {
     380                        NewEP->m_owner_simulation = NULL;
     381                        for (int i = 0; i < SearchList->Count(); ++i)
     382                        {
     383                                if ((*SearchList)[i] == NewEP)
     384                                {
     385                                        SearchList->Remove(i--);
     386                                        break;
     387                                }
     388                        }
     389                }
     390        }
     391        void ObjectRegistration(bool AddObject, EasyConstraint* NewEC)
     392        {
     393                Array<EasyConstraint*>* SearchList = NULL;
     394                SearchList = &m_constraint_list;
     395
     396                if (AddObject)
     397                {
     398                        NewEC->m_owner_simulation = this;
     399                        (*SearchList) << NewEC;
     400                }
     401                else
     402                {
     403                        NewEC->m_owner_simulation = NULL;
     404                        for (int i = 0; i < SearchList->Count(); ++i)
     405                        {
     406                                if ((*SearchList)[i] == NewEC)
     407                                {
     408                                        SearchList->Remove(i--);
     409                                        break;
     410                                }
     411                        }
     412                }
     413        }
    331414
    332415        //Easy Physics body List
     
    334417        Array<EasyPhysic*>                                              m_static_list;
    335418        Array<EasyPhysic*>                                              m_ghost_list;
     419        Array<EasyPhysic*>                                              m_collision_object_list;
     420        Array<EasyPhysic*>                                              m_character_controller_list;
    336421        Array<EasyConstraint*>                                  m_constraint_list;
    337422
  • trunk/test/Physics/Src/EasyCharacterController.cpp

    r1779 r1782  
    6363
    6464                m_character = new btKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);
     65
     66                //Deactivate Character controller basic behaviour.
     67                //m_character->setGravity(.0f);
     68                //m_character->setFallSpeed(.0f);
     69
    6570                dynamics_world->addAction(m_character);
     71                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CharacterController);
    6672                Ticker::Ref(this);
    6773        }
     
    7985                {
    8086                        dynamics_world->removeAction(m_character);
     87                        current_simulation->ObjectRegistration(false, this, Simulation::EEPT_CharacterController);
    8188                        Ticker::Unref(this);
    8289                }
     
    119126        Entity::TickGame(seconds);
    120127
    121         m_character->setVelocityForTimeInterval(LOL2BT_VEC3(LOL2BT_UNIT * (m_base_cached_movement + m_frame_cached_movement)) / seconds, seconds);
     128        int IterationsNb = (int)(seconds / m_owner_simulation->m_timestep);
     129        float NewSeconds = IterationsNb * m_owner_simulation->m_timestep;
     130        m_character->setVelocityForTimeInterval(LOL2BT_VEC3(LOL2BT_UNIT * (m_base_cached_movement + m_frame_cached_movement)) / NewSeconds, NewSeconds);
    122131        m_base_cached_movement = vec3(.0f);
    123132}
  • trunk/test/Physics/Src/EasyConstraint.cpp

    r1760 r1782  
    2626        {
    2727                dynamics_world->addConstraint(m_typed_constraint, m_disable_a2b_collision);
    28                 current_simulation->AddToConstraint(this);
     28                current_simulation->ObjectRegistration(true, this);
    2929        }
    3030}
     
    3434        btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
    3535        if (dynamics_world && m_typed_constraint)
     36        {
    3637                dynamics_world->removeConstraint(m_typed_constraint);
     38                current_simulation->ObjectRegistration(false, this);
     39        }
    3740}
    3841
  • trunk/test/Physics/Src/EasyPhysics.cpp

    r1768 r1782  
    4242        m_collision_mask(1),
    4343        m_owner_entity(NewOwnerEntity),
     44        m_owner_simulation(NULL),
    4445        m_base_physic(NULL)
    4546{
     
    287288                {
    288289                        dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
    289                         current_simulation->AddToGhost(this);
     290                        current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Ghost);
    290291                }
    291292                else if (m_rigid_body)
     
    293294                        dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
    294295                        if (m_mass != .0f)
    295                                 current_simulation->AddToDynamic(this);
     296                                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Dynamic);
    296297                        else
    297                                 current_simulation->AddToStatic(this);
     298                                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Static);
    298299                }
    299300                else
     301                {
    300302                        dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
     303                        current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CollisionObject);
     304                }
    301305        }
    302306}
     
    309313        {
    310314                if (m_rigid_body)
     315                {
    311316                        dynamics_world->removeRigidBody(m_rigid_body);
    312                 else if (m_collision_object)
     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                {
    313324                        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                }
    314329        }
    315330}
Note: See TracChangeset for help on using the changeset viewer.