Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 28, 2008, 1:25:16 AM (15 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.