Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 10, 2008, 1:38:17 PM (17 years ago)
Author:
rgrieder
Message:

Trying to synchronise phyiscs over the network.

  • Removed derivation of CollisionShape from WorldEntity (BaseObject instead).
File:
1 edited

Legend:

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

    r2306 r2374  
    3333
    3434#include "util/Debug.h"
     35#include "util/MathConvert.h"
    3536#include "util/Exception.h"
    3637#include "core/CoreIncludes.h"
     
    4546        RegisterObject(MovableEntity);
    4647
    47         this->velocity_ = Vector3::ZERO;
     48        this->linearAcceleration_  = Vector3::ZERO;
     49        this->linearVelocity_      = Vector3::ZERO;
     50        this->angularAcceleration_ = Vector3::ZERO;
     51        this->angularVelocity_     = Vector3::ZERO;
    4852
    4953        this->registerVariables();
     
    5862        SUPER(MovableEntity, XMLPort, xmlelement, mode);
    5963
    60         XMLPortParamTemplate(MovableEntity, "velocity", setVelocity, getVelocity, xmlelement, mode, const Vector3&);
     64        XMLPortParamTemplate(MovableEntity, "velocity",     setVelocity,     getVelocity,     xmlelement, mode, const Vector3&);
     65        XMLPortParamTemplate(MovableEntity, "rotationaxis", setRotationAxis, getRotationAxis, xmlelement, mode, const Vector3&);
     66        XMLPortParam(MovableEntity, "rotationrate", setRotationRate, getRotationRate, xmlelement, mode);
    6167    }
    6268
     
    6571    }
    6672
     73    void MovableEntity::tick(float dt)
     74    {
     75        if (this->isActive())
     76        {
     77            // Check whether Bullet doesn't do the physics for us
     78            if (!this->isDynamic())
     79            {
     80                // Linear part
     81                this->linearVelocity_.x += this->linearAcceleration_.x * dt;
     82                this->linearVelocity_.y += this->linearAcceleration_.y * dt;
     83                this->linearVelocity_.z += this->linearAcceleration_.z * dt;
     84                linearVelocityChanged(true);
     85                this->node_->translate(this->linearVelocity_ * dt);
     86                positionChanged(true);
     87
     88                // Angular part
     89                // Note: angularVelocity_ is a Quaternion with w = 0 while angularAcceleration_ is a Vector3
     90                this->angularVelocity_.x += angularAcceleration_.x * dt;
     91                this->angularVelocity_.y += angularAcceleration_.y * dt;
     92                this->angularVelocity_.z += angularAcceleration_.z * dt;
     93                angularVelocityChanged(true);
     94                // Calculate new orientation with quaternion derivative. This is about 30% faster than with angle/axis method.
     95                float mult = dt * 0.5;
     96                // TODO: this could be optimized by writing it out. The calls currently create 4 new Quaternions!
     97                Quaternion newOrientation(0.0f, this->angularVelocity_.x * mult, this->angularVelocity_.y * mult, this->angularVelocity_.z * mult);
     98                newOrientation = this->node_->getOrientation() + newOrientation * this->node_->getOrientation();
     99                newOrientation.normalise();
     100                this->node_->setOrientation(newOrientation);
     101                orientationChanged(true);
     102            }
     103        }
     104    }
     105
    67106    void MovableEntity::setPosition(const Vector3& position)
    68107    {
    69108        if (this->isDynamic())
    70109        {
     110            //if (this->isPhysicsRunning())
     111            //    return;
    71112            btTransform transf = this->physicalBody_->getWorldTransform();
    72113            transf.setOrigin(btVector3(position.x, position.y, position.z));
     
    74115        }
    75116
     117        //COUT(0) << "setting position: " << position << std::endl;
    76118        this->node_->setPosition(position);
    77         positionChanged();
     119        positionChanged(false);
    78120    }
    79121
     
    88130
    89131        this->node_->translate(distance, relativeTo);
    90         positionChanged();
     132        positionChanged(false);
    91133    }
    92134
     
    96138        {
    97139            btTransform transf = this->physicalBody_->getWorldTransform();
    98             transf.setRotation(btQuaternion(orientation.w, orientation.x, orientation.y, orientation.z));
     140            transf.setRotation(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w));
    99141            this->physicalBody_->setWorldTransform(transf);
    100142        }
    101143
    102144        this->node_->setOrientation(orientation);
    103         orientationChanged();
     145        orientationChanged(false);
    104146    }
    105147
     
    111153                                                          to any other space than TS_LOCAL.");
    112154            btTransform transf = this->physicalBody_->getWorldTransform();
    113             this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation.w, rotation.x, rotation.y, rotation.z)));
     155            this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w)));
    114156        }
    115157
    116158        this->node_->rotate(rotation, relativeTo);
    117         orientationChanged();
     159        orientationChanged(false);
    118160    }
    119161
     
    130172
    131173        this->node_->yaw(angle, relativeTo);
    132         orientationChanged();
     174        orientationChanged(false);
    133175    }
    134176
     
    145187
    146188        this->node_->pitch(angle, relativeTo);
    147         orientationChanged();
     189        orientationChanged(false);
    148190    }
    149191
     
    160202
    161203        this->node_->roll(angle, relativeTo);
    162         orientationChanged();
     204        orientationChanged(false);
    163205    }
    164206
     
    175217
    176218        this->node_->lookAt(target, relativeTo, localDirectionVector);
    177         orientationChanged();
     219        orientationChanged(false);
    178220    }
    179221
     
    190232
    191233        this->node_->setDirection(direction, relativeTo, localDirectionVector);
    192         orientationChanged();
     234        orientationChanged(false);
    193235    }
    194236
     
    196238    {
    197239        if (this->isDynamic())
    198         {
    199240            this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
    200         }
    201 
    202         this->velocity_ = velocity;
    203         velocityChanged();
     241
     242        this->linearVelocity_ = velocity;
     243        linearVelocityChanged(false);
     244    }
     245
     246    void MovableEntity::setAngularVelocity(const Vector3& velocity)
     247    {
     248        if (this->isDynamic())
     249            this->physicalBody_->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z));
     250
     251        this->angularVelocity_ = velocity;
     252        angularVelocityChanged(false);
     253    }
     254
     255    void MovableEntity::setAcceleration(const Vector3& acceleration)
     256    {
     257        if (this->isDynamic())
     258            this->physicalBody_->applyCentralForce(btVector3(acceleration.x * this->getMass(), acceleration.y * this->getMass(), acceleration.z * this->getMass()));
     259
     260        this->linearAcceleration_ = acceleration;
     261    }
     262
     263    void MovableEntity::setAngularAcceleration(const Vector3& acceleration)
     264    {
     265        if (this->isDynamic())
     266        {
     267            btVector3 inertia(btVector3(1, 1, 1) / this->physicalBody_->getInvInertiaDiagLocal());
     268            this->physicalBody_->applyTorque(btVector3(acceleration.x, acceleration.y, acceleration.z) * inertia);
     269        }
     270
     271        this->angularAcceleration_ = acceleration;
    204272    }
    205273
     
    220288        this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z()));
    221289        this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z()));
    222         const btVector3& velocity = this->physicalBody_->getLinearVelocity();
    223         this->velocity_.x = velocity.x();
    224         this->velocity_.y = velocity.y();
    225         this->velocity_.z = velocity.z();
    226         velocityChanged();
    227         positionChanged();
    228         orientationChanged();
     290        COUT(0) << "setting world transform: " << omni_cast<std::string>(node_->getPosition()) << std::endl;
     291        this->linearVelocity_.x = this->physicalBody_->getLinearVelocity().x();
     292        this->linearVelocity_.y = this->physicalBody_->getLinearVelocity().y();
     293        this->linearVelocity_.z = this->physicalBody_->getLinearVelocity().z();
     294        this->angularVelocity_.x = this->physicalBody_->getAngularVelocity().x();
     295        this->angularVelocity_.y = this->physicalBody_->getAngularVelocity().y();
     296        this->angularVelocity_.z = this->physicalBody_->getAngularVelocity().z();
     297        linearVelocityChanged(true);
     298        angularVelocityChanged(true);
     299        positionChanged(true);
     300        orientationChanged(true);
    229301    }
    230302
    231303    void MovableEntity::getWorldTransform(btTransform& worldTrans) const
    232304    {
    233         // We use a kinematic body 
     305        // We use a kinematic body
    234306        worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z));
    235         worldTrans.setRotation(btQuaternion(node_->getOrientation().w, node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z));
     307        worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w));
    236308        if (this->isDynamic())
    237309        {
    238310            // This function gets called only once for dynamic objects to set the initial conditions
    239             // We have to set the velocity too.
    240             this->physicalBody_->setLinearVelocity(btVector3(velocity_.x, velocity_.y, velocity_.z));
     311            // We have to set the velocities too.
     312            this->physicalBody_->setLinearVelocity(btVector3(linearVelocity_.x, linearVelocity_.y, linearVelocity_.z));
     313            this->physicalBody_->setAngularVelocity(btVector3(angularVelocity_.x, angularVelocity_.y, angularVelocity_.z));
    241314        }
    242315    }
Note: See TracChangeset for help on using the changeset viewer.