Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2292 for code


Ignore:
Timestamp:
Nov 28, 2008, 1:25:16 AM (16 years ago)
Author:
rgrieder
Message:

Finally managed to work out some physics. According to my tests, collisions with simple spheres should work with dynamic/kinematic/static objects. There are currently only a limited number of XML parameters, but we're surely going to extend that. Furthermore there is some more thinking to be done concerning changes of btRigidBody properties when it's already added to the world.

Location:
code/branches/physics/src/orxonox/objects
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • code/branches/physics/src/orxonox/objects/Scene.cc

    r2192 r2292  
    3939#include "core/XMLPort.h"
    4040
     41#include "BulletCollision/BroadphaseCollision/btAxisSweep3.h"
     42#include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
     43#include "BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h"
     44
    4145namespace orxonox
    4246{
     
    7074        }
    7175
    72         /////////////
    73         // Physics //
    74         /////////////
    75 
    76         // create bullet world; bullet solver etc.
    77 
    78         // int maxProxies = 1024;
    79 
    80         btVector3 worldAabbMin(-10000,-10000,-10000);
    81         btVector3 worldAabbMax(10000,10000,10000);
    82         bt32BitAxisSweep3* broadphase = new bt32BitAxisSweep3(worldAabbMin,worldAabbMax);
    83 
    84         this -> collisionConfiguration_ = new btDefaultCollisionConfiguration();
    85         this -> dispatcher_ = new btCollisionDispatcher(collisionConfiguration_);
    86 
    87         this -> solver_ = new btSequentialImpulseConstraintSolver;
    88 
    89         this -> dynamicsWorld_ =  new btDiscreteDynamicsWorld(dispatcher_,broadphase,solver_,collisionConfiguration_);
    90 
    91         dynamicsWorld_->setGravity(btVector3(0,-10,0));
    92 
     76        // No physics for default
     77        this->physicalWorld_ = 0;
    9378
    9479        // test test test
     
    130115        XMLPortParam(Scene, "shadow", setShadow, getShadow, xmlelement, mode).defaultValues(true);
    131116
     117        const int defaultMaxWorldSize = 100000;
     118        Vector3 worldAabbMin(-defaultMaxWorldSize, -defaultMaxWorldSize, -defaultMaxWorldSize);
     119        Vector3 worldAabbMax( defaultMaxWorldSize,  defaultMaxWorldSize,  defaultMaxWorldSize);
     120        XMLPortParamVariable(Scene, "negativeWorldRange", worldAabbMin, xmlelement, mode);
     121        XMLPortParamVariable(Scene, "positiveWorldRange", worldAabbMax, xmlelement, mode);
     122        XMLPortParam(Scene, "hasPhysics", setPhysicalWorld, hasPhysics, xmlelement, mode).defaultValue(0, true).defaultValue(1, worldAabbMin).defaultValue(2, worldAabbMax);
     123
    132124        XMLPortObjectExtended(Scene, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
    133125    }
     
    137129        REGISTERSTRING(this->skybox_,     network::direction::toclient, new network::NetworkCallback<Scene>(this, &Scene::networkcallback_applySkybox));
    138130        REGISTERDATA(this->ambientLight_, network::direction::toclient, new network::NetworkCallback<Scene>(this, &Scene::networkcallback_applyAmbientLight));
     131    }
     132
     133    void Scene::setPhysicalWorld(bool wantPhysics, const Vector3& worldAabbMin, const Vector3& worldAabbMax)
     134    {
     135        if (wantPhysics && !hasPhysics())
     136        {
     137            btVector3 worldAabbMin(worldAabbMin.x, worldAabbMin.y, worldAabbMin.z);
     138            btVector3 worldAabbMax(worldAabbMax.x, worldAabbMax.y, worldAabbMax.z);
     139
     140            btDefaultCollisionConfiguration*     collisionConfig = new btDefaultCollisionConfiguration();
     141            btCollisionDispatcher*               dispatcher      = new btCollisionDispatcher(collisionConfig);
     142            bt32BitAxisSweep3*                   broadphase      = new bt32BitAxisSweep3(worldAabbMin,worldAabbMax);
     143            btSequentialImpulseConstraintSolver* solver          = new btSequentialImpulseConstraintSolver;
     144
     145            this->physicalWorld_ =  new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfig);
     146
     147            // test test test
     148            this->physicalWorld_->setGravity(btVector3(0,0,0));
     149            // test test test
     150        }
     151        else
     152        {
     153            // TODO: Destroy Bullet physics
     154        }
     155    }
     156
     157    void Scene::tick(float dt)
     158    {
     159        if (physicalWorld_)
     160            physicalWorld_->stepSimulation(dt,10);
    139161    }
    140162
  • code/branches/physics/src/orxonox/objects/Scene.h

    r2192 r2292  
    3535#include "core/BaseObject.h"
    3636#include "util/Math.h"
     37#include "objects/Tickable.h"
    3738
    38 #include "ogrebullet/Dynamics/OgreBulletDynamics.h"
     39#include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
    3940
    4041namespace orxonox
    4142{
    42     class _OrxonoxExport Scene : public BaseObject, public network::Synchronisable
     43    class _OrxonoxExport Scene : public BaseObject, public network::Synchronisable, public Tickable
    4344    {
    4445        public:
     
    5556
    5657            inline btDiscreteDynamicsWorld* getPhysicalWorld() const
    57                 { return this->dynamicsWorld_; }
     58                { return this->physicalWorld_; }
    5859
    5960            void setSkybox(const std::string& skybox);
     
    6970                { return this->bShadows_; }
    7071
     72            inline const Vector3& getWorldAabbMax()
     73            {
     74                this->physicalWorld_->getBroadphase();
     75            }
     76
     77            inline bool hasPhysics()
     78                { return this->physicalWorld_ != 0; }
     79            void setPhysicalWorld(bool wantsPhysics, const Vector3& worldAabbMin, const Vector3& worldAabbMax);
     80
     81            void tick(float dt);
     82
    7183        private:
    7284            void addObject(BaseObject* object);
     
    8193            Ogre::SceneNode*       rootSceneNode_;
    8294
    83             btDiscreteDynamicsWorld* dynamicsWorld_;
    84             btSequentialImpulseConstraintSolver* solver_;
    85             btDefaultCollisionConfiguration* collisionConfiguration_;
    86             btCollisionDispatcher* dispatcher_;
    87             // Point auf Bullet btDynamics world && solver
     95            btDiscreteDynamicsWorld* physicalWorld_;
    8896
    8997            std::string            skybox_;
  • code/branches/physics/src/orxonox/objects/worldentities/ControllableEntity.cc

    r2201 r2292  
    229229        if (this->isActive())
    230230        {
    231             this->velocity_ += (dt * this->acceleration_);
    232             this->node_->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
     231            if (!this->isDynamic())
     232            {
     233                this->velocity_ += (dt * this->acceleration_);
     234                this->node_->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
     235            }
    233236
    234237            if (Core::isMaster())
     
    325328    }
    326329
    327 
    328     // virtual void PositionChanged() { }
    329     void ControllableEntity::positionChanged(const Vector3& position)
     330    void ControllableEntity::positionChanged()
    330331    {
    331332        if (Core::isMaster())
    332333        {
    333             this->server_position_ = this->getPosition();
    334             ++this->server_overwrite_;
    335         }
    336         else if (this->bControlled_)
    337         {
    338             this->client_position_ = position;
    339         }
    340     }
    341 
    342     void ControllableEntity::setVelocity(const Vector3& velocity)
    343     {
    344         if (Core::isMaster())
    345         {
    346             this->velocity_ = velocity;
    347             this->server_velocity_ = velocity;
    348             ++this->server_overwrite_;
    349         }
    350         else if (this->bControlled_)
    351         {
    352             this->velocity_ = velocity;
    353             this->client_velocity_ = velocity;
    354         }
    355     }
    356     // virtual void translateChanged() { }
    357     void ControllableEntity::translateChanged(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
    358     {
    359         if (Core::isMaster())
    360         {
    361             this->node_->translate(distance, relativeTo);
    362334            this->server_position_ = this->node_->getPosition();
    363335            ++this->server_overwrite_;
     
    365337        else if (this->bControlled_)
    366338        {
    367             this->node_->translate(distance, relativeTo);
    368339            this->client_position_ = this->node_->getPosition();
    369340        }
    370341    }
    371     // virtual void orientationChanged() { }
    372     void ControllableEntity::orientationChanged(const Quaternion& orientation)
     342
     343    void ControllableEntity::orientationChanged()
    373344    {
    374345        if (Core::isMaster())
    375346        {
    376             this->node_->setOrientation(orientation);
    377             this->server_orientation_ = orientation;
    378             ++this->server_overwrite_;
    379         }
    380         else if (this->bControlled_)
    381         {
    382             this->node_->setOrientation(orientation);
    383             this->client_orientation_ = orientation;
    384         }
    385     }
    386     // virtual void rotateChanged() { }
    387     void ControllableEntity::rotateChanged(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
    388     {
    389         if (Core::isMaster())
    390         {
    391             this->node_->rotate(rotation, relativeTo);
    392347            this->server_orientation_ = this->node_->getOrientation();
    393348            ++this->server_overwrite_;
     
    395350        else if (this->bControlled_)
    396351        {
    397             this->node_->rotate(rotation, relativeTo);
    398352            this->client_orientation_ = this->node_->getOrientation();
    399353        }
    400354    }
    401     // virtual void yawChanged() { }
    402     void ControllableEntity::yawChanged(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
     355
     356    void ControllableEntity::velocityChanged()
    403357    {
    404358        if (Core::isMaster())
    405359        {
    406             this->node_->yaw(angle, relativeTo);
    407             this->server_orientation_ = this->node_->getOrientation();
     360            this->server_velocity_ = this->velocity_;
    408361            ++this->server_overwrite_;
    409362        }
    410363        else if (this->bControlled_)
    411364        {
    412             this->node_->yaw(angle, relativeTo);
    413             this->client_orientation_ = this->node_->getOrientation();
    414         }
    415     }
    416     // virtual void pitchChanged() { }
    417     void ControllableEntity::pitchChanged(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    418     {
    419         if (Core::isMaster())
    420         {
    421             this->node_->pitch(angle, relativeTo);
    422             this->server_orientation_ = this->node_->getOrientation();
    423             ++this->server_overwrite_;
    424         }
    425         else if (this->bControlled_)
    426         {
    427             this->node_->pitch(angle, relativeTo);
    428             this->client_orientation_ = this->node_->getOrientation();
    429         }
    430     }
    431     // virtual void rollChanged() { }
    432     void ControllableEntity::rollChanged(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    433     {
    434         if (Core::isMaster())
    435         {
    436             this->node_->roll(angle, relativeTo);
    437             this->server_orientation_ = this->node_->getOrientation();
    438             ++this->server_overwrite_;
    439         }
    440         else if (this->bControlled_)
    441         {
    442             this->node_->roll(angle, relativeTo);
    443             this->client_orientation_ = this->node_->getOrientation();
    444         }
    445     }
    446     //virtual void lookAtChanged() { }
    447     void ControllableEntity::lookAtChanged(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
    448     {
    449         if (Core::isMaster())
    450         {
    451             this->node_->lookAt(target, relativeTo, localDirectionVector);
    452             this->server_orientation_ = this->node_->getOrientation();
    453             ++this->server_overwrite_;
    454         }
    455         else if (this->bControlled_)
    456         {
    457             this->node_->lookAt(target, relativeTo, localDirectionVector);
    458             this->client_orientation_ = this->node_->getOrientation();
    459         }
    460     }
    461     // virtual void directionChanged( ) { }
    462     void ControllableEntity::directionChanged(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
    463     {
    464         if (Core::isMaster())
    465         {
    466             this->node_->setDirection(direction, relativeTo, localDirectionVector);
    467             this->server_orientation_ = this->node_->getOrientation();
    468             ++this->server_overwrite_;
    469         }
    470         else if (this->bControlled_)
    471         {
    472             this->node_->setDirection(direction, relativeTo, localDirectionVector);
    473             this->client_orientation_ = this->node_->getOrientation();
     365            this->client_velocity_ = this->velocity_;
     366        }
     367    }
     368
     369    void ControllableEntity::setVelocity(const Vector3& velocity)
     370    {
     371        if (this->bControlled_ || Core::isMaster())
     372        {
     373            if (!this->isDynamic())
     374            {
     375                // no physics, we do it ourselves
     376                internalSetVelocity(velocity);
     377            }
     378            else
     379            {
     380                this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
     381            }
     382            velocityChanged();
    474383        }
    475384    }
  • code/branches/physics/src/orxonox/objects/worldentities/ControllableEntity.h

    r2201 r2292  
    7979                { return this->hudtemplate_; }
    8080
    81             void setPosition();
    82             void translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);
    83             void setOrientation(const Quaternion& orientation);
    84             void rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);
    85             void yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);
    86             void pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);
    87             void roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);
    88             void lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
    89             void setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
    90 
    9181            void setVelocity(const Vector3& velocity);
    9282            inline void setVelocity(float x, float y, float z)
    93                 { this->velocity_.x = x; this->velocity_.y = y; this->velocity_.z = z; }
     83                { setVelocity(Vector3(x,y,z)); }
    9484
    9585            inline void setAcceleration(const Vector3& acceleration)
     
    137127            void processClientOrientation();
    138128
     129            inline void internalSetVelocity(const Vector3& velocity)
     130                { this->velocity_ = velocity; }
     131            void positionChanged();
     132            void orientationChanged();
     133            void velocityChanged();
     134
    139135            void networkcallback_changedplayerID();
    140136
  • code/branches/physics/src/orxonox/objects/worldentities/LinearEntity.cc

    r2192 r2292  
    7474        if (this->isActive())
    7575        {
    76             this->velocity_ += (dt * this->acceleration_);
    77             this->node_->translate(dt * this->velocity_);
     76            if (!this->isDynamic())
     77            {
     78                // we have to do 'physics' ourselves.
     79                this->velocity_ += (dt * this->acceleration_);
     80                this->node_->translate(dt * this->velocity_);
    7881
    79             this->rotationRate_ += (dt * this->momentum_);
    80             this->node_->rotate(this->rotationAxis_, this->rotationRate_  * dt);
     82                this->rotationRate_ += (dt * this->momentum_);
     83                this->node_->rotate(this->rotationAxis_, this->rotationRate_  * dt);
     84            }
    8185        }
    8286    }
     
    123127    }
    124128
    125     void LinearEntity::setPosition(const Vector3& position)
     129    void LinearEntity::positionChanged()
    126130    {
    127         this->node_->setPosition(position);
    128         this->overwrite_position_ = this->node_->getPosition();
     131        this->overwrite_position_  = this->node_->getPosition();
    129132    }
    130133
    131     void LinearEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
     134    void LinearEntity::orientationChanged()
    132135    {
    133         this->node_->translate(distance, relativeTo);
    134         this->overwrite_position_ = this->node_->getPosition();
     136        this->overwrite_orientation_  = this->node_->getOrientation();
    135137    }
    136138
    137     void LinearEntity::setOrientation(const Quaternion& orientation)
     139    void LinearEntity::setVelocity(const Vector3& velocity)
    138140    {
    139         this->node_->setOrientation(orientation);
    140         this->overwrite_orientation_ = this->node_->getOrientation();
    141     }
    142 
    143     void LinearEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
    144     {
    145         this->node_->rotate(rotation, relativeTo);
    146         this->overwrite_orientation_ = this->node_->getOrientation();
    147     }
    148 
    149     void LinearEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    150     {
    151         this->node_->yaw(angle, relativeTo);
    152         this->overwrite_orientation_ = this->node_->getOrientation();
    153     }
    154 
    155     void LinearEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    156     {
    157         this->node_->pitch(angle, relativeTo);
    158         this->overwrite_orientation_ = this->node_->getOrientation();
    159     }
    160 
    161     void LinearEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    162     {
    163         this->node_->roll(angle, relativeTo);
    164         this->overwrite_orientation_ = this->node_->getOrientation();
    165     }
    166 
    167     void LinearEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
    168     {
    169         this->node_->lookAt(target, relativeTo, localDirectionVector);
    170         this->overwrite_orientation_ = this->node_->getOrientation();
    171     }
    172 
    173     void LinearEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
    174     {
    175         this->node_->setDirection(direction, relativeTo, localDirectionVector);
    176         this->overwrite_orientation_ = this->node_->getOrientation();
     141        if (!this->isDynamic())
     142        {
     143            // no physics, we do it ourselves
     144            internalSetVelocity(velocity);
     145        }
     146        else
     147        {
     148            this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
     149        }
    177150    }
    178151}
  • code/branches/physics/src/orxonox/objects/worldentities/LinearEntity.h

    r2192 r2292  
    3838namespace orxonox
    3939{
    40     class _OrxonoxExport LinearEntity : public MovableEntity, public network::ClientConnectionListener
     40    class _OrxonoxExport LinearEntity : public MovableEntity, public network::ClientConnectionListener, public Tickable
    4141    {
    4242        public:
     
    4848            void registerVariables();
    4949
    50             using WorldEntity::setPosition;
    51             using WorldEntity::translate;
    52             using WorldEntity::setOrientation;
    53             using WorldEntity::rotate;
    54             using WorldEntity::yaw;
    55             using WorldEntity::pitch;
    56             using WorldEntity::roll;
    57             using WorldEntity::lookAt;
    58             using WorldEntity::setDirection;
    59 
    60             void setPosition(const Vector3& position);
    61             void translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);
    62             void setOrientation(const Quaternion& orientation);
    63             void rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);
    64             void yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);
    65             void pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);
    66             void roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);
    67             void lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
    68             void setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);
    69 
    70             inline void setVelocity(const Vector3& velocity)
    71                 { this->velocity_ = velocity; }
     50            inline void setVelocity(const Vector3& velocity);
    7251            inline void setVelocity(float x, float y, float z)
    7352                { this->velocity_.x = x; this->velocity_.y = y; this->velocity_.z = z; }
     
    11190            void overwriteOrientation();
    11291
     92            void positionChanged();
     93            void orientationChanged();
     94            inline void internalSetVelocity(const Vector3& velocity)
     95                { this->velocity_ = velocity; }
     96
    11397            Vector3 velocity_;
    11498            Vector3 acceleration_;
  • code/branches/physics/src/orxonox/objects/worldentities/MovableEntity.cc

    r2201 r2292  
    3030#include "MovableEntity.h"
    3131
    32 #include "objects/Scene.h"
    33 
     32#include "util/Exception.h"
    3433#include "core/CoreIncludes.h"
    3534#include "core/XMLPort.h"
     35
     36#include "objects/Scene.h"
    3637
    3738namespace orxonox
     
    5758    }
    5859
    59     void MovableEntity::setPosition(const Vector3& position){
    60         // Here the code
    61         if(isDynamic() && bAddedToPhysicalWorld_){
    62             this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
    63             this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    64         }
    65         this->node_->setPosition(position);
     60    //void MovableEntity::setPosition(const Vector3& position)
     61    //{
     62    //    //if (isDynamic() && bAddedToPhysicalWorld_)
     63    //    //{
     64    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
     65    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
     66    //    //}
     67    //    this->node_->setPosition(position);
     68    //    positionChanged();
     69    //}
     70
     71    //void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
     72    //{
     73    //    //if (isDynamic() && bAddedToPhysicalWorld_)
     74    //    //{
     75    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
     76    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
     77    //    //}
     78    //    this->node_->translate(distance, relativeTo);
     79    //    positionChanged();
     80    //}
     81
     82    //void MovableEntity::setOrientation(const Quaternion& orientation)
     83    //{
     84    //    //if (isDynamic() && bAddedToPhysicalWorld_)
     85    //    //{
     86    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
     87    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
     88    //    //}
     89    //    this->node_->setOrientation(orientation);
     90    //    orientationChanged();
     91    //}
     92
     93    //void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
     94    //{
     95    //    //if (isDynamic() && bAddedToPhysicalWorld_)
     96    //    //{
     97    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
     98    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
     99    //    //}
     100    //    this->node_->rotate(rotation, relativeTo);
     101    //    orientationChanged();
     102    //}
     103
     104    //void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
     105    //{
     106    //    //if (isDynamic() && bAddedToPhysicalWorld_)
     107    //    //{
     108    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
     109    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
     110    //    //}
     111    //    this->node_->yaw(angle, relativeTo);
     112    //    orientationChanged();
     113    //}
     114
     115    //void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
     116    //{
     117    //    //if (isDynamic() && bAddedToPhysicalWorld_)
     118    //    //{
     119    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
     120    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
     121    //    //}
     122    //    this->node_->pitch(angle, relativeTo);
     123    //    orientationChanged();
     124    //}
     125
     126    //void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
     127    //{
     128    //    //if (isDynamic() && bAddedToPhysicalWorld_)
     129    //    //{
     130    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
     131    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
     132    //    //}
     133    //    this->node_->roll(angle, relativeTo);
     134    //    orientationChanged();
     135    //}
     136
     137    //void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
     138    //{
     139    //    //if (isDynamic() && bAddedToPhysicalWorld_)
     140    //    //{
     141    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
     142    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
     143    //    //}
     144    //    this->node_->lookAt(target, relativeTo, localDirectionVector);
     145    //    orientationChanged();
     146    //}
     147
     148    //void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
     149    //{
     150    //    //if (isDynamic() && bAddedToPhysicalWorld_)
     151    //    //{
     152    //    //    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
     153    //    //    this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
     154    //    //}
     155    //    this->node_->setDirection(direction, relativeTo, localDirectionVector);
     156    //    orientationChanged();
     157    //}
     158
     159    void MovableEntity::setPosition(const Vector3& position)
     160    {
     161        if (this->isDynamic())
     162        {
     163            btTransform transf = this->physicalBody_->getWorldTransform();
     164            transf.setOrigin(btVector3(position.x, position.y, position.z));
     165            this->physicalBody_->setWorldTransform(transf);
     166        }
     167        else
     168        {
     169            // no physics, we do it ourselves
     170            this->node_->setPosition(position);
     171        }
     172    }
     173
     174    void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
     175    {
     176        if (this->isDynamic())
     177        {
     178            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot translate physical object relative \
     179                                                          to any other space than TS_LOCAL.");
     180            this->physicalBody_->translate(btVector3(distance.x, distance.y, distance.z));
     181        }
     182        else
     183        {
     184            // no physics, we do it ourselves
     185            this->node_->translate(distance, relativeTo);
     186        }
     187    }
     188
     189    void MovableEntity::setOrientation(const Quaternion& orientation)
     190    {
     191        if (this->isDynamic())
     192        {
     193            btTransform transf = this->physicalBody_->getWorldTransform();
     194            transf.setRotation(btQuaternion(orientation.w, orientation.x, orientation.y, orientation.z));
     195            this->physicalBody_->setWorldTransform(transf);
     196        }
     197        else
     198        {
     199            // no physics, we do it ourselves
     200            this->node_->setOrientation(orientation);
     201        }
     202    }
     203
     204    void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
     205    {
     206        if (this->isDynamic())
     207        {
     208            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot rotate physical object relative \
     209                                                          to any other space than TS_LOCAL.");
     210            btTransform transf = this->physicalBody_->getWorldTransform();
     211            this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation.w, rotation.x, rotation.y, rotation.z)));
     212        }
     213        else
     214        {
     215            // no physics, we do it ourselves
     216            this->node_->rotate(rotation, relativeTo);
     217        }
     218    }
     219
     220    void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
     221    {
     222        if (this->isDynamic())
     223        {
     224            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot yaw physical object relative \
     225                                                          to any other space than TS_LOCAL.");
     226            btTransform transf = this->physicalBody_->getWorldTransform();
     227            btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f));
     228            this->physicalBody_->setWorldTransform(transf * rotation);
     229        }
     230        else
     231        {
     232            // no physics, we do it ourselves
     233            this->node_->yaw(angle, relativeTo);
     234        }
     235    }
     236
     237    void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
     238    {
     239        if (this->isDynamic())
     240        {
     241            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot pitch physical object relative \
     242                                                          to any other space than TS_LOCAL.");
     243            btTransform transf = this->physicalBody_->getWorldTransform();
     244            btTransform rotation(btQuaternion(0.0f, angle.valueRadians(), 0.0f));
     245            this->physicalBody_->setWorldTransform(transf * rotation);
     246        }
     247        else
     248        {
     249            // no physics, we do it ourselves
     250            this->node_->pitch(angle, relativeTo);
     251        }
     252    }
     253
     254    void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
     255    {
     256        if (this->isDynamic())
     257        {
     258            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot roll physical object relative \
     259                                                          to any other space than TS_LOCAL.");
     260            btTransform transf = this->physicalBody_->getWorldTransform();
     261            btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f));
     262            this->physicalBody_->setWorldTransform(transf * rotation);
     263        }
     264        else
     265        {
     266            // no physics, we do it ourselves
     267            this->node_->roll(angle, relativeTo);
     268        }
     269    }
     270
     271    void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
     272    {
     273        if (this->isDynamic())
     274        {
     275            ThrowException(NotImplemented, "ControllableEntity::lookAt() is not yet supported for physical objects.");
     276            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \
     277                                                          to any other space than TS_LOCAL.");
     278            //btTransform transf = this->physicalBody_->getWorldTransform();
     279            //this->physicalBody_->setWorldTransform(transf);
     280        }
     281        else
     282        {
     283            // no physics, we do it ourselves
     284            this->node_->lookAt(target, relativeTo, localDirectionVector);
     285        }
     286    }
     287
     288    void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
     289    {
     290        if (this->isDynamic())
     291        {
     292            ThrowException(NotImplemented, "ControllableEntity::lookAt() is not yet supported for physical objects.");
     293            OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \
     294                                                          to any other space than TS_LOCAL.");
     295            //btTransform transf = this->physicalBody_->getWorldTransform();
     296            //this->physicalBody_->setWorldTransform(transf);
     297        }
     298        else
     299        {
     300            // no physics, we do it ourselves
     301            this->node_->setDirection(direction, relativeTo, localDirectionVector);
     302        }
     303    }
     304
     305    void MovableEntity::setWorldTransform(const btTransform& worldTrans)
     306    {
     307        // We use a dynamic body. So we translate our node accordingly.
     308        this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z()));
     309        this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z()));
     310        const btVector3& velocity = this->physicalBody_->getLinearVelocity();
     311        internalSetVelocity(Vector3(velocity.x(), velocity.y(), velocity.z()));
    66312        positionChanged();
    67     }
    68 
    69     void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo){
    70         if(isDynamic() && bAddedToPhysicalWorld_){
    71             this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
    72             this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    73         }
    74         this->node_->translate(distance, relativeTo);
    75         translateChanged();
    76     }
    77 
    78     void MovableEntity::setOrientation(const Quaternion& orientation){
    79         if(isDynamic() && bAddedToPhysicalWorld_){
    80             this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
    81             this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    82         }
    83         this->node_->setOrientation(orientation);
    84313        orientationChanged();
    85314    }
    86315
    87     void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo){
    88         if(isDynamic() && bAddedToPhysicalWorld_){
    89             this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
    90             this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    91         }
    92         this->node_->rotate(rotation, relativeTo);
    93         rotateChanged();
    94     }
    95 
    96     void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo){
    97         if(isDynamic() && bAddedToPhysicalWorld_){
    98             this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
    99             this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    100         }
    101         this->node_->yaw(angle, relativeTo);
    102         yawChanged();
    103     }
    104 
    105     void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo){
    106         if(isDynamic() && bAddedToPhysicalWorld_){
    107             this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
    108             this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    109         }
    110         this->node_->pitch(angle, relativeTo);
    111         pitchChanged();
    112     }
    113 
    114     void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo){
    115         if(isDynamic() && bAddedToPhysicalWorld_){
    116             this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
    117             this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    118         }
    119         this->node_->roll(angle, relativeTo);
    120         rollChanged();
    121     }
    122 
    123     void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector){
    124         if(isDynamic() && bAddedToPhysicalWorld_){
    125             this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
    126             this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    127         }
    128         this->node_->lookAt(target, relativeTo, localDirectionVector);
    129         lookAtChanged();
    130     }
    131 
    132     void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector){
    133         if(isDynamic() && bAddedToPhysicalWorld_){
    134             this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
    135             this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    136         }
    137         this->node_->setDirection(direction, relativeTo, localDirectionVector);
    138         directionChanged();
    139     }
    140 
     316    void MovableEntity::getWorldTransform(btTransform& worldTrans) const
     317    {
     318        // We use a kinematic body
     319        worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
     320        worldTrans.setRotation(btQuaternion(node_->getOrientation().w, node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z));
     321    }
    141322}
  • code/branches/physics/src/orxonox/objects/worldentities/MovableEntity.h

    r2201 r2292  
    6969
    7070        private:
    71 
    72             virtual void rollChanged() { }
    73             virtual void positionChanged() { }
    74             virtual void translateChanged() { }
    75             virtual void orientationChanged() { }
    76             virtual void rotateChanged() { }
    77             virtual void yawChanged() { }
    78             virtual void pitchChanged() { }
    79             virtual void lookAtChanged() { }
    80             virtual void directionChanged( ) { }
    81 
    8271            //void attachPhysicalObject(WorldEntity* object);
    8372
    84             //// Bullet btMotionState related
    85             //void setWorldTransform(const btTransform& worldTrans)
    86             //{
    87             //    this->node_->setPosition(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z());
    88             //    this->node_->setOrientation(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z());
    89             //    //this->velocity_.x = this->physicalBody_->
    90             //}
     73            virtual void internalSetVelocity(const Vector3& velocity) { }
     74            virtual void positionChanged() { }
     75            virtual void orientationChanged() { }
    9176
    92             //// Bullet btMotionState related
    93             //void getWorldTransform(btTransform& worldTrans) const
    94             //{
    95             //    worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
    96             //    worldTrans.setRotation(btQuaternion(node_->getOrientation().w, node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z));
    97             //}
     77            // Bullet btMotionState related
     78            void setWorldTransform(const btTransform& worldTrans);
     79            void getWorldTransform(btTransform& worldTrans) const;
    9880    };
    9981}
  • code/branches/physics/src/orxonox/objects/worldentities/PositionableEntity.h

    r2192 r2292  
    4343            void registerVariables();
    4444
    45             using WorldEntity::setPosition;
    46             using WorldEntity::translate;
    47             using WorldEntity::setOrientation;
    48             using WorldEntity::rotate;
    49             using WorldEntity::yaw;
    50             using WorldEntity::pitch;
    51             using WorldEntity::roll;
    52             using WorldEntity::lookAt;
    53             using WorldEntity::setDirection;
    54 
    55             inline void setPosition(const Vector3& position)
    56                 { this->node_->setPosition(position); }
    57             inline void translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
    58                 { this->node_->translate(distance, relativeTo); }
    59             inline void setOrientation(const Quaternion& orientation)
    60                 { this->node_->setOrientation(orientation); }
    61             inline void rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
    62                 { this->node_->rotate(rotation, relativeTo); }
    63             inline void yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
    64                 { this->node_->yaw(angle, relativeTo); }
    65             inline void pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
    66                 { this->node_->pitch(angle, relativeTo); }
    67             inline void roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL)
    68                 { this->node_->roll(angle, relativeTo); }
    69             inline void lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z)
    70                 { this->node_->lookAt(target, relativeTo, localDirectionVector); }
    71             inline void setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z)
    72                 { this->node_->setDirection(direction, relativeTo, localDirectionVector); }
    73 
    7445        private:
    7546            //void attachPhysicalObject(WorldEntity* object);
  • code/branches/physics/src/orxonox/objects/worldentities/StaticEntity.cc

    r2192 r2292  
    2929#include "OrxonoxStableHeaders.h"
    3030#include "StaticEntity.h"
     31
     32#include "util/Exception.h"
    3133#include "core/CoreIncludes.h"
    3234
     
    5759        REGISTERDATA(this->getOrientation().z, network::direction::toclient);
    5860    }
     61
     62    void StaticEntity::setCollisionType(CollisionType type)
     63    {
     64        if (!this->physicalBody_)
     65            return;
     66        if (type != Static)
     67            ThrowException(Argument, "Cannot tell a StaticEntity to be kinematic or dynamic");
     68
     69        this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() & !btCollisionObject::CF_KINEMATIC_OBJECT | btCollisionObject::CF_STATIC_OBJECT);
     70    }
     71
     72    void StaticEntity::setWorldTransform(const btTransform& worldTrans)
     73    {
     74        OrxAssert(false, "Setting world transform of a StaticEntity, which is static!");
     75        //COUT(0) << "Setting world transform of a StaticEntity, which is static!" << std::endl;
     76    }
     77
     78    void StaticEntity::getWorldTransform(btTransform& worldTrans) const
     79    {
     80        worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
     81        worldTrans.setRotation(btQuaternion(node_->getOrientation().w, node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z));
     82    }
    5983}
  • code/branches/physics/src/orxonox/objects/worldentities/StaticEntity.h

    r2192 r2292  
    7171            inline void setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z)
    7272                { this->node_->setDirection(direction, relativeTo, localDirectionVector); }
     73
     74        private:
     75
     76            void setCollisionType(CollisionType type);
     77
     78            // Bullet btMotionState related
     79            void setWorldTransform(const btTransform& worldTrans);
     80            void getWorldTransform(btTransform& worldTrans) const;
    7381    };
    7482}
  • code/branches/physics/src/orxonox/objects/worldentities/WorldEntity.cc

    r2201 r2292  
    3232#include <cassert>
    3333#include <OgreSceneManager.h>
     34
    3435#include "BulletCollision/CollisionShapes/btSphereShape.h"
    3536
     37#include "util/Exception.h"
     38#include "util/Convert.h"
    3639#include "core/CoreIncludes.h"
    3740#include "core/XMLPort.h"
    38 #include "util/Convert.h"
    3941
    4042#include "objects/Scene.h"
     43
     44#include "StaticEntity.h"
    4145
    4246namespace orxonox
     
    6569
    6670        // Default behaviour does not include physics
    67         this->bAddedToPhysicalWorld_ = false;
    6871        this->physicalBody_ = 0;
    6972
     
    8285            if (this->physicalBody_)
    8386            {
    84                 if (this->bAddedToPhysicalWorld_)
     87                if (this->physicalBody_->isInWorld())
    8588                    this->getScene()->getPhysicalWorld()->removeRigidBody(this->physicalBody_);
    8689                if (this->physicalBody_->getCollisionShape())
     
    104107        XMLPortParamTemplate(WorldEntity, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&);
    105108        XMLPortParam(WorldEntity, "scale", setScale, getScale, xmlelement, mode);
    106         XMLPortParam(WorldEntity, "collisionRadius", setcollisionRadius, getcollisionRadius, xmlelement, mode);
     109
     110        XMLPortParam(WorldEntity, "collisionRadius", setCollisionRadius, getCollisionRadius, xmlelement, mode);
     111        XMLPortParam(WorldEntity, "collisionType", setCollisionTypeStr, getCollisionTypeStr, xmlelement, mode);
     112        XMLPortParam(WorldEntity, "mass", setMass, getMass, xmlelement, mode);
     113
     114        if (this->physicalBody_)
     115            this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    107116
    108117        XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode);
     
    145154
    146155        // Do the physical connection if required
    147         this->attachPhysicalObject(object);
    148     }
    149 
    150     void WorldEntitiy::attachPhysicalObject(WorldEntity* object){
    151     //function attachhysicalObject
    152         StaticEntity* staticObject = dynamic_cast<WorldEntity*>(object);
    153         if (staticObject != 0 && hasPhysics()){
    154            btCompountShape* compoundShape = dynamic_cast<btCompoundShape*>(physicalBody_->getCollisionShape());
    155            if(compoundShape == 0){
    156                 //NEW
    157                 btCompoundShape* newShape = new btCompoundShape();
    158                 newShape->addChildShape(this->physcialBody_->getCollisionShape());
    159                 newShape->addChildShape(staticObject->getCollisionShape());
    160                 this->physicalBody_->setCollisionShape();
    161            }
    162            else{
    163                compoundShape -> addChildShape(staticObject->getCollisionShape());
    164            }
    165         }
    166     }
     156        //this->attachPhysicalObject(object);
     157    }
     158
     159    //void WorldEntity::attachPhysicalObject(WorldEntity* object)
     160    //{
     161    //    StaticEntity* staticObject = dynamic_cast<StaticEntity*>(object);
     162    //    if (staticObject != 0 && this->hasPhysics())
     163    //    {
     164    //       btCompoundShape* compoundShape = dynamic_cast<btCompoundShape*>(this->physicalBody_->getCollisionShape());
     165    //       if (compoundShape == 0)
     166    //       {
     167    //            // create a compound shape and add both
     168    //            compoundShape = new btCompoundShape();
     169    //            compoundShape->addChildShape(this->physicalBody_->getCollisionShape());
     170    //            compoundShape->addChildShape(staticObject->getCollisionShape());
     171    //            this->physicalBody_->setCollisionShape();
     172    //       }
     173    //       else
     174    //       {
     175    //           compoundShape -> addChildShape(staticObject->getCollisionShape());
     176    //       }
     177    //    }
     178    //}
    167179
    168180    void WorldEntity::detach(WorldEntity* object)
     
    193205        btRigidBody::btRigidBodyConstructionInfo bodyConstructionInfo(0, this, 0, btVector3(0,0,0));
    194206        this->physicalBody_ = new btRigidBody(bodyConstructionInfo);
    195         this->getScene()->getPhysicalWorld()->addRigidBody(this->physicalBody_);
    196         this->bAddedToPhysicalWorld_ = true;
    197     }
    198 
    199     void WorldEntity::setcollisionRadius(float radius)
     207    }
     208
     209    float WorldEntity::getMass()
     210    {
     211        if (!this->physicalBody_)
     212            return 0.0f;
     213
     214        return 1.0f/this->physicalBody_->getInvMass();
     215    }
     216
     217    void WorldEntity::setMass(float mass)
     218    {
     219        if (!this->physicalBody_)
     220            this->createPhysicalBody();
     221
     222        this->physicalBody_->setMassProps(mass, btVector3(0,0,0));
     223    }
     224
     225    void WorldEntity::setCollisionType(WorldEntity::CollisionType type)
     226    {
     227        if (!this->physicalBody_)
     228            this->createPhysicalBody();
     229
     230        switch (type)
     231        {
     232        case Dynamic:
     233            this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() & !(btCollisionObject::CF_STATIC_OBJECT | btCollisionObject::CF_KINEMATIC_OBJECT));
     234            break;
     235        case Kinematic:
     236            this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() & !btCollisionObject::CF_STATIC_OBJECT | btCollisionObject::CF_KINEMATIC_OBJECT);
     237            break;
     238        case Static:
     239            this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() & !btCollisionObject::CF_KINEMATIC_OBJECT | btCollisionObject::CF_STATIC_OBJECT);
     240            break;
     241        }
     242    }
     243
     244    WorldEntity::CollisionType WorldEntity::getCollisionType()
     245    {
     246        if (!this->physicalBody_)
     247            ThrowException(Argument, "Cannot retrieve collision type of a non physical object.");
     248
     249        int flags = this->physicalBody_->getCollisionFlags();
     250        if (flags & btCollisionObject::CF_STATIC_OBJECT)
     251            return Static;
     252        else if (flags & btCollisionObject::CF_KINEMATIC_OBJECT)
     253            return Kinematic;
     254        else
     255            return Dynamic;
     256    }
     257
     258    void WorldEntity::setCollisionTypeStr(const std::string& type)
     259    {
     260        std::string lower = getLowercase(type);
     261        if (lower == "dynamic")
     262            setCollisionType(Dynamic);
     263        else if (lower == "static")
     264            setCollisionType(Static);
     265        else if (lower == "kinematic")
     266            setCollisionType(Kinematic);
     267        else
     268            ThrowException(Argument, std::string("Trying to set an unknown collision type: '") + type + "'.");
     269    }
     270
     271    std::string WorldEntity::getCollisionTypeStr()
     272    {
     273        switch (this->getCollisionType())
     274        {
     275        case Dynamic:
     276            return "dynamic";
     277        case Kinematic:
     278            return "kinematic";
     279        case Static:
     280            return "static";
     281        default:
     282            ThrowException(Argument, "Encountered unknown collision Type.");
     283        }
     284    }
     285
     286    void WorldEntity::setCollisionRadius(float radius)
    200287    {
    201288        if (!this->physicalBody_)
    202289            createPhysicalBody();
    203290
    204         // destroy old onw first
     291        // destroy old one first
    205292        btCollisionShape* oldShape = this->physicalBody_->getCollisionShape();
    206293        if (oldShape)
     
    210297    }
    211298
    212     float WorldEntity::getcollisionRadius()
     299    float WorldEntity::getCollisionRadius()
    213300    {
    214301        if (this->physicalBody_)
  • code/branches/physics/src/orxonox/objects/worldentities/WorldEntity.h

    r2192 r2292  
    4646    class _OrxonoxExport WorldEntity : public BaseObject, public network::Synchronisable, public btMotionState
    4747    {
     48        public:
     49            enum CollisionType
     50            {
     51                Dynamic,
     52                Kinematic,
     53                Static
     54            };
     55
    4856        public:
    4957            WorldEntity(BaseObject* creator);
     
    127135                { this->node_->scale(scale, scale, scale); }
    128136
    129             void setcollisionRadius(float radius);
    130             float getcollisionRadius();
    131 
    132137            bool hasPhysics()  { return this->physicalBody_; }
    133138            bool isKinematic() { return this->physicalBody_ && this->physicalBody_->isKinematicObject(); }
     
    147152                { return this->parent_; }
    148153
     154            void setCollisionRadius(float radius);
     155            float getCollisionRadius();
     156
     157            void setCollisionTypeStr(const std::string& type);
     158            std::string getCollisionTypeStr();
     159
     160            void setMass(float mass);
     161            float getMass();
     162
     163            CollisionType getCollisionType();
     164
    149165        protected:
    150166            //virtual btCollisionShape* getCollisionShape() = 0;
    151167
    152168            void createPhysicalBody();
    153             virtual void attachPhysicalObject(WorldEntity* object) { }
     169            //virtual void attachPhysicalObject(WorldEntity* object);
     170            virtual void setCollisionType(CollisionType type);
    154171
    155172            Ogre::SceneNode* node_;
    156             bool bAddedToPhysicalWorld_;
    157173            btRigidBody* physicalBody_;
    158174
     
    171187                { this->roll(angle); }
    172188
    173             // Bullet btMotionState related
    174             virtual void setWorldTransform(const btTransform& worldTrans)
    175             {
    176             }
    177 
    178             // Bullet btMotionState related
    179             virtual void getWorldTransform(btTransform& worldTrans) const
    180             {
    181             }
    182 
    183189            WorldEntity* parent_;
    184190            unsigned int parentID_;
  • code/branches/physics/src/orxonox/objects/worldentities/pawns/SpaceShip.cc

    r2087 r2292  
    3030#include "SpaceShip.h"
    3131
     32#include "util/Math.h"
     33#include "util/Exception.h"
    3234#include "core/CoreIncludes.h"
    3335#include "core/ConfigValueIncludes.h"
    3436#include "core/XMLPort.h"
    35 #include "util/Math.h"
    3637
    3738namespace orxonox
     
    5960
    6061        this->setDestroyWhenPlayerLeft(true);
     62
     63        // create a phyisical body
     64        OrxAssert(this->physicalBody_ == 0, " Check why there is already a physical body in the space ship.");
     65        this->createPhysicalBody();
    6166
    6267        this->setConfigValues();
     
    165170    void SpaceShip::moveFrontBack(const Vector2& value)
    166171    {
    167         this->acceleration_.z = -this->translationAcceleration_ * value.x;
     172        assert(this->physicalBody_);
     173        this->physicalBody_->applyCentralForce(btVector3(0.0f, 0.0f, -1.0f/this->physicalBody_->getInvMass() * value.x));
     174//        this->acceleration_.z = -this->translationAcceleration_ * value.x;
    168175    }
    169176
    170177    void SpaceShip::moveRightLeft(const Vector2& value)
    171178    {
    172         this->acceleration_.x = this->translationAcceleration_ * value.x;
     179        this->physicalBody_->applyCentralForce(btVector3(1.0f/this->physicalBody_->getInvMass() * value.x, 0.0f, 0.0f));
     180//        this->acceleration_.x = this->translationAcceleration_ * value.x;
    173181    }
    174182
    175183    void SpaceShip::moveUpDown(const Vector2& value)
    176184    {
    177         this->acceleration_.y = this->translationAcceleration_ * value.x;
     185        this->physicalBody_->applyCentralForce(btVector3(0.0f, 1.0f/this->physicalBody_->getInvMass() * value.x, 0.0f));
     186//        this->acceleration_.y = this->translationAcceleration_ * value.x;
    178187    }
    179188
Note: See TracChangeset for help on using the changeset viewer.