Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 14, 2008, 4:16:52 PM (16 years ago)
Author:
rgrieder
Message:

Finally merged physics stuff. Target is physics_merge because I'll have to do some testing first.

Location:
code/branches/physics_merge
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/physics_merge

  • code/branches/physics_merge/src/orxonox/objects/Scene.cc

    r2371 r2442  
    2323 *      Fabian 'x3n' Landau
    2424 *   Co-authors:
    25  *      ...
     25 *      Reto Grieder (physics)
    2626 *
    2727 */
     
    3535#include <OgreLight.h>
    3636
     37#include "BulletCollision/BroadphaseCollision/btAxisSweep3.h"
     38#include "BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h"
     39#include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
     40#include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
     41
    3742#include "core/CoreIncludes.h"
    3843#include "core/Core.h"
    3944#include "core/XMLPort.h"
     45#include "tools/BulletConversions.h"
     46#include "objects/worldentities/WorldEntity.h"
    4047
    4148namespace orxonox
     
    6976            this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
    7077        }
     78
     79        // No physics for default
     80        this->physicalWorld_ = 0;
    7181
    7282        // test test test
     
    91101            if (Ogre::Root::getSingletonPtr())
    92102            {
    93 //                this->sceneManager_->destroySceneNode(this->rootSceneNode_->getName()); // TODO: remove getName() for newer versions of Ogre
    94103                Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_);
    95104            }
     
    109118        XMLPortParam(Scene, "shadow", setShadow, getShadow, xmlelement, mode).defaultValues(true);
    110119
     120        //const int defaultMaxWorldSize = 100000;
     121        //Vector3 worldAabbMin(-defaultMaxWorldSize, -defaultMaxWorldSize, -defaultMaxWorldSize);
     122        //Vector3 worldAabbMax( defaultMaxWorldSize,  defaultMaxWorldSize,  defaultMaxWorldSize);
     123        //XMLPortParamVariable(Scene, "negativeWorldRange", worldAabbMin, xmlelement, mode);
     124        //XMLPortParamVariable(Scene, "positiveWorldRange", worldAabbMax, xmlelement, mode);
     125        XMLPortParam(Scene, "hasPhysics", setPhysicalWorld, hasPhysics, xmlelement, mode).defaultValue(0, true);//.defaultValue(1, worldAabbMin).defaultValue(2, worldAabbMax);
     126
    111127        XMLPortObjectExtended(Scene, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
    112128    }
     
    114130    void Scene::registerVariables()
    115131    {
    116         registerVariable(this->skybox_,     variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applySkybox));
     132        registerVariable(this->skybox_,       variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applySkybox));
    117133        registerVariable(this->ambientLight_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applyAmbientLight));
     134        registerVariable(this->bHasPhysics_,  variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_hasPhysics));
     135    }
     136
     137    void Scene::setPhysicalWorld(bool wantPhysics)//, const Vector3& worldAabbMin, const Vector3& worldAabbMax)
     138    {
     139        this->bHasPhysics_ = wantPhysics;
     140        if (wantPhysics && !hasPhysics())
     141        {
     142            //float x = worldAabbMin.x;
     143            //float y = worldAabbMin.y;
     144            //float z = worldAabbMin.z;
     145            btVector3 worldAabbMin(-100000, -100000, -100000);
     146            //x = worldAabbMax.x;
     147            //y = worldAabbMax.y;
     148            //z = worldAabbMax.z;
     149            btVector3 worldAabbMax(100000, 100000, 100000);
     150
     151            btDefaultCollisionConfiguration*     collisionConfig = new btDefaultCollisionConfiguration();
     152            btCollisionDispatcher*               dispatcher      = new btCollisionDispatcher(collisionConfig);
     153            bt32BitAxisSweep3*                   broadphase      = new bt32BitAxisSweep3(worldAabbMin,worldAabbMax);
     154            btSequentialImpulseConstraintSolver* solver          = new btSequentialImpulseConstraintSolver;
     155
     156            this->physicalWorld_ =  new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfig);
     157
     158            // Disable Gravity for space
     159            this->physicalWorld_->setGravity(btVector3(0,0,0));
     160        }
     161        else
     162        {
     163            // TODO: Destroy Bullet physics
     164        }
     165    }
     166
     167    void Scene::tick(float dt)
     168    {
     169        if (!Core::showsGraphics())
     170        {
     171            // We need to update the scene nodes if we don't render
     172            this->rootSceneNode_->_update(true, false);
     173        }
     174        if (physicalWorld_)
     175        {
     176            if (this->physicsQueue_.size() > 0)
     177            {
     178                // Add all scheduled WorldEntities
     179                for (std::set<btRigidBody*>::const_iterator it = this->physicsQueue_.begin();
     180                    it != this->physicsQueue_.end(); ++it)
     181                {
     182                    if (!(*it)->isInWorld())
     183                    {
     184                        //COUT(0) << "body position: " << omni_cast<Vector3>((*it)->getWorldTransform().getOrigin()) << std::endl;
     185                        //COUT(0) << "body velocity: " << omni_cast<Vector3>((*it)->getLinearVelocity()) << std::endl;
     186                        //COUT(0) << "body orientation: " << omni_cast<Quaternion>((*it)->getWorldTransform().getRotation()) << std::endl;
     187                        //COUT(0) << "body angular: " << omni_cast<Vector3>((*it)->getAngularVelocity()) << std::endl;
     188                        //COUT(0) << "body mass: " << omni_cast<float>((*it)->getInvMass()) << std::endl;
     189                        //COUT(0) << "body inertia: " << omni_cast<Vector3>((*it)->getInvInertiaDiagLocal()) << std::endl;
     190                        this->physicalWorld_->addRigidBody(*it);
     191                    }
     192                }
     193                this->physicsQueue_.clear();
     194            }
     195
     196            // TODO: This is not stable! If physics cannot be calculated real time anymore,
     197            //       framerate will drop exponentially.
     198            physicalWorld_->stepSimulation(dt,(int)(dt/0.0166666f + 1.0f));
     199        }
    118200    }
    119201
     
    165247    }
    166248
    167     void Scene::tick(float dt)
    168     {
    169         if (!Core::showsGraphics())
    170         {
    171             // We need to update the scene nodes if we don't render
    172             this->rootSceneNode_->_update(true, false);
     249    void Scene::addRigidBody(btRigidBody* body)
     250    {
     251        if (!this->physicalWorld_)
     252            COUT(1) << "Error: Cannot add WorldEntity body to physical Scene: No physics." << std::endl;
     253        else if (body)
     254            this->physicsQueue_.insert(body);
     255    }
     256
     257    void Scene::removeRigidBody(btRigidBody* body)
     258    {
     259        if (!this->physicalWorld_)
     260            COUT(1) << "Error: Cannot remove WorldEntity body from physical Scene: No physics." << std::endl;
     261        else if (body)
     262        {
     263            this->physicalWorld_->removeRigidBody(body);
     264            // Also check queue
     265            std::set<btRigidBody*>::iterator it = this->physicsQueue_.find(body);
     266            if (it != this->physicsQueue_.end())
     267                this->physicsQueue_.erase(it);
    173268        }
    174269    }
Note: See TracChangeset for help on using the changeset viewer.