Changeset 2662 for code/trunk/src/orxonox/objects/worldentities
- Timestamp:
- Feb 14, 2009, 10:17:35 PM (16 years ago)
- Location:
- code/trunk
- Files:
-
- 2 deleted
- 39 edited
- 10 copied
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/objects/worldentities/Backlight.cc
- Property svn:mergeinfo changed
r2261 r2662 30 30 #include "Backlight.h" 31 31 32 #include <OgreBillboard.h>33 32 #include <OgreRibbonTrail.h> 34 33 #include <OgreSceneManager.h> 35 34 35 #include "core/Core.h" 36 36 #include "core/CoreIncludes.h" 37 #include "core/ConfigValueIncludes.h"38 37 #include "core/Executor.h" 39 #include "util/Math.h" 40 #include "GraphicsEngine.h" 38 #include "core/XMLPort.h" 39 #include "objects/Scene.h" 40 #include "util/Exception.h" 41 41 42 42 namespace orxonox … … 44 44 CreateFactory(Backlight); 45 45 46 float Backlight::timeFactor_s = 1.0; 47 48 Backlight::Backlight(float maxspeed, float brakingtime, float scale) 46 Backlight::Backlight(BaseObject* creator) : FadingBillboard(creator) 49 47 { 50 48 RegisterObject(Backlight); 51 49 52 this->setConfigValues(); 53 this->traillength_ = 1; 54 55 this->configure(maxspeed, brakingtime, scale); 56 } 57 58 bool Backlight::create(){ 59 if(!WorldEntity::create()) 60 return false; 61 62 this->getNode()->setInheritScale(false); 63 64 this->billboard_.setBillboardSet("Flares/backlightflare"); 65 this->attachObject(this->billboard_.getBillboardSet()); 66 67 this->ribbonTrail_ = GraphicsEngine::getInstance().getLevelSceneManager()->createRibbonTrail(this->getName() + "RibbonTrail"); 68 this->ribbonTrailNode_ = GraphicsEngine::getInstance().getLevelSceneManager()->getRootSceneNode()->createChildSceneNode(this->getName() + "RibbonTrailNode"); 69 this->ribbonTrailNode_->attachObject(this->ribbonTrail_); 70 this->ribbonTrail_->addNode(this->getNode()); 71 72 73 this->ribbonTrail_->setTrailLength(this->maxTraillength_); 74 this->ribbonTrail_->setMaterialName("Trail/backlighttrail"); 75 76 //this->setTimeFactor(Orxonox::getInstance().getTimeFactor()); 77 this->setTimeFactor(1.0f); 78 79 this->ribbonTrail_->setMaxChainElements(this->maxTrailsegments_); 80 this->ribbonTrail_->setTrailLength(this->traillength_ = 2 * this->maxTrailsegments_); 81 this->ribbonTrail_->setInitialWidth(0, this->width_ * this->getScale()); 82 this->ribbonTrail_->setWidthChange(0, this->width_ * this->getScale() / this->maxLifeTime_ * Backlight::timeFactor_s); 83 return true; 50 this->ribbonTrail_ = 0; 51 this->ribbonTrailNode_ = 0; 52 53 this->width_ = 0; 54 this->length_ = 1.0f; 55 this->lifetime_ = 0.001f; 56 this->maxelements_ = 1; 57 58 this->tickcount_ = 0; 59 60 if (Core::showsGraphics()) 61 { 62 if (!this->getScene()) 63 ThrowException(AbortLoading, "Can't create Backlight, no scene given."); 64 if (!this->getScene()->getSceneManager()) 65 ThrowException(AbortLoading, "Can't create Backlight, no scene manager given."); 66 if (!this->getScene()->getRootSceneNode()) 67 ThrowException(AbortLoading, "Can't create Backlight, no root scene node given."); 68 69 this->ribbonTrail_ = this->getScene()->getSceneManager()->createRibbonTrail(this->getNode()->getName()); 70 71 this->ribbonTrailNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); 72 this->ribbonTrailNode_->attachObject(this->ribbonTrail_); 73 74 this->ribbonTrail_->setMaxChainElements(this->maxelements_); 75 this->ribbonTrail_->setTrailLength(this->length_); 76 this->ribbonTrail_->setInitialWidth(0, 0); 77 } 78 79 this->registerVariables(); 84 80 } 85 81 … … 88 84 if (this->isInitialized()) 89 85 { 90 this->detachObject(this->billboard_.getBillboardSet()); 91 GraphicsEngine::getInstance().getLevelSceneManager()->destroySceneNode(this->getName() + "RibbonTrailNode"); 92 GraphicsEngine::getInstance().getLevelSceneManager()->destroyRibbonTrail(this->ribbonTrail_); 93 } 94 } 95 96 void Backlight::setConfigValues() 97 { 98 SetConfigValue(maxLifeTime_, 4.0).description("The maximal amount of seconds the trail behind a SpaceShip stays visible"); 99 SetConfigValue(trailSegmentLength_, 50).description("The length of one segment of the trail behind a SpaceShip (lower values make it more smooth)"); 100 SetConfigValue(width_, 7.0).description("The width of the trail"); 101 } 102 103 void Backlight::setTimeFactor(float factor) 104 { 105 Backlight::timeFactor_s = factor; 106 float change = Backlight::timeFactor_s / this->maxLifeTime_; 107 this->ribbonTrail_->setWidthChange(0, this->width_ * change); 108 this->updateColourChange(); 109 } 110 111 void Backlight::updateColourChange() 112 { 113 this->ribbonTrail_->setColourChange(0, ColourValue(0, 0, 0, this->maxTraillength_ / this->traillength_ / this->maxLifeTime_ * Backlight::timeFactor_s)); 114 } 115 116 117 void Backlight::XMLPort(Element& xmlelement, XMLPort::Mode mode){ 118 SUPER(Backlight, XMLPort, xmlelement, mode); 119 120 Backlight::create(); 86 if (this->ribbonTrail_) 87 { 88 if (this->ribbonTrailNode_) 89 { 90 this->ribbonTrailNode_->detachObject(this->ribbonTrail_); 91 this->getScene()->getSceneManager()->destroySceneNode(this->ribbonTrailNode_->getName()); 92 } 93 this->getScene()->getSceneManager()->destroyRibbonTrail(this->ribbonTrail_); 94 } 95 } 96 } 97 98 void Backlight::XMLPort(Element& xmlelement, XMLPort::Mode mode) 99 { 100 SUPER(Backlight, XMLPort, xmlelement, mode); 101 102 XMLPortParam(Backlight, "length", setLength, getLength, xmlelement, mode).defaultValues(100.0f); 103 XMLPortParam(Backlight, "width", setWidth, getWidth, xmlelement, mode).defaultValues(1.0f); 104 XMLPortParam(Backlight, "elements", setMaxElements, getMaxElements, xmlelement, mode).defaultValues(10); 105 XMLPortParam(Backlight, "lifetime", setLifetime, getLifetime, xmlelement, mode).defaultValues(1.0f); 106 XMLPortParam(Backlight, "trailmaterial", setTrailMaterial, getTrailMaterial, xmlelement, mode); 107 } 108 109 void Backlight::registerVariables() 110 { 111 registerVariable(this->width_, variableDirection::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_width)); 112 registerVariable(this->lifetime_, variableDirection::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_lifetime)); 113 registerVariable(this->length_, variableDirection::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_length)); 114 registerVariable(this->maxelements_, variableDirection::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_maxelements)); 115 registerVariable(this->trailmaterial_, variableDirection::toclient, new NetworkCallback<Backlight>(this, &Backlight::update_trailmaterial)); 116 } 117 118 void Backlight::changedColour() 119 { 120 FadingBillboard::changedColour(); 121 122 if (this->ribbonTrail_ && this->tickcount_ >= 2) 123 this->ribbonTrail_->setInitialColour(0, this->getFadedColour()); 124 } 125 126 void Backlight::update_width() 127 { 128 if (this->ribbonTrail_ && this->tickcount_ >= 2) 129 this->ribbonTrail_->setInitialWidth(0, this->width_ * this->getWorldScale()); 130 this->update_lifetime(); 131 } 132 133 void Backlight::update_lifetime() 134 { 135 if (this->ribbonTrail_ && this->tickcount_ >= 2) 136 { 137 this->ribbonTrail_->setWidthChange(0, this->width_ * this->getWorldScale() / this->lifetime_ * this->getTimeFactor()); 138 this->ribbonTrail_->setColourChange(0, 0, 0, 0, 1.0f / this->lifetime_ * this->getTimeFactor()); 139 } 140 } 141 142 void Backlight::update_length() 143 { 144 if (this->ribbonTrail_ && this->tickcount_ >= 2) 145 this->ribbonTrail_->setTrailLength(this->length_ * this->getWorldScale()); 146 } 147 148 void Backlight::update_maxelements() 149 { 150 if (this->ribbonTrail_ && this->tickcount_ >= 2) 151 this->ribbonTrail_->setMaxChainElements(this->maxelements_); 152 } 153 154 void Backlight::update_trailmaterial() 155 { 156 if (this->ribbonTrail_ && this->tickcount_ >= 2) 157 this->ribbonTrail_->setMaterialName(this->trailmaterial_); 158 } 159 160 void Backlight::changedVisibility() 161 { 162 SUPER(Backlight, changedVisibility); 163 164 if (this->ribbonTrail_) 165 this->ribbonTrail_->setVisible(this->isVisible()); 166 } 167 168 void Backlight::startturnonoff() 169 { 170 FadingBillboard::startturnonoff(); 171 172 if (this->ribbonTrail_ && this->isActive() && this->isVisible()) 173 this->ribbonTrail_->setVisible(true); 174 } 175 176 void Backlight::stopturnonoff() 177 { 178 this->postprocessingtime_ = max(0.0f, this->lifetime_ - this->turnofftime_); 179 180 FadingBillboard::stopturnonoff(); 181 182 if (this->ribbonTrail_) 183 this->ribbonTrail_->setInitialColour(0, this->getFadedColour()); 184 } 185 186 void Backlight::poststopturnonoff() 187 { 188 FadingBillboard::poststopturnonoff(); 189 190 if (this->ribbonTrail_) 191 this->ribbonTrail_->setVisible(false); 192 } 193 194 void Backlight::changedScale() 195 { 196 SUPER(Backlight, changedScale); 197 198 this->update_width(); 199 this->update_length(); 121 200 } 122 201 123 202 void Backlight::tick(float dt) 124 203 { 204 if (this->tickcount_ < 2) 205 { 206 ++this->tickcount_; 207 if (this->tickcount_ == 2) 208 { 209 this->changedColour(); 210 this->update_width(); 211 this->update_lifetime(); 212 this->update_length(); 213 this->update_maxelements(); 214 this->update_trailmaterial(); 215 if (this->ribbonTrail_) 216 this->ribbonTrail_->addNode(this->node_); 217 } 218 } 219 125 220 SUPER(Backlight, tick, dt); 126 221 127 if (this->isActive()) 128 { 129 if (this->traillength_ < this->maxTraillength_) 130 { 131 this->traillength_ = min<float>(this->maxTraillength_, this->traillength_ + dt * this->maxTraillength_ / this->maxLifeTime_); 132 this->updateColourChange(); 133 } 134 } 135 else 136 { 137 if (this->traillength_ > 1) 138 { 139 this->traillength_ = max<float>(1, this->traillength_ - this->brakefactor_ * dt * this->maxTraillength_ / this->maxLifeTime_); 140 this->updateColourChange(); 141 } 142 } 143 144 this->ribbonTrail_->setTrailLength(this->traillength_); 145 } 146 147 void Backlight::setColour(const ColourValue& colour) 148 { 149 this->billboard_.getBillboardSet()->getBillboard(0)->setColour(colour); 150 this->ribbonTrail_->setInitialColour(0, ColourValue(colour.r / 4 + 0.75, colour.g / 4 + 0.75, colour.b / 4 + 0.75)); 151 } 152 153 void Backlight::configure(float maxspeed, float brakingtime, float scale) 154 { 155 this->maxTraillength_ = this->maxLifeTime_ * maxspeed; 156 this->maxTrailsegments_ = (size_t)(this->maxTraillength_ / this->trailSegmentLength_); 157 158 this->brakefactor_ = this->maxLifeTime_ / brakingtime; 159 160 this->scale(scale); 161 } 162 163 void Backlight::changedVisibility() 164 { 165 SUPER(Backlight, changedVisibility); 166 167 this->billboard_.setVisible(this->isVisible()); 168 this->ribbonTrail_->setVisible(this->isVisible()); 222 if (this->ribbonTrail_ && this->changedirection_ != 0) 223 { 224 // we use alpha_blend, only adjust alpha 225 const ColourValue& colour = this->getColour(); 226 this->ribbonTrail_->setInitialColour(0, colour.r, colour.g, colour.b, this->getFadedColour().a); 227 } 228 } 229 230 void Backlight::changedTimeFactor(float factor_new, float factor_old) 231 { 232 this->update_lifetime(); 169 233 } 170 234 } -
code/trunk/src/orxonox/objects/worldentities/Backlight.h
- Property svn:mergeinfo changed
r2261 r2662 31 31 32 32 #include "OrxonoxPrereqs.h" 33 34 #include "PositionableEntity.h" 35 #include "tools/BillboardSet.h" 33 #include "FadingBillboard.h" 34 #include "gamestates/GSRoot.h" 36 35 37 36 namespace orxonox 38 37 { 39 class _OrxonoxExport Backlight : public PositionableEntity38 class _OrxonoxExport Backlight : public FadingBillboard, public TimeFactorListener 40 39 { 41 40 public: 42 Backlight( float maxspeed = 1.0, float brakingtime = 1.0, float scale = 1.0);41 Backlight(BaseObject* creator); 43 42 virtual ~Backlight(); 44 43 45 void setConfigValues();46 44 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 45 void registerVariables(); 46 47 47 virtual void tick(float dt); 48 48 virtual void changedVisibility(); 49 virtual bool create();50 49 51 void setColour(const ColourValue& colour); 52 void setTimeFactor(float factor); 50 inline void setWidth(float width) 51 { this->width_ = width; this->update_width(); } 52 inline float getWidth() const 53 { return this->width_; } 54 55 inline void setLifetime(float lifetime) 56 { this->lifetime_ = lifetime; this->update_lifetime(); } 57 inline float getLifetime() const 58 { return this->lifetime_; } 59 60 inline void setLength(float length) 61 { this->length_ = length; this->update_length(); } 62 inline float getLength() const 63 { return this->length_; } 64 65 inline void setMaxElements(size_t maxelements) 66 { this->maxelements_ = maxelements; this->update_maxelements(); } 67 inline size_t getMaxElements() const 68 { return this->maxelements_; } 69 70 inline void setTrailMaterial(const std::string& material) 71 { this->trailmaterial_ = material; this->update_trailmaterial(); } 72 inline const std::string& getTrailMaterial() const 73 { return this->trailmaterial_; } 74 75 virtual void changedScale(); 76 77 protected: 78 virtual void changedTimeFactor(float factor_new, float factor_old); 53 79 54 80 private: 55 void configure(float maxspeed, float brakingtime, float scale = 1); 56 void updateColourChange(); 81 virtual void startturnonoff(); 82 virtual void stopturnonoff(); 83 virtual void poststopturnonoff(); 84 virtual void changedColour(); 85 void update_width(); 86 void update_lifetime(); 87 void update_length(); 88 void update_maxelements(); 89 void update_trailmaterial(); 57 90 58 static float timeFactor_s; 59 BillboardSet billboard_; 91 Ogre::RibbonTrail* ribbonTrail_; 60 92 Ogre::SceneNode* ribbonTrailNode_; 61 Ogre::RibbonTrail* ribbonTrail_;62 63 float maxLifeTime_;64 float trailSegmentLength_;65 93 float width_; 66 67 float brakefactor_; 68 69 float maxTraillength_; 70 float traillength_; 71 72 size_t maxTrailsegments_; 94 float length_; 95 float lifetime_; 96 size_t maxelements_; 97 std::string trailmaterial_; 98 char tickcount_; 73 99 }; 74 100 } -
code/trunk/src/orxonox/objects/worldentities/Billboard.cc
r2171 r2662 30 30 #include "Billboard.h" 31 31 32 #include <OgreBillboardSet.h> 33 32 34 #include "core/CoreIncludes.h" 33 35 #include "core/XMLPort.h" 36 #include "core/Core.h" 34 37 #include "objects/Scene.h" 35 38 … … 38 41 CreateFactory(Billboard); 39 42 40 Billboard::Billboard(BaseObject* creator) : PositionableEntity(creator)43 Billboard::Billboard(BaseObject* creator) : StaticEntity(creator) 41 44 { 42 45 RegisterObject(Billboard); 46 47 this->material_ = ""; 48 this->colour_ = ColourValue::White; 43 49 44 50 this->registerVariables(); … … 50 56 { 51 57 if (this->isInitialized() && this->billboard_.getBillboardSet()) 52 this-> getNode()->detachObject(this->billboard_.getName());58 this->detachOgreObject(this->billboard_.getBillboardSet()); 53 59 } 54 60 } … … 64 70 void Billboard::registerVariables() 65 71 { 66 REGISTERSTRING(this->material_, direction::toclient, new NetworkCallback<Billboard>(this, &Billboard::changedMaterial));67 REGISTERDATA (this->colour_, direction::toclient, new NetworkCallback<Billboard>(this, &Billboard::changedColour));72 registerVariable(this->material_, variableDirection::toclient, new NetworkCallback<Billboard>(this, &Billboard::changedMaterial)); 73 registerVariable(this->colour_, variableDirection::toclient, new NetworkCallback<Billboard>(this, &Billboard::changedColour)); 68 74 } 69 75 70 76 void Billboard::changedMaterial() 71 77 { 78 if (this->material_ == "") 79 return; 80 72 81 if (!this->billboard_.getBillboardSet()) 73 82 { 74 if (this->getScene() && this->getScene()->getSceneManager())83 if (this->getScene() && Core::showsGraphics()) 75 84 { 76 85 this->billboard_.setBillboardSet(this->getScene()->getSceneManager(), this->material_, this->colour_, 1); 77 86 if (this->billboard_.getBillboardSet()) 78 this->getNode()->attachObject(this->billboard_.getBillboardSet());87 this->attachOgreObject(this->billboard_.getBillboardSet()); 79 88 this->billboard_.setVisible(this->isVisible()); 80 89 } … … 88 97 if (!this->billboard_.getBillboardSet()) 89 98 { 90 if (this->getScene() && this->getScene()->getSceneManager()) 99 /* 100 if (this->getScene() && Core::showsGraphics() && (this->material_ != "")) 91 101 { 92 102 this->billboard_.setBillboardSet(this->getScene()->getSceneManager(), this->material_, this->colour_, 1); 93 103 if (this->billboard_.getBillboardSet()) 94 this-> getNode()->attachObject(this->billboard_.getBillboardSet());104 this->attachOgreObject(this->billboard_.getBillboardSet()); 95 105 this->billboard_.setVisible(this->isVisible()); 96 106 } 107 */ 97 108 } 98 109 else -
code/trunk/src/orxonox/objects/worldentities/Billboard.h
r2087 r2662 31 31 32 32 #include "OrxonoxPrereqs.h" 33 #include " PositionableEntity.h"33 #include "StaticEntity.h" 34 34 #include "util/Math.h" 35 35 #include "tools/BillboardSet.h" … … 37 37 namespace orxonox 38 38 { 39 class _OrxonoxExport Billboard : public PositionableEntity39 class _OrxonoxExport Billboard : public StaticEntity 40 40 { 41 41 public: … … 61 61 { return this->colour_; } 62 62 63 protected: 64 inline BillboardSet& getBillboardSet() 65 { return this->billboard_; } 66 67 virtual void changedColour(); 68 63 69 private: 64 70 void changedMaterial(); 65 void changedColour();66 71 67 72 BillboardSet billboard_; -
code/trunk/src/orxonox/objects/worldentities/BlinkingBillboard.cc
r2171 r2662 68 68 void BlinkingBillboard::registerVariables() 69 69 { 70 // REGISTERDATA(this->amplitude_, direction::toclient);71 // REGISTERDATA(this->frequency_, direction::toclient);72 // REGISTERDATA(this->phase_, direction::toclient);70 // registerVariable(this->amplitude_, variableDirection::toclient); 71 // registerVariable(this->frequency_, variableDirection::toclient); 72 // registerVariable(this->phase_, variableDirection::toclient); 73 73 } 74 74 75 75 void BlinkingBillboard::tick(float dt) 76 76 { 77 SUPER(BlinkingBillboard, tick, dt); 78 77 79 if (Core::isMaster() && this->isActive()) 78 80 { -
code/trunk/src/orxonox/objects/worldentities/CMakeLists.txt
r2131 r2662 1 1 SET( SRC_FILES 2 2 WorldEntity.cc 3 PositionableEntity.cc3 StaticEntity.cc 4 4 MovableEntity.cc 5 MobileEntity.cc 5 6 ControllableEntity.cc 6 Model.cc 7 8 Backlight.cc 7 9 Billboard.cc 8 10 BlinkingBillboard.cc 11 ExplosionChunk.cc 12 FadingBillboard.cc 9 13 Light.cc 10 14 Camera.cc 11 15 CameraPosition.cc 12 SpawnPoint.cc16 Model.cc 13 17 ParticleEmitter.cc 14 18 ParticleSpawner.cc 15 # Backlight.cc 19 Planet.cc 20 SpawnPoint.cc 16 21 ) 17 22 -
code/trunk/src/orxonox/objects/worldentities/Camera.cc
- Property svn:mergeinfo changed
r2261 r2662 36 36 #include <OgreSceneManager.h> 37 37 #include <OgreSceneNode.h> 38 #include <OgreViewport.h>39 38 40 39 #include "util/Exception.h" … … 48 47 CreateFactory(Camera); 49 48 50 Camera::Camera(BaseObject* creator) : PositionableEntity(creator)49 Camera::Camera(BaseObject* creator) : StaticEntity(creator) 51 50 { 52 51 RegisterObject(Camera); 53 52 54 if (!this->getScene() || !this->getScene()->getSceneManager()) 55 ThrowException(AbortLoading, "Can't create Camera, no scene or no scene manager given."); 53 if (!this->getScene()) 54 ThrowException(AbortLoading, "Can't create Camera, no scene."); 55 if (!this->getScene()->getSceneManager()) 56 ThrowException(AbortLoading, "Can't create Camera, no scene-manager given."); 57 if (!this->getScene()->getRootSceneNode()) 58 ThrowException(AbortLoading, "Can't create Camera, no root-scene-node given."); 56 59 57 60 this->camera_ = this->getScene()->getSceneManager()->createCamera(getUniqueNumberString()); 58 this->getNode()->attachObject(this->camera_); 61 this->cameraNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode(); 62 this->attachNode(this->cameraNode_); 63 this->cameraNode_->attachObject(this->camera_); 59 64 60 65 this->bHasFocus_ = false; … … 66 71 this->setConfigValues(); 67 72 this->configvaluecallback_changedNearClipDistance(); 68 69 this->requestFocus(); // ! HACK ! REMOVE THIS !70 73 } 71 74 … … 75 78 { 76 79 this->releaseFocus(); 80 81 this->cameraNode_->detachAllObjects(); 82 this->getScene()->getSceneManager()->destroyCamera(this->camera_); 83 84 if (this->bDrag_) 85 this->detachNode(this->cameraNode_); 86 87 if (this->getScene()->getSceneManager()) 88 this->getScene()->getSceneManager()->destroySceneNode(this->cameraNode_->getName()); 77 89 } 78 90 } … … 90 102 void Camera::tick(float dt) 91 103 { 92 /* 93 // this stuff here may need some adjustments 94 float coeff = (this->bDrag_) ? min(1.0f, 15.0f * dt) : (1.0f); 104 SUPER(Camera, tick, dt); 95 105 96 Vector3 offset = this->getNode()->getWorldPosition() - this->cameraNode_->getWorldPosition(); 97 this->cameraNode_->translate(coeff * offset); 106 if (this->bDrag_) 107 { 108 // this stuff here may need some adjustments 109 float coeff = min(1.0f, 15.0f * dt); 98 110 99 this->cameraNode_->setOrientation(Quaternion::Slerp(coeff, this->cameraNode_->getWorldOrientation(), this->getWorldOrientation(), false)); 100 */ 111 Vector3 offset = this->getWorldPosition() - this->cameraNode_->getWorldPosition(); 112 this->cameraNode_->translate(coeff * offset); 113 114 this->cameraNode_->setOrientation(Quaternion::Slerp(coeff, this->cameraNode_->getWorldOrientation(), this->getWorldOrientation(), true)); 115 //this->cameraNode_->setOrientation(this->getWorldOrientation()); 116 } 101 117 } 102 118 … … 120 136 } 121 137 122 void Camera::setFocus( Ogre::Viewport* viewport)138 void Camera::setFocus() 123 139 { 124 140 this->bHasFocus_ = true; 125 viewport->setCamera(this->camera_); 141 CameraManager::getInstance().useCamera(this->camera_); 142 } 143 144 void Camera::setDrag(bool bDrag) 145 { 146 if (bDrag != this->bDrag_) 147 { 148 this->bDrag_ = bDrag; 149 150 if (!bDrag) 151 { 152 this->attachNode(this->cameraNode_); 153 this->cameraNode_->setPosition(Vector3::ZERO); 154 this->cameraNode_->setOrientation(Quaternion::IDENTITY); 155 } 156 else 157 { 158 this->detachNode(this->cameraNode_); 159 this->cameraNode_->setPosition(this->getWorldPosition()); 160 this->cameraNode_->setOrientation(this->getWorldOrientation()); 161 } 162 } 126 163 } 127 164 } -
code/trunk/src/orxonox/objects/worldentities/Camera.h
- Property svn:mergeinfo changed
r2261 r2662 33 33 34 34 #include <OgrePrerequisites.h> 35 #include "objects/worldentities/ PositionableEntity.h"35 #include "objects/worldentities/StaticEntity.h" 36 36 #include "objects/Tickable.h" 37 37 38 38 namespace orxonox 39 39 { 40 class _OrxonoxExport Camera : public PositionableEntity, public Tickable40 class _OrxonoxExport Camera : public StaticEntity, public Tickable 41 41 { 42 42 friend class CameraManager; … … 55 55 { return this->bHasFocus_; } 56 56 57 inline void setDrag(bool bDrag) 58 { this->bDrag_ = bDrag; } 57 void setDrag(bool bDrag); 59 58 inline bool getDrag() const 60 59 { return this->bDrag_; } … … 62 61 private: 63 62 void removeFocus(); 64 void setFocus( Ogre::Viewport* viewport);63 void setFocus(); 65 64 void configvaluecallback_changedNearClipDistance(); 66 65 67 Ogre::Camera* camera_; 68 float nearClipDistance_; 69 bool bHasFocus_; 70 bool bDrag_; 66 Ogre::Camera* camera_; 67 Ogre::SceneNode* cameraNode_; 68 float nearClipDistance_; 69 bool bHasFocus_; 70 bool bDrag_; 71 71 }; 72 72 } -
code/trunk/src/orxonox/objects/worldentities/CameraPosition.cc
r2087 r2662 38 38 CreateFactory(CameraPosition); 39 39 40 CameraPosition::CameraPosition(BaseObject* creator) : PositionableEntity(creator)40 CameraPosition::CameraPosition(BaseObject* creator) : StaticEntity(creator) 41 41 { 42 42 RegisterObject(CameraPosition); 43 43 44 44 this->bDrag_ = false; 45 this->bAllowMouseLook_ = false; 45 46 46 47 this->setObjectMode(0x0); … … 56 57 57 58 XMLPortParam(CameraPosition, "drag", setDrag, getDrag, xmlelement, mode).defaultValues(false); 59 XMLPortParam(CameraPosition, "mouselook", setAllowMouseLook, getAllowMouseLook, xmlelement, mode).defaultValues(false); 58 60 } 59 61 60 62 void CameraPosition::attachCamera(Camera* camera) 61 63 { 62 camera->setDrag(this->bDrag_); 64 if (!this->bDrag_) 65 camera->setDrag(false); 66 63 67 this->attach(camera); 68 69 if (this->bDrag_) 70 camera->setDrag(true); 64 71 } 65 72 } -
code/trunk/src/orxonox/objects/worldentities/CameraPosition.h
r2087 r2662 32 32 #include "OrxonoxPrereqs.h" 33 33 34 #include "objects/worldentities/ PositionableEntity.h"34 #include "objects/worldentities/StaticEntity.h" 35 35 36 36 namespace orxonox 37 37 { 38 class _OrxonoxExport CameraPosition : public PositionableEntity38 class _OrxonoxExport CameraPosition : public StaticEntity 39 39 { 40 40 public: … … 49 49 { return this->bDrag_; } 50 50 51 inline void setAllowMouseLook(bool bAllow) 52 { this->bAllowMouseLook_ = bAllow; } 53 inline bool getAllowMouseLook() const 54 { return this->bAllowMouseLook_; } 55 51 56 void attachCamera(Camera* camera); 52 57 53 58 private: 54 59 bool bDrag_; 60 bool bAllowMouseLook_; 55 61 }; 56 62 } -
code/trunk/src/orxonox/objects/worldentities/ControllableEntity.cc
r2171 r2662 23 23 * Fabian 'x3n' Landau 24 24 * Co-authors: 25 * ...25 * Reto Grieder 26 26 * 27 27 */ … … 30 30 #include "ControllableEntity.h" 31 31 32 #include <OgreSceneManager.h> 33 32 34 #include "core/CoreIncludes.h" 35 #include "core/ConfigValueIncludes.h" 33 36 #include "core/Core.h" 34 37 #include "core/XMLPort.h" 35 38 #include "core/Template.h" 36 39 40 #include "objects/Scene.h" 37 41 #include "objects/infos/PlayerInfo.h" 38 42 #include "objects/worldentities/Camera.h" 39 43 #include "objects/worldentities/CameraPosition.h" 44 #include "objects/gametypes/Gametype.h" 40 45 #include "overlays/OverlayGroup.h" 41 46 … … 44 49 CreateFactory(ControllableEntity); 45 50 46 ControllableEntity::ControllableEntity(BaseObject* creator) : WorldEntity(creator)51 ControllableEntity::ControllableEntity(BaseObject* creator) : MobileEntity(creator) 47 52 { 48 53 RegisterObject(ControllableEntity); 49 54 50 this->bControlled_ = false; 55 this->bHasLocalController_ = false; 56 this->bHasHumanController_ = false; 57 51 58 this->server_overwrite_ = 0; 52 59 this->client_overwrite_ = 0; … … 56 63 this->camera_ = 0; 57 64 this->bDestroyWhenPlayerLeft_ = false; 58 59 this->velocity_ = Vector3::ZERO; 60 this->acceleration_ = Vector3::ZERO; 61 62 this->server_position_ = Vector3::ZERO; 63 this->client_position_ = Vector3::ZERO; 64 this->server_velocity_ = Vector3::ZERO; 65 this->client_velocity_ = Vector3::ZERO; 66 this->server_orientation_ = Quaternion::IDENTITY; 67 this->client_orientation_ = Quaternion::IDENTITY; 68 65 this->cameraPositionRootNode_ = this->node_->createChildSceneNode(); 66 this->bMouseLook_ = false; 67 this->mouseLookSpeed_ = 200; 68 69 this->gtinfo_ = 0; 70 this->gtinfoID_ = OBJECTID_UNKNOWN; 71 this->changedGametype(); 72 73 this->server_position_ = Vector3::ZERO; 74 this->client_position_ = Vector3::ZERO; 75 this->server_linear_velocity_ = Vector3::ZERO; 76 this->client_linear_velocity_ = Vector3::ZERO; 77 this->server_orientation_ = Quaternion::IDENTITY; 78 this->client_orientation_ = Quaternion::IDENTITY; 79 this->server_angular_velocity_ = Vector3::ZERO; 80 this->client_angular_velocity_ = Vector3::ZERO; 81 82 83 this->setConfigValues(); 84 this->setPriority( priority::very_high ); 69 85 this->registerVariables(); 70 86 } … … 74 90 if (this->isInitialized()) 75 91 { 76 if (this->bControlled_) 77 this->stopLocalControl(); 92 this->bDestroyWhenPlayerLeft_ = false; 93 94 if (this->bHasLocalController_ && this->bHasHumanController_) 95 this->stopLocalHumanControl(); 96 97 if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this) 98 this->getPlayer()->stopControl(this, false); 78 99 79 100 if (this->hud_) … … 83 104 delete this->camera_; 84 105 85 if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this) 86 this->getPlayer()->stopControl(this, false); 106 for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it) 107 delete (*it); 108 109 if (this->getScene()->getSceneManager()) 110 this->getScene()->getSceneManager()->destroySceneNode(this->cameraPositionRootNode_->getName()); 87 111 } 88 112 } … … 98 122 } 99 123 124 void ControllableEntity::setConfigValues() 125 { 126 SetConfigValue(mouseLookSpeed_, 3.0f); 127 } 128 129 void ControllableEntity::changedGametype() 130 { 131 //SUPER(ControllableEntity, changedGametype); 132 WorldEntity::changedGametype(); 133 134 this->gtinfo_ = 0; 135 this->gtinfoID_ = OBJECTID_UNKNOWN; 136 137 if (this->getGametype() && this->getGametype()->getGametypeInfo()) 138 { 139 this->gtinfo_ = this->getGametype()->getGametypeInfo(); 140 this->gtinfoID_ = this->gtinfo_->getObjectID(); 141 } 142 } 143 100 144 void ControllableEntity::addCameraPosition(CameraPosition* position) 101 145 { 102 this->attach(position); 146 if (position->getAllowMouseLook()) 147 position->attachToNode(this->cameraPositionRootNode_); 148 else 149 this->attach(position); 103 150 this->cameraPositions_.push_back(position); 104 151 } … … 141 188 else 142 189 { 143 this-> attach(this->camera_);190 this->camera_->attachToNode(this->cameraPositionRootNode_); 144 191 } 145 192 } 193 } 194 195 void ControllableEntity::mouseLook() 196 { 197 this->bMouseLook_ = !this->bMouseLook_; 198 199 if (!this->bMouseLook_) 200 this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY); 201 } 202 203 void ControllableEntity::rotateYaw(const Vector2& value) 204 { 205 if (this->bMouseLook_) 206 this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); 207 } 208 209 void ControllableEntity::rotatePitch(const Vector2& value) 210 { 211 if (this->bMouseLook_) 212 this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); 213 } 214 215 void ControllableEntity::rotateRoll(const Vector2& value) 216 { 217 if (this->bMouseLook_) 218 this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL); 146 219 } 147 220 … … 156 229 this->player_ = player; 157 230 this->playerID_ = player->getObjectID(); 158 this->bControlled_ = (player->isLocalPlayer() && player->isHumanPlayer()); 159 if (this->bControlled_) 160 { 161 this->startLocalControl(); 231 this->bHasLocalController_ = player->isLocalPlayer(); 232 this->bHasHumanController_ = player->isHumanPlayer(); 233 234 if (this->bHasLocalController_ && this->bHasHumanController_) 235 { 236 this->startLocalHumanControl(); 162 237 163 238 if (!Core::isMaster()) 164 239 { 165 240 this->client_overwrite_ = this->server_overwrite_; 166 COUT(0) << "CE: bidirectional synchronization" << std::endl; 167 this->setObjectMode(direction::bidirectional); 241 this->setObjectMode(objectDirection::bidirectional); 168 242 } 169 243 } … … 172 246 void ControllableEntity::removePlayer() 173 247 { 174 if (this->b Controlled_)175 this->stopLocal Control();248 if (this->bHasLocalController_ && this->bHasHumanController_) 249 this->stopLocalHumanControl(); 176 250 177 251 this->player_ = 0; 178 252 this->playerID_ = OBJECTID_UNKNOWN; 179 this->bControlled_ = false; 180 this->setObjectMode(direction::toclient); 253 this->bHasLocalController_ = false; 254 this->bHasHumanController_ = false; 255 this->setObjectMode(objectDirection::toclient); 181 256 182 257 if (this->bDestroyWhenPlayerLeft_) … … 195 270 } 196 271 197 void ControllableEntity::startLocalControl() 198 { 199 // std::cout << this->getObjectID() << " ###### start local control" << std::endl; 200 this->camera_ = new Camera(this); 201 this->camera_->requestFocus(); 202 if (this->cameraPositionTemplate_ != "") 203 this->addTemplate(this->cameraPositionTemplate_); 204 if (this->cameraPositions_.size() > 0) 205 this->cameraPositions_.front()->attachCamera(this->camera_); 206 else 207 this->attach(this->camera_); 208 209 if (this->hudtemplate_ != "") 210 { 211 this->hud_ = new OverlayGroup(this); 212 this->hud_->addTemplate(this->hudtemplate_); 213 } 214 } 215 216 void ControllableEntity::stopLocalControl() 217 { 218 // std::cout << "###### stop local control" << std::endl; 219 this->camera_->detachFromParent(); 220 delete this->camera_; 221 this->camera_ = 0; 222 223 delete this->hud_; 224 this->hud_ = 0; 272 void ControllableEntity::networkcallback_changedgtinfoID() 273 { 274 if (this->gtinfoID_ != OBJECTID_UNKNOWN) 275 { 276 this->gtinfo_ = dynamic_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_)); 277 278 if (!this->gtinfo_) 279 this->gtinfoID_ = OBJECTID_UNKNOWN; 280 } 281 } 282 283 void ControllableEntity::startLocalHumanControl() 284 { 285 if (!this->camera_) 286 { 287 this->camera_ = new Camera(this); 288 this->camera_->requestFocus(); 289 if (this->cameraPositionTemplate_ != "") 290 this->addTemplate(this->cameraPositionTemplate_); 291 if (this->cameraPositions_.size() > 0) 292 this->cameraPositions_.front()->attachCamera(this->camera_); 293 else 294 this->camera_->attachToNode(this->cameraPositionRootNode_); 295 } 296 297 if (!this->hud_) 298 { 299 if (this->hudtemplate_ != "") 300 { 301 this->hud_ = new OverlayGroup(this); 302 this->hud_->addTemplate(this->hudtemplate_); 303 this->hud_->setOwner(this); 304 } 305 } 306 } 307 308 void ControllableEntity::stopLocalHumanControl() 309 { 310 if (this->camera_) 311 { 312 this->camera_->detachFromParent(); 313 delete this->camera_; 314 this->camera_ = 0; 315 } 316 317 if (this->hud_) 318 { 319 delete this->hud_; 320 this->hud_ = 0; 321 } 225 322 } 226 323 227 324 void ControllableEntity::tick(float dt) 228 325 { 326 MobileEntity::tick(dt); 327 229 328 if (this->isActive()) 230 329 { 231 this->velocity_ += (dt * this->acceleration_); 232 this->node_->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL); 233 234 if (Core::isMaster()) 330 // Check whether Bullet doesn't do the physics for us 331 if (!this->isDynamic()) 235 332 { 236 this->server_velocity_ = this->velocity_; 237 this->server_position_ = this->node_->getPosition(); 333 if (Core::isMaster()) 334 { 335 this->server_position_ = this->getPosition(); 336 this->server_orientation_ = this->getOrientation(); 337 this->server_linear_velocity_ = this->getVelocity(); 338 this->server_angular_velocity_ = this->getAngularVelocity(); 339 } 340 else if (this->bHasLocalController_) 341 { 342 this->client_position_ = this->getPosition(); 343 this->client_orientation_ = this->getOrientation(); 344 this->client_linear_velocity_ = this->getVelocity(); 345 this->client_angular_velocity_ = this->getAngularVelocity(); 346 } 238 347 } 239 else if (this->bControlled_)240 {241 // COUT(2) << "setting client position" << endl;242 this->client_velocity_ = this->velocity_;243 this->client_position_ = this->node_->getPosition();244 }245 348 } 246 349 } … … 248 351 void ControllableEntity::registerVariables() 249 352 { 250 REGISTERSTRING(this->cameraPositionTemplate_, direction::toclient); 251 252 REGISTERDATA(this->client_overwrite_, direction::toserver); 253 254 REGISTERDATA(this->server_position_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition)); 255 REGISTERDATA(this->server_velocity_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerVelocity)); 256 REGISTERDATA(this->server_orientation_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation)); 257 REGISTERDATA(this->server_overwrite_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite)); 258 259 REGISTERDATA(this->client_position_, direction::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition)); 260 REGISTERDATA(this->client_velocity_, direction::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientVelocity)); 261 REGISTERDATA(this->client_orientation_, direction::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation)); 262 263 264 REGISTERDATA(this->playerID_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID)); 353 registerVariable(this->cameraPositionTemplate_, variableDirection::toclient); 354 registerVariable(this->hudtemplate_, variableDirection::toclient); 355 356 registerVariable(this->server_position_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition)); 357 registerVariable(this->server_linear_velocity_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity)); 358 registerVariable(this->server_orientation_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation)); 359 registerVariable(this->server_angular_velocity_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity)); 360 361 registerVariable(this->server_overwrite_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite)); 362 registerVariable(this->client_overwrite_, variableDirection::toserver); 363 364 registerVariable(this->client_position_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition)); 365 registerVariable(this->client_linear_velocity_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity)); 366 registerVariable(this->client_orientation_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation)); 367 registerVariable(this->client_angular_velocity_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity)); 368 369 registerVariable(this->playerID_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID)); 370 registerVariable(this->gtinfoID_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedgtinfoID)); 265 371 } 266 372 267 373 void ControllableEntity::processServerPosition() 268 374 { 269 if (!this->b Controlled_)270 this->node_->setPosition(this->server_position_);271 } 272 273 void ControllableEntity::processServer Velocity()274 { 275 if (!this->b Controlled_)276 this->velocity_ = this->server_velocity_;375 if (!this->bHasLocalController_) 376 MobileEntity::setPosition(this->server_position_); 377 } 378 379 void ControllableEntity::processServerLinearVelocity() 380 { 381 if (!this->bHasLocalController_) 382 MobileEntity::setVelocity(this->server_linear_velocity_); 277 383 } 278 384 279 385 void ControllableEntity::processServerOrientation() 280 386 { 281 if (!this->bControlled_) 282 this->node_->setOrientation(this->server_orientation_); 387 if (!this->bHasLocalController_) 388 MobileEntity::setOrientation(this->server_orientation_); 389 } 390 391 void ControllableEntity::processServerAngularVelocity() 392 { 393 if (!this->bHasLocalController_) 394 MobileEntity::setAngularVelocity(this->server_angular_velocity_); 283 395 } 284 396 285 397 void ControllableEntity::processOverwrite() 286 398 { 287 if (this->b Controlled_)399 if (this->bHasLocalController_) 288 400 { 289 401 this->setPosition(this->server_position_); 290 this->setVelocity(this->server_velocity_);291 402 this->setOrientation(this->server_orientation_); 403 this->setVelocity(this->server_linear_velocity_); 404 this->setAngularVelocity(this->server_angular_velocity_); 292 405 293 406 this->client_overwrite_ = this->server_overwrite_; … … 299 412 if (this->server_overwrite_ == this->client_overwrite_) 300 413 { 301 // COUT(2) << "callback: setting client position" << endl; 302 this->node_->setPosition(this->client_position_); 303 this->server_position_ = this->client_position_; 304 } 305 // else 306 // COUT(2) << "callback: not setting client position" << endl; 307 } 308 309 void ControllableEntity::processClientVelocity() 414 MobileEntity::setPosition(this->client_position_); 415 this->server_position_ = this->getPosition(); 416 } 417 } 418 419 void ControllableEntity::processClientLinearVelocity() 310 420 { 311 421 if (this->server_overwrite_ == this->client_overwrite_) 312 422 { 313 this->velocity_ = this->client_velocity_;314 this->server_ velocity_ = this->client_velocity_;423 MobileEntity::setVelocity(this->client_linear_velocity_); 424 this->server_linear_velocity_ = this->getVelocity(); 315 425 } 316 426 } … … 320 430 if (this->server_overwrite_ == this->client_overwrite_) 321 431 { 322 this->node_->setOrientation(this->client_orientation_); 323 this->server_orientation_ = this->client_orientation_; 324 } 325 } 326 432 MobileEntity::setOrientation(this->client_orientation_); 433 this->server_orientation_ = this->getOrientation(); 434 } 435 } 436 437 void ControllableEntity::processClientAngularVelocity() 438 { 439 if (this->server_overwrite_ == this->client_overwrite_) 440 { 441 MobileEntity::setAngularVelocity(this->client_angular_velocity_); 442 this->server_angular_velocity_ = this->getAngularVelocity(); 443 } 444 } 327 445 328 446 void ControllableEntity::setPosition(const Vector3& position) … … 330 448 if (Core::isMaster()) 331 449 { 332 this->node_->setPosition(position);333 this->server_position_ = position;450 MobileEntity::setPosition(position); 451 this->server_position_ = this->getPosition(); 334 452 ++this->server_overwrite_; 335 453 } 336 else if (this->bControlled_) 337 { 338 this->node_->setPosition(position); 339 this->client_position_ = position; 454 else if (this->bHasLocalController_) 455 { 456 MobileEntity::setPosition(position); 457 this->client_position_ = this->getPosition(); 458 } 459 } 460 461 void ControllableEntity::setOrientation(const Quaternion& orientation) 462 { 463 if (Core::isMaster()) 464 { 465 MobileEntity::setOrientation(orientation); 466 this->server_orientation_ = this->getOrientation(); 467 ++this->server_overwrite_; 468 } 469 else if (this->bHasLocalController_) 470 { 471 MobileEntity::setOrientation(orientation); 472 this->client_orientation_ = this->getOrientation(); 340 473 } 341 474 } … … 345 478 if (Core::isMaster()) 346 479 { 347 this->velocity_ = velocity;348 this->server_ velocity_ = velocity;480 MobileEntity::setVelocity(velocity); 481 this->server_linear_velocity_ = this->getVelocity(); 349 482 ++this->server_overwrite_; 350 483 } 351 else if (this->b Controlled_)352 { 353 this->velocity_ = velocity;354 this->client_ velocity_ = velocity;355 } 356 } 357 358 void ControllableEntity:: translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)484 else if (this->bHasLocalController_) 485 { 486 MobileEntity::setVelocity(velocity); 487 this->client_linear_velocity_ = this->getVelocity(); 488 } 489 } 490 491 void ControllableEntity::setAngularVelocity(const Vector3& velocity) 359 492 { 360 493 if (Core::isMaster()) 361 494 { 362 this->node_->translate(distance, relativeTo);363 this->server_ position_ = this->node_->getPosition();495 MobileEntity::setAngularVelocity(velocity); 496 this->server_angular_velocity_ = this->getAngularVelocity(); 364 497 ++this->server_overwrite_; 365 498 } 366 else if (this->bControlled_) 367 { 368 this->node_->translate(distance, relativeTo); 369 this->client_position_ = this->node_->getPosition(); 370 } 371 } 372 373 void ControllableEntity::setOrientation(const Quaternion& orientation) 374 { 499 else if (this->bHasLocalController_) 500 { 501 MobileEntity::setAngularVelocity(velocity); 502 this->client_angular_velocity_ = this->getAngularVelocity(); 503 } 504 } 505 506 void ControllableEntity::setWorldTransform(const btTransform& worldTrans) 507 { 508 MobileEntity::setWorldTransform(worldTrans); 375 509 if (Core::isMaster()) 376 510 { 377 this->node_->setOrientation(orientation); 378 this->server_orientation_ = orientation; 379 ++this->server_overwrite_; 380 } 381 else if (this->bControlled_) 382 { 383 this->node_->setOrientation(orientation); 384 this->client_orientation_ = orientation; 385 } 386 } 387 388 void ControllableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo) 389 { 390 if (Core::isMaster()) 391 { 392 this->node_->rotate(rotation, relativeTo); 393 this->server_orientation_ = this->node_->getOrientation(); 394 ++this->server_overwrite_; 395 } 396 else if (this->bControlled_) 397 { 398 this->node_->rotate(rotation, relativeTo); 399 this->client_orientation_ = this->node_->getOrientation(); 400 } 401 } 402 403 void ControllableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo) 404 { 405 if (Core::isMaster()) 406 { 407 this->node_->yaw(angle, relativeTo); 408 this->server_orientation_ = this->node_->getOrientation(); 409 ++this->server_overwrite_; 410 } 411 else if (this->bControlled_) 412 { 413 this->node_->yaw(angle, relativeTo); 414 this->client_orientation_ = this->node_->getOrientation(); 415 } 416 } 417 418 void ControllableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo) 419 { 420 if (Core::isMaster()) 421 { 422 this->node_->pitch(angle, relativeTo); 423 this->server_orientation_ = this->node_->getOrientation(); 424 ++this->server_overwrite_; 425 } 426 else if (this->bControlled_) 427 { 428 this->node_->pitch(angle, relativeTo); 429 this->client_orientation_ = this->node_->getOrientation(); 430 } 431 } 432 433 void ControllableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo) 434 { 435 if (Core::isMaster()) 436 { 437 this->node_->roll(angle, relativeTo); 438 this->server_orientation_ = this->node_->getOrientation(); 439 ++this->server_overwrite_; 440 } 441 else if (this->bControlled_) 442 { 443 this->node_->roll(angle, relativeTo); 444 this->client_orientation_ = this->node_->getOrientation(); 445 } 446 } 447 448 void ControllableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) 449 { 450 if (Core::isMaster()) 451 { 452 this->node_->lookAt(target, relativeTo, localDirectionVector); 453 this->server_orientation_ = this->node_->getOrientation(); 454 ++this->server_overwrite_; 455 } 456 else if (this->bControlled_) 457 { 458 this->node_->lookAt(target, relativeTo, localDirectionVector); 459 this->client_orientation_ = this->node_->getOrientation(); 460 } 461 } 462 463 void ControllableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) 464 { 465 if (Core::isMaster()) 466 { 467 this->node_->setDirection(direction, relativeTo, localDirectionVector); 468 this->server_orientation_ = this->node_->getOrientation(); 469 ++this->server_overwrite_; 470 } 471 else if (this->bControlled_) 472 { 473 this->node_->setDirection(direction, relativeTo, localDirectionVector); 474 this->client_orientation_ = this->node_->getOrientation(); 511 this->server_position_ = this->getPosition(); 512 this->server_orientation_ = this->getOrientation(); 513 this->server_linear_velocity_ = this->getVelocity(); 514 this->server_angular_velocity_ = this->getAngularVelocity(); 515 } 516 else if (this->bHasLocalController_) 517 { 518 this->client_position_ = this->getPosition(); 519 this->client_orientation_ = this->getOrientation(); 520 this->client_linear_velocity_ = this->getVelocity(); 521 this->client_angular_velocity_ = this->getAngularVelocity(); 475 522 } 476 523 } -
code/trunk/src/orxonox/objects/worldentities/ControllableEntity.h
r2087 r2662 23 23 * Fabian 'x3n' Landau 24 24 * Co-authors: 25 * ...25 * Reto Grieder 26 26 * 27 27 */ … … 32 32 #include "OrxonoxPrereqs.h" 33 33 34 #include " WorldEntity.h"35 #include "objects/ Tickable.h"34 #include "MobileEntity.h" 35 #include "objects/weaponSystem/WeaponSystem.h" 36 36 37 37 namespace orxonox 38 38 { 39 class _OrxonoxExport ControllableEntity : public WorldEntity, public Tickable39 class _OrxonoxExport ControllableEntity : public MobileEntity 40 40 { 41 41 public: … … 46 46 virtual void tick(float dt); 47 47 void registerVariables(); 48 void setConfigValues(); 49 50 virtual void changedGametype(); 48 51 49 52 virtual void setPlayer(PlayerInfo* player); … … 61 64 virtual void moveUpDown(const Vector2& value) {} 62 65 63 virtual void rotateYaw(const Vector2& value) {}64 virtual void rotatePitch(const Vector2& value) {}65 virtual void rotateRoll(const Vector2& value) {}66 virtual void rotateYaw(const Vector2& value); 67 virtual void rotatePitch(const Vector2& value); 68 virtual void rotateRoll(const Vector2& value); 66 69 67 virtual void fire() {} 68 virtual void altFire() {} 70 inline void moveFrontBack(float value) 71 { this->moveFrontBack(Vector2(value, 0)); } 72 inline void moveRightLeft(float value) 73 { this->moveRightLeft(Vector2(value, 0)); } 74 inline void moveUpDown(float value) 75 { this->moveUpDown(Vector2(value, 0)); } 69 76 77 inline void rotateYaw(float value) 78 { this->rotateYaw(Vector2(value, 0)); } 79 inline void rotatePitch(float value) 80 { this->rotatePitch(Vector2(value, 0)); } 81 inline void rotateRoll(float value) 82 { this->rotateRoll(Vector2(value, 0)); } 83 84 virtual void fire(WeaponMode::Enum fireMode) {} 85 virtual void altFire(WeaponMode::Enum fireMode) {} 86 87 virtual void boost() {} 70 88 virtual void greet() {} 71 89 virtual void use() {} 90 virtual void dropItems() {} 72 91 virtual void switchCamera(); 92 virtual void mouseLook(); 73 93 74 inline const Vector3& getVelocity() const75 { return this->velocity_; }76 inline const Vector3& getAcceleration() const77 { return this->acceleration_; }78 94 inline const std::string& getHudTemplate() const 79 95 { return this->hudtemplate_; } 80 81 using WorldEntity::setPosition;82 using WorldEntity::translate;83 using WorldEntity::setOrientation;84 using WorldEntity::rotate;85 using WorldEntity::yaw;86 using WorldEntity::pitch;87 using WorldEntity::roll;88 using WorldEntity::lookAt;89 using WorldEntity::setDirection;90 91 void setPosition(const Vector3& position);92 void translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);93 void setOrientation(const Quaternion& orientation);94 void rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);95 void yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);96 void pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);97 void roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL);98 void lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);99 void setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z);100 101 void setVelocity(const Vector3& velocity);102 inline void setVelocity(float x, float y, float z)103 { this->velocity_.x = x; this->velocity_.y = y; this->velocity_.z = z; }104 105 inline void setAcceleration(const Vector3& acceleration)106 { this->acceleration_ = acceleration; }107 inline void setAcceleration(float x, float y, float z)108 { this->acceleration_.x = x; this->acceleration_.y = y; this->acceleration_.z = z; }109 96 110 97 inline Camera* getCamera() const … … 123 110 { return this->cameraPositionTemplate_; } 124 111 112 using WorldEntity::setPosition; 113 using WorldEntity::setOrientation; 114 using MobileEntity::setVelocity; 115 using MobileEntity::setAngularVelocity; 116 117 void setPosition(const Vector3& position); 118 void setOrientation(const Quaternion& orientation); 119 void setVelocity(const Vector3& velocity); 120 void setAngularVelocity(const Vector3& velocity); 121 122 inline bool hasLocalController() const 123 { return this->bHasLocalController_; } 124 inline bool hasHumanController() const 125 { return this->bHasHumanController_; } 126 127 inline const GametypeInfo* getGametypeInfo() const 128 { return this->gtinfo_; } 129 130 inline bool isInMouseLook() const 131 { return this->bMouseLook_; } 132 inline float getMouseLookSpeed() const 133 { return this->mouseLookSpeed_; } 134 125 135 protected: 126 virtual void startLocal Control();127 virtual void stopLocal Control();136 virtual void startLocalHumanControl(); 137 virtual void stopLocalHumanControl(); 128 138 129 139 inline void setHudTemplate(const std::string& name) 130 140 { this->hudtemplate_ = name; } 131 132 inline bool isLocallyControlled() const133 { return this->bControlled_; }134 135 Vector3 acceleration_;136 141 137 142 private: … … 140 145 141 146 void processServerPosition(); 142 void processServer Velocity();147 void processServerLinearVelocity(); 143 148 void processServerOrientation(); 149 void processServerAngularVelocity(); 144 150 145 151 void processClientPosition(); 146 void processClient Velocity();152 void processClientLinearVelocity(); 147 153 void processClientOrientation(); 154 void processClientAngularVelocity(); 148 155 149 156 void networkcallback_changedplayerID(); 157 void networkcallback_changedgtinfoID(); 158 159 // Bullet btMotionState related 160 void setWorldTransform(const btTransform& worldTrans); 150 161 151 162 unsigned int server_overwrite_; 152 163 unsigned int client_overwrite_; 153 164 154 Vector3 velocity_; 165 bool bHasLocalController_; 166 bool bHasHumanController_; 167 bool bDestroyWhenPlayerLeft_; 155 168 156 bool bControlled_;157 169 Vector3 server_position_; 158 170 Vector3 client_position_; 159 Vector3 server_ velocity_;160 Vector3 client_ velocity_;171 Vector3 server_linear_velocity_; 172 Vector3 client_linear_velocity_; 161 173 Quaternion server_orientation_; 162 174 Quaternion client_orientation_; 175 Vector3 server_angular_velocity_; 176 Vector3 client_angular_velocity_; 163 177 164 178 PlayerInfo* player_; 165 179 unsigned int playerID_; 180 166 181 std::string hudtemplate_; 167 182 OverlayGroup* hud_; 183 168 184 Camera* camera_; 169 bool bDestroyWhenPlayerLeft_; 170 185 bool bMouseLook_; 186 float mouseLookSpeed_; 187 Ogre::SceneNode* cameraPositionRootNode_; 171 188 std::list<CameraPosition*> cameraPositions_; 172 189 std::string cameraPositionTemplate_; 190 191 const GametypeInfo* gtinfo_; 192 unsigned int gtinfoID_; 173 193 }; 174 194 } -
code/trunk/src/orxonox/objects/worldentities/Light.cc
r2171 r2662 36 36 37 37 #include "util/String.h" 38 #include "util/Convert.h" 38 #include "util/Exception.h" 39 #include "core/Core.h" 39 40 #include "core/CoreIncludes.h" 40 41 #include "core/XMLPort.h" … … 43 44 namespace orxonox 44 45 { 45 unsigned int Light::lightCounter_s = 0;46 47 46 CreateFactory(Light); 48 47 49 Light::Light(BaseObject* creator) : PositionableEntity(creator)48 Light::Light(BaseObject* creator) : StaticEntity(creator) 50 49 { 51 50 RegisterObject(Light); 52 51 53 if (this->getScene() && this->getScene()->getSceneManager()) 54 this->light_ = this->getScene()->getSceneManager()->createLight("Light" + convertToString(Light::lightCounter_s++)); 55 this->getNode()->attachObject(this->light_); 52 this->light_ = 0; 53 this->diffuse_ = ColourValue::White; 54 this->specular_ = ColourValue::White; 55 this->type_ = Ogre::Light::LT_POINT; 56 this->attenuation_ = Vector4(100000, 1, 0, 0); 57 this->spotlightRange_ = Vector3(40.0f, 30.0f, 1.0f); 58 59 if (Core::showsGraphics()) 60 { 61 if (!this->getScene()) 62 ThrowException(AbortLoading, "Can't create Light, no scene given."); 63 if (!this->getScene()->getSceneManager()) 64 ThrowException(AbortLoading, "Can't create Light, no scene manager given."); 65 66 if (this->getScene() && this->getScene()->getSceneManager()) 67 { 68 this->light_ = this->getScene()->getSceneManager()->createLight("Light" + getUniqueNumberString()); 69 this->light_->setDirection(WorldEntity::FRONT); 70 this->attachOgreObject(this->light_); 71 72 this->updateType(); 73 this->updateDiffuseColour(); 74 this->updateSpecularColour(); 75 this->updateAttenuation(); 76 this->updateSpotlightRange(); 77 } 78 } 56 79 57 80 this->registerVariables(); … … 71 94 SUPER(Light, XMLPort, xmlelement, mode); 72 95 73 XMLPortParam(Light, "type", setTypeString, getTypeString, xmlelement, mode).defaultValues("point"); 74 XMLPortParamExternTemplate(Light, Ogre::Light, this->light_, "diffuse", setDiffuseColour, getDiffuseColour, xmlelement, mode, const ColourValue&); 75 XMLPortParamExternTemplate(Light, Ogre::Light, this->light_, "specular", setSpecularColour, getSpecularColour, xmlelement, mode, const ColourValue&); 76 XMLPortParamExternTemplate(Light, Ogre::Light, this->light_, "direction", setDirection, getDirection, xmlelement, mode, const Vector3&); 96 XMLPortParam(Light, "type", setTypeString, getTypeString, xmlelement, mode).defaultValues("point"); 97 XMLPortParam(Light, "diffuse", setDiffuseColour, getDiffuseColour, xmlelement, mode).defaultValues(ColourValue::White); 98 XMLPortParam(Light, "specular", setSpecularColour, getSpecularColour, xmlelement, mode).defaultValues(ColourValue::White); 99 XMLPortParam(Light, "attenuation", setAttenuation, getAttenuation, xmlelement, mode).defaultValues(Vector4(100000, 1, 0, 0)); 100 XMLPortParam(Light, "spotlightrange", setSpotlightRange, getSpotlightRange, xmlelement, mode).defaultValues(Vector3(40.0f, 30.0f, 1.0f)); 77 101 } 78 102 79 103 void Light::registerVariables() 80 104 { 81 REGISTERDATA(this->type_, direction::toclient, new NetworkCallback<Light>(this, &Light::changedType)); 82 REGISTERDATA(this->light_->getDiffuseColour(), direction::toclient); 83 REGISTERDATA(this->light_->getSpecularColour(), direction::toclient); 84 REGISTERDATA(this->light_->getDirection(), direction::toclient); 105 registerVariable((int&)this->type_, variableDirection::toclient, new NetworkCallback<Light>(this, &Light::updateType)); 106 registerVariable(this->diffuse_, variableDirection::toclient, new NetworkCallback<Light>(this, &Light::updateDiffuseColour)); 107 registerVariable(this->specular_, variableDirection::toclient, new NetworkCallback<Light>(this, &Light::updateSpecularColour)); 108 registerVariable(this->attenuation_, variableDirection::toclient, new NetworkCallback<Light>(this, &Light::updateAttenuation)); 109 registerVariable(this->spotlightRange_, variableDirection::toclient, new NetworkCallback<Light>(this, &Light::updateSpotlightRange)); 85 110 } 86 111 87 const std::string& Light::getName() const112 void Light::updateDiffuseColour() 88 113 { 89 114 if (this->light_) 90 return this->light_->getName(); 91 else 92 return BLANKSTRING; 115 this->light_->setDiffuseColour(this->diffuse_); 93 116 } 94 117 95 void Light:: setDiffuseColour(const ColourValue& colour)118 void Light::updateSpecularColour() 96 119 { 97 120 if (this->light_) 98 this->light_->set DiffuseColour(colour);121 this->light_->setSpecularColour(this->specular_); 99 122 } 100 123 101 const ColourValue& Light::getDiffuseColour() const124 void Light::updateAttenuation() 102 125 { 103 if (this->light_) 104 return this->light_->getDiffuseColour(); 105 else 106 return ColourValue::White; 126 if (this->light_ && this->type_ != Ogre::Light::LT_DIRECTIONAL) 127 this->light_->setAttenuation(this->attenuation_.x, this->attenuation_.y, this->attenuation_.z, this->attenuation_.w); 107 128 } 108 129 109 void Light:: setSpecularColour(const ColourValue& colour)130 void Light::updateSpotlightRange() 110 131 { 111 if (this->light_) 112 this->light_->setSpecularColour(colour); 113 } 114 115 const ColourValue& Light::getSpecularColour() const 116 { 117 if (this->light_) 118 return this->light_->getSpecularColour(); 119 else 120 return ColourValue::White; 121 } 122 123 void Light::setDirection(const Vector3& direction) 124 { 125 if (this->light_) 126 this->light_->setDirection(direction); 127 } 128 129 const Vector3& Light::getDirection() const 130 { 131 if (this->light_) 132 return this->light_->getDirection(); 133 else 134 return Vector3::ZERO; 132 if (this->light_ && this->type_ == Ogre::Light::LT_SPOTLIGHT) 133 this->light_->setSpotlightRange(Degree(this->spotlightRange_.x), Degree(this->spotlightRange_.y), this->spotlightRange_.z); 135 134 } 136 135 … … 157 156 case Ogre::Light::LT_POINT: 158 157 default: 159 return "poin T";158 return "point"; 160 159 } 161 160 } 162 161 163 void Light:: changedType()162 void Light::updateType() 164 163 { 165 this->light_->setType(this->type_); 164 if (this->light_) 165 { 166 this->light_->setType(this->type_); 167 168 if (this->type_ != Ogre::Light::LT_DIRECTIONAL) 169 this->updateAttenuation(); 170 if (this->type_ == Ogre::Light::LT_SPOTLIGHT) 171 this->updateSpotlightRange(); 172 } 166 173 } 167 174 … … 170 177 SUPER(Light, changedVisibility); 171 178 172 this->light_->setVisible(this->isVisible()); 179 if (this->light_) 180 this->light_->setVisible(this->isVisible()); 173 181 } 174 182 } -
code/trunk/src/orxonox/objects/worldentities/Light.h
r2087 r2662 31 31 32 32 #include "OrxonoxPrereqs.h" 33 #include " PositionableEntity.h"33 #include "StaticEntity.h" 34 34 35 35 #include <string> … … 40 40 namespace orxonox 41 41 { 42 class _OrxonoxExport Light : public PositionableEntity42 class _OrxonoxExport Light : public StaticEntity 43 43 { 44 44 public: … … 54 54 { return this->light_; } 55 55 56 const std::string& getName() const;57 58 56 inline void setType(Ogre::Light::LightTypes type) 59 { this->type_ = type; this-> changedType(); }60 Ogre::Light::LightTypes getType() const57 { this->type_ = type; this->updateType(); } 58 inline Ogre::Light::LightTypes getType() const 61 59 { return this->type_; } 62 60 63 void setDiffuseColour(const ColourValue& colour); 64 const ColourValue& getDiffuseColour() const; 61 inline void setDiffuseColour(const ColourValue& colour) 62 { this->diffuse_ = colour; this->updateDiffuseColour(); } 63 inline const ColourValue& getDiffuseColour() const 64 { return this->diffuse_; } 65 65 66 void setSpecularColour(const ColourValue& colour); 67 const ColourValue& getSpecularColour() const; 66 inline void setSpecularColour(const ColourValue& colour) 67 { this->specular_ = colour; this->updateSpecularColour(); } 68 inline const ColourValue& getSpecularColour() const 69 { return this->specular_; } 68 70 69 void setDirection(const Vector3& direction); 70 const Vector3& getDirection() const; 71 /** 72 @brief Sets the attenuation parameters of the light source i.e. how it diminishes with distance. 73 74 @param attenuation.x range (The absolute upper range of the light in world units) 75 @param attenuation.y constant (The constant factor in the attenuation formula: 1.0 means never attenuate, 0.0 is complete attenuation) 76 @param attenuation.z linear (The linear factor in the attenuation formula: 1 means attenuate evenly over the distance) 77 @param attenuation.w quadratic (The quadratic factor in the attenuation formula: adds a curvature to the attenuation formula) 78 79 Quote from the Ogre API: 80 Lights normally get fainter the further they are away. Also, each light is given a maximum range beyond which it cannot affect any objects. 81 Light attenuation is not applicable to directional lights since they have an infinite range and constant intensity. 82 This follows a standard attenuation approach - see any good 3D text for the details of what they mean since i don't have room here! 83 84 Quote from the Ogre wiki: 85 "Using these numbers, the light has 100% intensity at 0 distance, and 86 trails off to near black at a distance equal to the Range. Keep in mind 87 that most of the light falls in the first 20% of the range." 88 89 Range Constant Linear Quadratic 90 3250, 1.0, 0.0014, 0.000007 91 600, 1.0, 0.007, 0.0002 92 325, 1.0, 0.014, 0.0007 93 200, 1.0, 0.022, 0.0019 94 160, 1.0, 0.027, 0.0028 95 100, 1.0, 0.045, 0.0075 96 65, 1.0, 0.07, 0.017 97 50, 1.0, 0.09, 0.032 98 32, 1.0, 0.14, 0.07 99 20, 1.0, 0.22, 0.20 100 13, 1.0, 0.35, 0.44 101 7, 1.0, 0.7, 1.8 102 */ 103 inline void setAttenuation(const Vector4& attenuation) 104 { this->attenuation_ = attenuation; this->updateAttenuation(); } 105 inline const Vector4& getAttenuation() const 106 { return this->attenuation_; } 107 108 /** 109 @brief Sets the range of a spotlight, i.e. the angle of the inner and outer cones and the rate of falloff between them. 110 111 @param spotlightRange.x innerAngle (The angle covered by the bright inner cone) 112 @param spotlightRange.x outerAngle (The angle covered by the outer cone) 113 @param spotlightRange.x falloff (The rate of falloff between the inner and outer cones. 1.0 means a linear falloff, less means slower falloff, higher means faster falloff.) 114 */ 115 inline void setSpotlightRange(const Vector3& spotlightRange) 116 { this->spotlightRange_ = spotlightRange; this->updateSpotlightRange(); } 117 inline const Vector3& getSpotlightRange() const 118 { return this->spotlightRange_; } 71 119 72 120 private: … … 74 122 std::string getTypeString() const; 75 123 76 void changedType(); 124 void updateType(); 125 void updateDiffuseColour(); 126 void updateSpecularColour(); 127 void updateAttenuation(); 128 void updateSpotlightRange(); 77 129 78 static unsigned int lightCounter_s;79 130 Ogre::Light* light_; 80 131 Ogre::Light::LightTypes type_; 81 132 ColourValue diffuse_; 133 ColourValue specular_; 134 Vector4 attenuation_; 135 Vector3 spotlightRange_; 82 136 }; 83 137 } -
code/trunk/src/orxonox/objects/worldentities/MobileEntity.cc
- Property svn:mergeinfo changed (with no actual effect on merging)
-
code/trunk/src/orxonox/objects/worldentities/MobileEntity.h
- Property svn:mergeinfo changed (with no actual effect on merging)
-
code/trunk/src/orxonox/objects/worldentities/Model.cc
r2171 r2662 29 29 #include "OrxonoxStableHeaders.h" 30 30 31 #include <OgreEntity.h> 31 32 #include "Model.h" 32 33 #include "core/CoreIncludes.h" … … 38 39 CreateFactory(Model); 39 40 40 Model::Model(BaseObject* creator) : PositionableEntity(creator)41 Model::Model(BaseObject* creator) : StaticEntity(creator) 41 42 { 42 43 RegisterObject(Model); 44 45 this->bCastShadows_ = true; 43 46 44 47 this->registerVariables(); … … 48 51 { 49 52 if (this->isInitialized() && this->mesh_.getEntity()) 50 this-> getNode()->detachObject(this->mesh_.getEntity());53 this->detachOgreObject(this->mesh_.getEntity()); 51 54 } 52 55 … … 61 64 void Model::registerVariables() 62 65 { 63 REGISTERSTRING(this->meshSrc_, direction::toclient, new NetworkCallback<Model>(this, &Model::changedMesh));64 REGISTERDATA(this->bCastShadows_, direction::toclient, new NetworkCallback<Model>(this, &Model::changedShadows));66 registerVariable(this->meshSrc_, variableDirection::toclient, new NetworkCallback<Model>(this, &Model::changedMesh)); 67 registerVariable(this->bCastShadows_, variableDirection::toclient, new NetworkCallback<Model>(this, &Model::changedShadows)); 65 68 } 66 69 67 70 void Model::changedMesh() 68 71 { 69 if (this->mesh_.getEntity()) 70 this->getNode()->detachObject(this->mesh_.getEntity()); 72 if (Core::showsGraphics()) 73 { 74 if (this->mesh_.getEntity()) 75 this->detachOgreObject(this->mesh_.getEntity()); 71 76 72 this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);77 this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_); 73 78 74 if (this->mesh_.getEntity()) 75 { 76 this->getNode()->attachObject(this->mesh_.getEntity()); 77 this->mesh_.getEntity()->setCastShadows(this->bCastShadows_); 78 this->mesh_.setVisible(this->isVisible()); 79 if (this->mesh_.getEntity()) 80 { 81 this->attachOgreObject(this->mesh_.getEntity()); 82 this->mesh_.getEntity()->setCastShadows(this->bCastShadows_); 83 this->mesh_.setVisible(this->isVisible()); 84 } 79 85 } 80 86 } -
code/trunk/src/orxonox/objects/worldentities/Model.h
r2087 r2662 31 31 32 32 #include "OrxonoxPrereqs.h" 33 #include " PositionableEntity.h"33 #include "StaticEntity.h" 34 34 #include "tools/Mesh.h" 35 35 36 36 namespace orxonox 37 37 { 38 class _OrxonoxExport Model : public PositionableEntity38 class _OrxonoxExport Model : public StaticEntity 39 39 { 40 40 public: … … 70 70 } 71 71 72 #endif /* _ PositionableEntity_H__ */72 #endif /* _Model_H__ */ -
code/trunk/src/orxonox/objects/worldentities/MovableEntity.cc
r2171 r2662 22 22 * Author: 23 23 * Fabian 'x3n' Landau 24 * Reto Grieder 24 25 * Co-authors: 25 26 * ... … … 33 34 #include "core/XMLPort.h" 34 35 #include "core/Executor.h" 35 #include " tools/Timer.h"36 #include "core/Core.h" 36 37 37 38 namespace orxonox 38 39 { 39 40 static const float MAX_RESYNCHRONIZE_TIME = 3.0f; 41 static const float CONTINUOUS_SYNCHRONIZATION_TIME = 10.0f; 40 42 41 43 CreateFactory(MovableEntity); 42 44 43 MovableEntity::MovableEntity(BaseObject* creator) : WorldEntity(creator)45 MovableEntity::MovableEntity(BaseObject* creator) : MobileEntity(creator) 44 46 { 45 47 RegisterObject(MovableEntity); 46 48 47 this->velocity_ = Vector3::ZERO; 48 this->acceleration_ = Vector3::ZERO; 49 this->rotationAxis_ = Vector3::ZERO; 50 this->rotationRate_ = 0; 51 this->momentum_ = 0; 49 this->overwrite_position_ = Vector3::ZERO; 50 this->overwrite_orientation_ = Quaternion::IDENTITY; 52 51 53 this->overwrite_position_ = Vector3::ZERO; 54 this->overwrite_orientation_ = Quaternion::IDENTITY; 52 this->continuousResynchroTimer_ = 0; 53 54 this->setPriority(priority::low); 55 55 56 56 this->registerVariables(); … … 59 59 MovableEntity::~MovableEntity() 60 60 { 61 if (this->isInitialized()) 62 if (this->continuousResynchroTimer_) 63 delete this->continuousResynchroTimer_; 61 64 } 62 65 … … 64 67 { 65 68 SUPER(MovableEntity, XMLPort, xmlelement, mode); 66 67 XMLPortParamTemplate(MovableEntity, "velocity", setVelocity, getVelocity, xmlelement, mode, const Vector3&);68 XMLPortParamTemplate(MovableEntity, "rotationaxis", setRotationAxis, getRotationAxis, xmlelement, mode, const Vector3&);69 XMLPortParamTemplate(MovableEntity, "rotationrate", setRotationRate, getRotationRate, xmlelement, mode, const Degree&);70 }71 72 void MovableEntity::tick(float dt)73 {74 if (this->isActive())75 {76 this->velocity_ += (dt * this->acceleration_);77 this->node_->translate(dt * this->velocity_);78 79 this->rotationRate_ += (dt * this->momentum_);80 this->node_->rotate(this->rotationAxis_, this->rotationRate_ * dt);81 }82 69 } 83 70 84 71 void MovableEntity::registerVariables() 85 72 { 86 REGISTERDATA(this->velocity_.x, direction::toclient); 87 REGISTERDATA(this->velocity_.y, direction::toclient); 88 REGISTERDATA(this->velocity_.z, direction::toclient); 73 registerVariable(this->linearVelocity_, variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::processLinearVelocity)); 74 registerVariable(this->angularVelocity_, variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::processAngularVelocity)); 89 75 90 REGISTERDATA(this->rotationAxis_.x, direction::toclient); 91 REGISTERDATA(this->rotationAxis_.y, direction::toclient); 92 REGISTERDATA(this->rotationAxis_.z, direction::toclient); 93 94 REGISTERDATA(this->rotationRate_, direction::toclient); 95 96 REGISTERDATA(this->overwrite_position_, direction::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwritePosition)); 97 REGISTERDATA(this->overwrite_orientation_, direction::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwriteOrientation)); 98 } 99 100 void MovableEntity::overwritePosition() 101 { 102 this->node_->setPosition(this->overwrite_position_); 103 } 104 105 void MovableEntity::overwriteOrientation() 106 { 107 this->node_->setOrientation(this->overwrite_orientation_); 76 registerVariable(this->overwrite_position_, variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwritePosition)); 77 registerVariable(this->overwrite_orientation_, variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwriteOrientation)); 108 78 } 109 79 110 80 void MovableEntity::clientConnected(unsigned int clientID) 111 81 { 112 new Timer<MovableEntity>(rnd() * MAX_RESYNCHRONIZE_TIME, false, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), true);82 this->resynchronizeTimer_.setTimer(rnd() * MAX_RESYNCHRONIZE_TIME, false, this, createExecutor(createFunctor(&MovableEntity::resynchronize))); 113 83 } 114 84 … … 119 89 void MovableEntity::resynchronize() 120 90 { 91 if (Core::isMaster() && !this->continuousResynchroTimer_) 92 { 93 // Resynchronise every few seconds because we only work with velocities (no positions) 94 continuousResynchroTimer_ = new Timer<MovableEntity>(CONTINUOUS_SYNCHRONIZATION_TIME + rnd(-1, 1), 95 true, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), false); 96 } 97 121 98 this->overwrite_position_ = this->getPosition(); 122 99 this->overwrite_orientation_ = this->getOrientation(); 123 100 } 124 125 void MovableEntity::setPosition(const Vector3& position)126 {127 this->node_->setPosition(position);128 this->overwrite_position_ = this->node_->getPosition();129 }130 131 void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)132 {133 this->node_->translate(distance, relativeTo);134 this->overwrite_position_ = this->node_->getPosition();135 }136 137 void MovableEntity::setOrientation(const Quaternion& orientation)138 {139 this->node_->setOrientation(orientation);140 this->overwrite_orientation_ = this->node_->getOrientation();141 }142 143 void MovableEntity::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 MovableEntity::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 MovableEntity::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 MovableEntity::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 MovableEntity::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 MovableEntity::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();177 }178 101 } -
code/trunk/src/orxonox/objects/worldentities/MovableEntity.h
r2171 r2662 22 22 * Author: 23 23 * Fabian 'x3n' Landau 24 * Reto Grieder 24 25 * Co-authors: 25 26 * ... … … 32 33 #include "OrxonoxPrereqs.h" 33 34 34 #include "WorldEntity.h" 35 #include "objects/Tickable.h" 35 #include "MobileEntity.h" 36 36 #include "network/ClientConnectionListener.h" 37 #include "tools/Timer.h" 37 38 38 39 namespace orxonox 39 40 { 40 class _OrxonoxExport MovableEntity : public WorldEntity, public Tickable, public ClientConnectionListener41 class _OrxonoxExport MovableEntity : public MobileEntity, public ClientConnectionListener 41 42 { 42 43 public: … … 45 46 46 47 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 47 virtual void tick(float dt);48 48 void registerVariables(); 49 49 50 50 using WorldEntity::setPosition; 51 using WorldEntity::translate;52 51 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 52 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; } 72 inline void setVelocity(float x, float y, float z) 73 { this->velocity_.x = x; this->velocity_.y = y; this->velocity_.z = z; } 74 inline const Vector3& getVelocity() const 75 { return this->velocity_; } 76 77 inline void setAcceleration(const Vector3& acceleration) 78 { this->acceleration_ = acceleration; } 79 inline void setAcceleration(float x, float y, float z) 80 { this->acceleration_.x = x; this->acceleration_.y = y; this->acceleration_.z = z; } 81 inline const Vector3& getAcceleration() const 82 { return this->acceleration_; } 83 84 inline void setRotationAxis(const Vector3& axis) 85 { this->rotationAxis_ = axis; this->rotationAxis_.normalise(); } 86 inline void setRotationAxis(float x, float y, float z) 87 { this->rotationAxis_.x = x; this->rotationAxis_.y = y; this->rotationAxis_.z = z; rotationAxis_.normalise(); } 88 inline const Vector3& getRotationAxis() const 89 { return this->rotationAxis_; } 90 91 inline void setRotationRate(const Degree& angle) 92 { this->rotationRate_ = angle; } 93 inline void setRotationRate(const Radian& angle) 94 { this->rotationRate_ = angle; } 95 inline const Degree& getRotationRate() const 96 { return this->rotationRate_; } 97 98 inline void setMomentum(const Degree& angle) 99 { this->momentum_ = angle; } 100 inline void setMomentum(const Radian& angle) 101 { this->momentum_ = angle; } 102 inline const Degree& getMomentum() const 103 { return this->momentum_; } 53 inline void setPosition(const Vector3& position) 54 { MobileEntity::setPosition(position); this->overwrite_position_ = this->getPosition(); } 55 inline void setOrientation(const Quaternion& orientation) 56 { MobileEntity::setOrientation(orientation); this->overwrite_orientation_ = this->getOrientation(); } 104 57 105 58 private: … … 108 61 void resynchronize(); 109 62 110 void overwritePosition(); 111 void overwriteOrientation(); 63 inline void processLinearVelocity() 64 { this->setVelocity(this->linearVelocity_); } 65 inline void processAngularVelocity() 66 { this->setAngularVelocity(this->angularVelocity_); } 112 67 113 Vector3 velocity_; 114 Vector3 acceleration_; 115 Vector3 rotationAxis_; 116 Degree rotationRate_; 117 Degree momentum_; 68 inline void overwritePosition() 69 { this->setPosition(this->overwrite_position_); } 70 inline void overwriteOrientation() 71 { this->setOrientation(this->overwrite_orientation_); } 118 72 119 Vector3 overwrite_position_;73 Vector3 overwrite_position_; 120 74 Quaternion overwrite_orientation_; 75 76 Timer<MovableEntity> resynchronizeTimer_; 77 Timer<MovableEntity>* continuousResynchroTimer_; 121 78 }; 122 79 } -
code/trunk/src/orxonox/objects/worldentities/ParticleEmitter.cc
r2171 r2662 33 33 34 34 #include "OrxonoxStableHeaders.h" 35 #include "ParticleEmitter.h" 35 36 36 #include "ParticleEmitter.h"37 #include <OgreParticleSystem.h> 37 38 38 39 #include "tools/ParticleInterface.h" … … 46 47 CreateFactory(ParticleEmitter); 47 48 48 ParticleEmitter::ParticleEmitter(BaseObject* creator) : PositionableEntity(creator)49 ParticleEmitter::ParticleEmitter(BaseObject* creator) : StaticEntity(creator) 49 50 { 50 51 RegisterObject(ParticleEmitter); 51 52 52 if ( !this->getScene() || !this->getScene()->getSceneManager())53 ThrowException(AbortLoading, "Can't create Camera, no scene or no scene manager given.");53 if (Core::showsGraphics() && (!this->getScene() || !this->getScene()->getSceneManager())) 54 ThrowException(AbortLoading, "Can't create ParticleEmitter, no scene or no scene manager given."); 54 55 55 56 this->particles_ = 0; … … 62 63 { 63 64 if (this->isInitialized() && this->particles_) 65 { 66 this->detachOgreObject(this->particles_->getParticleSystem()); 64 67 delete this->particles_; 68 } 65 69 } 66 70 … … 75 79 void ParticleEmitter::registerVariables() 76 80 { 77 REGISTERSTRING(this->source_, direction::toclient, new NetworkCallback<ParticleEmitter>(this, &ParticleEmitter::sourceChanged));78 REGISTERDATA (this->LOD_, direction::toclient, new NetworkCallback<ParticleEmitter>(this, &ParticleEmitter::LODchanged));81 registerVariable(this->source_, variableDirection::toclient, new NetworkCallback<ParticleEmitter>(this, &ParticleEmitter::sourceChanged)); 82 registerVariable((int&)(this->LOD_), variableDirection::toclient, new NetworkCallback<ParticleEmitter>(this, &ParticleEmitter::LODchanged)); 79 83 } 80 84 … … 98 102 { 99 103 if (this->particles_) 104 { 100 105 delete this->particles_; 106 this->particles_ = 0; 107 } 101 108 102 if ( this->getScene() && this->getScene()->getSceneManager())109 if (Core::showsGraphics() && this->getScene() && this->getScene()->getSceneManager()) 103 110 { 104 111 try 105 112 { 106 113 this->particles_ = new ParticleInterface(this->getScene()->getSceneManager(), this->source_, this->LOD_); 107 this-> particles_->addToSceneNode(this->getNode());114 this->attachOgreObject(this->particles_->getParticleSystem()); 108 115 this->particles_->setVisible(this->isVisible()); 109 116 this->particles_->setEnabled(this->isActive()); … … 115 122 } 116 123 } 117 else118 this->particles_ = 0;119 124 } 120 125 -
code/trunk/src/orxonox/objects/worldentities/ParticleEmitter.h
r2087 r2662 31 31 32 32 #include "OrxonoxPrereqs.h" 33 #include " PositionableEntity.h"33 #include "StaticEntity.h" 34 34 35 35 namespace orxonox 36 36 { 37 class _OrxonoxExport ParticleEmitter : public PositionableEntity37 class _OrxonoxExport ParticleEmitter : public StaticEntity 38 38 { 39 39 public: -
code/trunk/src/orxonox/objects/worldentities/ParticleSpawner.cc
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/objects/worldentities/ParticleSpawner.h
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/objects/worldentities/SpawnPoint.cc
r2087 r2662 38 38 CreateFactory(SpawnPoint); 39 39 40 SpawnPoint::SpawnPoint(BaseObject* creator) : PositionableEntity(creator)40 SpawnPoint::SpawnPoint(BaseObject* creator) : StaticEntity(creator) 41 41 { 42 42 RegisterObject(SpawnPoint); -
code/trunk/src/orxonox/objects/worldentities/SpawnPoint.h
r2087 r2662 34 34 #include "core/Identifier.h" 35 35 #include "core/Template.h" 36 #include "PositionableEntity.h"37 36 #include "objects/worldentities/pawns/Pawn.h" 37 #include "objects/worldentities/StaticEntity.h" 38 38 39 39 namespace orxonox 40 40 { 41 class _OrxonoxExport SpawnPoint : public PositionableEntity41 class _OrxonoxExport SpawnPoint : public StaticEntity 42 42 { 43 43 public: -
code/trunk/src/orxonox/objects/worldentities/StaticEntity.cc
- Property svn:mergeinfo changed (with no actual effect on merging)
-
code/trunk/src/orxonox/objects/worldentities/StaticEntity.h
- Property svn:mergeinfo changed (with no actual effect on merging)
-
code/trunk/src/orxonox/objects/worldentities/WorldEntity.cc
r2171 r2662 22 22 * Author: 23 23 * Fabian 'x3n' Landau 24 * Reto Grieder (physics) 24 25 * Co-authors: 25 26 * ... … … 31 32 32 33 #include <cassert> 34 #include <OgreSceneNode.h> 33 35 #include <OgreSceneManager.h> 34 36 #include "BulletDynamics/Dynamics/btRigidBody.h" 37 38 #include "util/Exception.h" 39 #include "util/Convert.h" 35 40 #include "core/CoreIncludes.h" 36 41 #include "core/XMLPort.h" 37 #include "util/Convert.h"38 #include "util/Exception.h"39 42 40 43 #include "objects/Scene.h" 44 #include "objects/collisionshapes/WorldEntityCollisionShape.h" 41 45 42 46 namespace orxonox … … 49 53 const Vector3 WorldEntity::UP = Vector3::UNIT_Y; 50 54 55 /** 56 @brief 57 Creates a new WorldEntity that may immediately be used. 58 All the default values are being set here. 59 */ 51 60 WorldEntity::WorldEntity(BaseObject* creator) : BaseObject(creator), Synchronisable(creator) 52 61 { … … 64 73 this->node_->setOrientation(Quaternion::IDENTITY); 65 74 75 76 // Default behaviour does not include physics 77 this->physicalBody_ = 0; 78 this->bPhysicsActive_ = false; 79 this->bPhysicsActiveSynchronised_ = false; 80 this->bPhysicsActiveBeforeAttaching_ = false; 81 this->collisionShape_ = new WorldEntityCollisionShape(this); 82 this->collisionType_ = None; 83 this->collisionTypeSynchronised_ = None; 84 this->mass_ = 0; 85 this->childrenMass_ = 0; 86 // Using bullet default values 87 this->restitution_ = 0; 88 this->angularFactor_ = 1; 89 this->linearDamping_ = 0; 90 this->angularDamping_ = 0; 91 this->friction_ = 0.5; 92 this->bCollisionCallbackActive_ = false; 93 this->bCollisionResponseActive_ = true; 94 66 95 this->registerVariables(); 67 96 } 68 97 98 /** 99 @brief 100 Destroys the WorldEntity AND ALL its children with it. 101 */ 69 102 WorldEntity::~WorldEntity() 70 103 { 71 104 if (this->isInitialized()) 72 105 { 106 if (this->parent_) 107 this->detachFromParent(); 108 109 for (std::set<WorldEntity*>::const_iterator it = this->children_.begin(); it != this->children_.end(); ) 110 delete (*(it++)); 111 112 if (this->physicalBody_) 113 { 114 this->deactivatePhysics(); 115 delete this->physicalBody_; 116 } 117 delete this->collisionShape_; 118 73 119 this->node_->detachAllObjects(); 74 if (this->getScene()->getSceneManager()) 75 this->getScene()->getSceneManager()->destroySceneNode(this->node_->getName()); 120 this->node_->removeAllChildren(); 121 122 OrxAssert(this->getScene()->getSceneManager(), "No SceneManager defined in a WorldEntity."); 123 this->getScene()->getSceneManager()->destroySceneNode(this->node_->getName()); 76 124 } 77 125 } … … 81 129 SUPER(WorldEntity, XMLPort, xmlelement, mode); 82 130 83 XMLPortParamTemplate(WorldEntity, "position", setPosition, getPosition,xmlelement, mode, const Vector3&);131 XMLPortParamTemplate(WorldEntity, "position", setPosition, getPosition, xmlelement, mode, const Vector3&); 84 132 XMLPortParamTemplate(WorldEntity, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&); 85 XMLPortParamLoadOnly(WorldEntity, "lookat", lookAt_xmlport, xmlelement, mode); 86 XMLPortParamLoadOnly(WorldEntity, "direction", setDirection_xmlport, xmlelement, mode); 87 XMLPortParamLoadOnly(WorldEntity, "yaw", yaw_xmlport, xmlelement, mode); 88 XMLPortParamLoadOnly(WorldEntity, "pitch", pitch_xmlport, xmlelement, mode); 89 XMLPortParamLoadOnly(WorldEntity, "roll", roll_xmlport, xmlelement, mode); 90 XMLPortParamTemplate(WorldEntity, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&); 91 XMLPortParam(WorldEntity, "scale", setScale, getScale, xmlelement, mode); 92 133 XMLPortParamTemplate(WorldEntity, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&); 134 XMLPortParam (WorldEntity, "scale", setScale, getScale, xmlelement, mode); 135 XMLPortParamLoadOnly(WorldEntity, "lookat", lookAt_xmlport, xmlelement, mode); 136 XMLPortParamLoadOnly(WorldEntity, "direction", setDirection_xmlport, xmlelement, mode); 137 XMLPortParamLoadOnly(WorldEntity, "yaw", yaw_xmlport, xmlelement, mode); 138 XMLPortParamLoadOnly(WorldEntity, "pitch", pitch_xmlport, xmlelement, mode); 139 XMLPortParamLoadOnly(WorldEntity, "roll", roll_xmlport, xmlelement, mode); 140 141 // Physics 142 XMLPortParam(WorldEntity, "collisionType", setCollisionTypeStr, getCollisionTypeStr, xmlelement, mode); 143 XMLPortParam(WorldEntity, "collisionResponse", setCollisionResponse, hasCollisionResponse, xmlelement, mode); 144 XMLPortParam(WorldEntity, "mass", setMass, getMass, xmlelement, mode); 145 XMLPortParam(WorldEntity, "restitution", setRestitution, getRestitution, xmlelement, mode); 146 XMLPortParam(WorldEntity, "angularFactor", setAngularFactor, getAngularFactor, xmlelement, mode); 147 XMLPortParam(WorldEntity, "linearDamping", setLinearDamping, getLinearDamping, xmlelement, mode); 148 XMLPortParam(WorldEntity, "angularDamping", setAngularDamping, getAngularDamping, xmlelement, mode); 149 XMLPortParam(WorldEntity, "friction", setFriction, getFriction, xmlelement, mode); 150 151 // Other attached WorldEntities 93 152 XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode); 153 // Attached collision shapes 154 XMLPortObject(WorldEntity, CollisionShape, "collisionShapes", attachCollisionShape, getAttachedCollisionShape, xmlelement, mode); 94 155 } 95 156 96 157 void WorldEntity::registerVariables() 97 158 { 98 REGISTERDATA(this->bActive_, direction::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity)); 99 REGISTERDATA(this->bVisible_, direction::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility)); 100 101 REGISTERDATA(this->getScale3D().x, direction::toclient); 102 REGISTERDATA(this->getScale3D().y, direction::toclient); 103 REGISTERDATA(this->getScale3D().z, direction::toclient); 104 105 REGISTERDATA(this->parentID_, direction::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::updateParent)); 106 } 107 108 void WorldEntity::updateParent() 159 registerVariable(this->mainStateName_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedMainState)); 160 161 registerVariable(this->bActive_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity)); 162 registerVariable(this->bVisible_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility)); 163 164 registerVariable(this->getScale3D(), variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::scaleChanged)); 165 166 // Physics stuff 167 registerVariable(this->mass_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::massChanged)); 168 registerVariable(this->restitution_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::restitutionChanged)); 169 registerVariable(this->angularFactor_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::angularFactorChanged)); 170 registerVariable(this->linearDamping_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::linearDampingChanged)); 171 registerVariable(this->angularDamping_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::angularDampingChanged)); 172 registerVariable(this->friction_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::frictionChanged)); 173 registerVariable(this->bCollisionCallbackActive_, 174 variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::collisionCallbackActivityChanged)); 175 registerVariable(this->bCollisionResponseActive_, 176 variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::collisionResponseActivityChanged)); 177 registerVariable((int&)this->collisionTypeSynchronised_, 178 variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::collisionTypeChanged)); 179 registerVariable(this->bPhysicsActiveSynchronised_, 180 variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::physicsActivityChanged)); 181 182 // Attach to parent if necessary 183 registerVariable(this->parentID_, variableDirection::toclient, new NetworkCallback<WorldEntity>(this, &WorldEntity::parentChanged)); 184 } 185 186 /** 187 @brief 188 Network function that object this instance to its correct parent. 189 */ 190 void WorldEntity::parentChanged() 109 191 { 110 192 if (this->parentID_ != OBJECTID_UNKNOWN) … … 116 198 } 117 199 200 /** 201 @brief 202 Attaches this object to a parent SceneNode. 203 @Remarks 204 Only use this method if you know exactly what you're doing! 205 Normally, attaching works internally by attaching WE's. 206 */ 207 void WorldEntity::attachToNode(Ogre::SceneNode* node) 208 { 209 Ogre::Node* parent = this->node_->getParent(); 210 if (parent) 211 parent->removeChild(this->node_); 212 node->addChild(this->node_); 213 } 214 215 /** 216 @brief 217 Detaches this object from a parent SceneNode. 218 @Remarks 219 Only use this method if you know exactly what you're doing! 220 Normally, attaching works internally by attaching WE's. 221 */ 222 void WorldEntity::detachFromNode(Ogre::SceneNode* node) 223 { 224 node->removeChild(this->node_); 225 // this->getScene()->getRootSceneNode()->addChild(this->node_); 226 } 227 228 /** 229 @brief 230 Network callback for the collision type. Only change the type if it was valid. 231 */ 232 void WorldEntity::collisionTypeChanged() 233 { 234 if (this->collisionTypeSynchronised_ != Dynamic && 235 this->collisionTypeSynchronised_ != Kinematic && 236 this->collisionTypeSynchronised_ != Static && 237 this->collisionTypeSynchronised_ != None) 238 { 239 CCOUT(1) << "Error when collsion Type was received over network. Unknown enum value:" << this->collisionTypeSynchronised_ << std::endl; 240 } 241 else if (this->collisionTypeSynchronised_ != collisionType_) 242 { 243 if (this->parent_) 244 CCOUT(2) << "Warning: Network connection tried to set the collision type of an attached WE. Ignoring." << std::endl; 245 else 246 this->setCollisionType(this->collisionTypeSynchronised_); 247 } 248 } 249 250 //! Network callback for this->bPhysicsActive_ 251 void WorldEntity::physicsActivityChanged() 252 { 253 if (this->bPhysicsActiveSynchronised_) 254 this->activatePhysics(); 255 else 256 this->deactivatePhysics(); 257 } 258 259 //! Function sets whether Bullet should issue a callback on collisions 260 void WorldEntity::collisionCallbackActivityChanged() 261 { 262 if (this->hasPhysics()) 263 { 264 if (this->bCollisionCallbackActive_) 265 this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() | 266 btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK); 267 else 268 this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() & 269 ~btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK); 270 } 271 } 272 273 //! Function sets whether Bullet should react itself to a collision 274 void WorldEntity::collisionResponseActivityChanged() 275 { 276 if (this->hasPhysics()) 277 { 278 if (this->bCollisionResponseActive_) 279 this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() & 280 ~btCollisionObject::CF_NO_CONTACT_RESPONSE); 281 else 282 this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() | 283 btCollisionObject::CF_NO_CONTACT_RESPONSE); 284 } 285 } 286 287 /** 288 @brief 289 Attaches a child WorldEntity to this object. This calls notifyBeingAttached() 290 of the child WE. 291 @Note 292 The collision shape of the child object gets attached nevertheless. That also means 293 that you can change the collision shape of the child and it correctly cascadeds the changes to this instance. 294 Be aware of this implication: When implementing attaching of kinematic objects to others, you have to change 295 this behaviour because you then might not want to merge the collision shapes. 296 */ 118 297 void WorldEntity::attach(WorldEntity* object) 119 298 { 120 if (object ->getParent())121 object->detachFromParent();122 else123 {124 Ogre::Node* parent = object->node_->getParent();125 if (parent) 126 parent->removeChild(object->node_);127 }128 129 this-> node_->addChild(object->node_);299 if (object == this) 300 { 301 COUT(2) << "Warning: Can't attach a WorldEntity to itself." << std::endl; 302 return; 303 } 304 305 if (!object->notifyBeingAttached(this)) 306 return; 307 308 this->attachNode(object->node_); 130 309 this->children_.insert(object); 131 object->parent_ = this; 132 object->parentID_ = this->getObjectID(); 133 } 134 310 311 this->attachCollisionShape(object->collisionShape_); 312 // mass 313 this->childrenMass_ += object->getMass(); 314 recalculateMassProps(); 315 } 316 317 /** 318 @brief 319 Function gets called when this object is being attached to a new parent. 320 321 This operation is only allowed if the collision types "like" each other. 322 - You cannot a attach a non physical object to a physical one. 323 - Dynamic object can NOT be attached at all. 324 - It is also not possible to attach a kinematic to a dynamic one. 325 - Attaching of kinematic objects otherwise is not yet supported. 326 */ 327 bool WorldEntity::notifyBeingAttached(WorldEntity* newParent) 328 { 329 // check first whether attaching is even allowed 330 if (this->hasPhysics()) 331 { 332 if (!newParent->hasPhysics()) 333 { 334 COUT(2) << "Warning: Cannot attach a physical object to a non physical one." << std::endl; 335 return false; 336 } 337 else if (this->isDynamic()) 338 { 339 COUT(2) << "Warning: Cannot attach a dynamic object to a WorldEntity." << std::endl; 340 return false; 341 } 342 else if (this->isKinematic() && newParent->isDynamic()) 343 { 344 COUT(2) << "Warning: Cannot attach a kinematic object to a dynamic one." << std::endl; 345 return false; 346 } 347 else if (this->isKinematic()) 348 { 349 COUT(2) << "Warning: Cannot attach a kinematic object to a static or kinematic one: Not yet implemented." << std::endl; 350 return false; 351 } 352 } 353 354 if (this->isPhysicsActive()) 355 this->bPhysicsActiveBeforeAttaching_ = true; 356 this->deactivatePhysics(); 357 358 if (this->parent_) 359 this->detachFromParent(); 360 361 this->parent_ = newParent; 362 this->parentID_ = newParent->getObjectID(); 363 364 // apply transform to collision shape 365 this->collisionShape_->setPosition(this->getPosition()); 366 this->collisionShape_->setOrientation(this->getOrientation()); 367 // TODO: Scale 368 369 return true; 370 } 371 372 /** 373 @brief 374 Detaches a child WorldEntity from this instance. 375 */ 135 376 void WorldEntity::detach(WorldEntity* object) 136 377 { 137 this->node_->removeChild(object->node_); 378 if (this->children_.find(object) == this->children_.end()) 379 { 380 CCOUT(2) << "Warning: Cannot detach an object that is not a child." << std::endl; 381 return; 382 } 383 384 // collision shapes 385 this->detachCollisionShape(object->collisionShape_); 386 387 // mass 388 if (object->getMass() > 0.0f) 389 { 390 this->childrenMass_ -= object->getMass(); 391 recalculateMassProps(); 392 } 393 394 this->detachNode(object->node_); 138 395 this->children_.erase(object); 139 object->parent_ = 0; 140 object->parentID_ = OBJECTID_UNKNOWN; 141 142 // this->getScene()->getRootSceneNode()->addChild(object->node_); 143 } 144 145 WorldEntity* WorldEntity::getAttachedObject(unsigned int index) const 396 397 object->notifyDetached(); 398 } 399 400 /** 401 @brief 402 Function gets called when the object has been detached from its parent. 403 */ 404 void WorldEntity::notifyDetached() 405 { 406 this->parent_ = 0; 407 this->parentID_ = OBJECTID_UNKNOWN; 408 409 // reset orientation of the collisionShape (cannot be set within a WE usually) 410 this->collisionShape_->setPosition(Vector3::ZERO); 411 this->collisionShape_->setOrientation(Quaternion::IDENTITY); 412 // TODO: Scale 413 414 if (this->bPhysicsActiveBeforeAttaching_) 415 { 416 this->activatePhysics(); 417 this->bPhysicsActiveBeforeAttaching_ = false; 418 } 419 } 420 421 //! Returns an attached object (merely for XMLPort). 422 WorldEntity* WorldEntity::getAttachedObject(unsigned int index) 146 423 { 147 424 unsigned int i = 0; … … 154 431 return 0; 155 432 } 433 434 //! Attaches an Ogre::SceneNode to this WorldEntity. 435 void WorldEntity::attachNode(Ogre::SceneNode* node) 436 { 437 Ogre::Node* parent = node->getParent(); 438 if (parent) 439 parent->removeChild(node); 440 this->node_->addChild(node); 441 } 442 443 //! Detaches an Ogre::SceneNode from this WorldEntity. 444 void WorldEntity::detachNode(Ogre::SceneNode* node) 445 { 446 this->node_->removeChild(node); 447 // this->getScene()->getRootSceneNode()->addChild(node); 448 } 449 450 //! Attaches an Ogre::MovableObject to this WorldEntity. 451 void WorldEntity::attachOgreObject(Ogre::MovableObject* object) 452 { 453 this->node_->attachObject(object); 454 } 455 456 //! Detaches an Ogre::MovableObject from this WorldEntity. 457 void WorldEntity::detachOgreObject(Ogre::MovableObject* object) 458 { 459 this->node_->detachObject(object); 460 } 461 462 //! Detaches an Ogre::MovableObject (by string) from this WorldEntity. 463 Ogre::MovableObject* WorldEntity::detachOgreObject(const Ogre::String& name) 464 { 465 return this->node_->detachObject(name); 466 } 467 468 //! Attaches a collision Shape to this object (delegated to the internal CompoundCollisionShape) 469 void WorldEntity::attachCollisionShape(CollisionShape* shape) 470 { 471 this->collisionShape_->attach(shape); 472 // Note: this->collisionShape_ already notifies us of any changes. 473 } 474 475 //! Detaches a collision Shape from this object (delegated to the internal CompoundCollisionShape) 476 void WorldEntity::detachCollisionShape(CollisionShape* shape) 477 { 478 // Note: The collision shapes may not be detached with this function! 479 this->collisionShape_->detach(shape); 480 // Note: this->collisionShape_ already notifies us of any changes. 481 } 482 483 //! Returns an attached collision Shape of this object (delegated to the internal CompoundCollisionShape) 484 CollisionShape* WorldEntity::getAttachedCollisionShape(unsigned int index) 485 { 486 return this->collisionShape_->getAttachedShape(index); 487 } 488 489 // Note: These functions are placed in WorldEntity.h as inline functions for the release build. 490 #ifndef _NDEBUG 491 const Vector3& WorldEntity::getPosition() const 492 { 493 return this->node_->getPosition(); 494 } 495 496 const Quaternion& WorldEntity::getOrientation() const 497 { 498 return this->node_->getOrientation(); 499 } 500 501 const Vector3& WorldEntity::getScale3D() const 502 { 503 return this->node_->getScale(); 504 } 505 #endif 506 507 //! Returns the position relative to the root space 508 const Vector3& WorldEntity::getWorldPosition() const 509 { 510 return this->node_->_getDerivedPosition(); 511 } 512 513 //! Returns the orientation relative to the root space 514 const Quaternion& WorldEntity::getWorldOrientation() const 515 { 516 return this->node_->_getDerivedOrientation(); 517 } 518 519 //! Returns the scaling applied relative to the root space in 3 coordinates 520 const Vector3& WorldEntity::getWorldScale3D() const 521 { 522 return this->node_->_getDerivedScale(); 523 } 524 525 /** 526 @brief 527 Returns the scaling applied relative to the root space in 3 coordinates 528 @return 529 Returns the scaling if it is uniform, 1.0f otherwise. 530 */ 531 float WorldEntity::getWorldScale() const 532 { 533 Vector3 scale = this->getWorldScale3D(); 534 return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; 535 } 536 537 /** 538 @brief 539 Sets the three dimensional scaling of this object. 540 @Note 541 Scaling physical objects has not yet been implemented and is therefore forbidden. 542 */ 543 void WorldEntity::setScale3D(const Vector3& scale) 544 { 545 /* 546 HACK HACK HACK 547 if (bScalePhysics && this->hasPhysics() && scale != Vector3::UNIT_SCALE) 548 { 549 CCOUT(2) << "Warning: Cannot set the scale of a physical object: Not yet implemented. Ignoring scaling." << std::endl; 550 return; 551 } 552 HACK HACK HACK 553 */ 554 this->node_->setScale(scale); 555 556 this->changedScale(); 557 } 558 559 /** 560 @brief 561 Translates this WorldEntity by a vector. 562 @param relativeTo 563 @see TransformSpace::Enum 564 */ 565 void WorldEntity::translate(const Vector3& distance, TransformSpace::Enum relativeTo) 566 { 567 switch (relativeTo) 568 { 569 case TransformSpace::Local: 570 // position is relative to parent so transform downwards 571 this->setPosition(this->getPosition() + this->getOrientation() * distance); 572 break; 573 case TransformSpace::Parent: 574 this->setPosition(this->getPosition() + distance); 575 break; 576 case TransformSpace::World: 577 // position is relative to parent so transform upwards 578 if (this->node_->getParent()) 579 setPosition(getPosition() + (node_->getParent()->_getDerivedOrientation().Inverse() * distance) 580 / node_->getParent()->_getDerivedScale()); 581 else 582 this->setPosition(this->getPosition() + distance); 583 break; 584 } 585 } 586 587 /** 588 @brief 589 Rotates this WorldEntity by a quaternion. 590 @param relativeTo 591 @see TransformSpace::Enum 592 */ 593 void WorldEntity::rotate(const Quaternion& rotation, TransformSpace::Enum relativeTo) 594 { 595 switch(relativeTo) 596 { 597 case TransformSpace::Local: 598 this->setOrientation(this->getOrientation() * rotation); 599 break; 600 case TransformSpace::Parent: 601 // Rotations are normally relative to local axes, transform up 602 this->setOrientation(rotation * this->getOrientation()); 603 break; 604 case TransformSpace::World: 605 // Rotations are normally relative to local axes, transform up 606 this->setOrientation(this->getOrientation() * this->getWorldOrientation().Inverse() 607 * rotation * this->getWorldOrientation()); 608 break; 609 } 610 } 611 612 /** 613 @brief 614 Makes this WorldEntity look a specific target location. 615 @param relativeTo 616 @see TransformSpace::Enum 617 @param localDirectionVector 618 The vector which normally describes the natural direction of the object, usually -Z. 619 */ 620 void WorldEntity::lookAt(const Vector3& target, TransformSpace::Enum relativeTo, const Vector3& localDirectionVector) 621 { 622 Vector3 origin; 623 switch (relativeTo) 624 { 625 case TransformSpace::Local: 626 origin = Vector3::ZERO; 627 break; 628 case TransformSpace::Parent: 629 origin = this->getPosition(); 630 break; 631 case TransformSpace::World: 632 origin = this->getWorldPosition(); 633 break; 634 } 635 this->setDirection(target - origin, relativeTo, localDirectionVector); 636 } 637 638 /** 639 @brief 640 Makes this WorldEntity look in specific direction. 641 @param relativeTo 642 @see TransformSpace::Enum 643 @param localDirectionVector 644 The vector which normally describes the natural direction of the object, usually -Z. 645 */ 646 void WorldEntity::setDirection(const Vector3& direction, TransformSpace::Enum relativeTo, const Vector3& localDirectionVector) 647 { 648 Quaternion savedOrientation(this->getOrientation()); 649 Ogre::Node::TransformSpace ogreRelativeTo; 650 switch (relativeTo) 651 { 652 case TransformSpace::Local: 653 ogreRelativeTo = Ogre::Node::TS_LOCAL; break; 654 case TransformSpace::Parent: 655 ogreRelativeTo = Ogre::Node::TS_PARENT; break; 656 case TransformSpace::World: 657 ogreRelativeTo = Ogre::Node::TS_WORLD; break; 658 } 659 this->node_->setDirection(direction, ogreRelativeTo, localDirectionVector); 660 Quaternion newOrientation(this->node_->getOrientation()); 661 this->node_->setOrientation(savedOrientation); 662 this->setOrientation(newOrientation); 663 } 664 665 //! Activates physics if the CollisionType is not None. 666 void WorldEntity::activatePhysics() 667 { 668 if (this->isActive() && this->hasPhysics() && !this->isPhysicsActive() && !this->parent_) 669 { 670 this->getScene()->addPhysicalObject(this); 671 this->bPhysicsActive_ = true; 672 this->bPhysicsActiveSynchronised_ = true; 673 } 674 } 675 676 //! Deactivates physics but the CollisionType does not change. 677 void WorldEntity::deactivatePhysics() 678 { 679 if (this->isPhysicsActive()) 680 { 681 this->getScene()->removePhysicalObject(this); 682 this->bPhysicsActive_ = false; 683 this->bPhysicsActiveSynchronised_ = false; 684 } 685 } 686 687 //! Tells whether the object has already been added to the Bullet physics World. 688 bool WorldEntity::addedToPhysicalWorld() const 689 { 690 return this->physicalBody_ && this->physicalBody_->isInWorld(); 691 } 692 693 /** 694 @brief 695 Sets the CollisionType. This alters the object significantly! @see CollisionType. 696 @Note 697 Operation does not work on attached WorldEntities. 698 */ 699 void WorldEntity::setCollisionType(CollisionType type) 700 { 701 if (this->collisionType_ == type) 702 return; 703 704 // If we are already attached to a parent, this would be a bad idea.. 705 if (this->parent_) 706 { 707 CCOUT(2) << "Warning: Cannot set the collision type of a WorldEntity with a parent." << std::endl; 708 return; 709 } 710 711 // Check for type legality. Could be StaticEntity or MobileEntity. 712 if (!this->isCollisionTypeLegal(type)) 713 return; 714 715 if (this->isPhysicsActive()) 716 this->deactivatePhysics(); 717 718 bool bReactivatePhysics = true; 719 if (this->hasPhysics() && !this->isPhysicsActive()) 720 bReactivatePhysics = false; 721 722 // Check whether we have to create or destroy. 723 if (type != None && this->collisionType_ == None) 724 { 725 /* 726 HACK HACK HACK 727 // Check whether there was some scaling applied. 728 if (!this->node_->getScale().positionEquals(Vector3(1, 1, 1), 0.001)) 729 { 730 CCOUT(2) << "Warning: Cannot create a physical body if there is scaling applied to the node: Not yet implemented." << std::endl; 731 return; 732 } 733 HACK HACK HACK 734 */ 735 // Create new rigid body 736 btRigidBody::btRigidBodyConstructionInfo bodyConstructionInfo(0, this, this->collisionShape_->getCollisionShape()); 737 this->physicalBody_ = new btRigidBody(bodyConstructionInfo); 738 this->physicalBody_->setUserPointer(this); 739 this->physicalBody_->setActivationState(DISABLE_DEACTIVATION); 740 } 741 else if (type == None && this->collisionType_ != None) 742 { 743 // Destroy rigid body 744 assert(this->physicalBody_); 745 deactivatePhysics(); 746 delete this->physicalBody_; 747 this->physicalBody_ = 0; 748 this->collisionType_ = None; 749 this->collisionTypeSynchronised_ = None; 750 return; 751 } 752 753 // Change type 754 switch (type) 755 { 756 case Dynamic: 757 this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() & !(btCollisionObject::CF_STATIC_OBJECT | btCollisionObject::CF_KINEMATIC_OBJECT)); 758 break; 759 case Kinematic: 760 this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() & !btCollisionObject::CF_STATIC_OBJECT | btCollisionObject::CF_KINEMATIC_OBJECT); 761 break; 762 case Static: 763 this->physicalBody_->setCollisionFlags(this->physicalBody_->getCollisionFlags() & !btCollisionObject::CF_KINEMATIC_OBJECT | btCollisionObject::CF_STATIC_OBJECT); 764 break; 765 case None: 766 assert(false); // Doesn't happen 767 return; 768 } 769 this->collisionType_ = type; 770 this->collisionTypeSynchronised_ = type; 771 772 // update mass and inertia tensor 773 recalculateMassProps(); 774 internalSetPhysicsProps(); 775 collisionCallbackActivityChanged(); 776 collisionResponseActivityChanged(); 777 if (bReactivatePhysics) 778 activatePhysics(); 779 } 780 781 //! Sets the CollisionType by string (used for the XMLPort) 782 void WorldEntity::setCollisionTypeStr(const std::string& typeStr) 783 { 784 std::string typeStrLower = getLowercase(typeStr); 785 CollisionType type; 786 if (typeStrLower == "dynamic") 787 type = Dynamic; 788 else if (typeStrLower == "static") 789 type = Static; 790 else if (typeStrLower == "kinematic") 791 type = Kinematic; 792 else if (typeStrLower == "none") 793 type = None; 794 else 795 ThrowException(ParseError, std::string("Attempting to set an unknown collision type: '") + typeStr + "'."); 796 this->setCollisionType(type); 797 } 798 799 //! Gets the CollisionType by string (used for the XMLPort) 800 std::string WorldEntity::getCollisionTypeStr() const 801 { 802 switch (this->getCollisionType()) 803 { 804 case Dynamic: 805 return "dynamic"; 806 case Kinematic: 807 return "kinematic"; 808 case Static: 809 return "static"; 810 case None: 811 return "none"; 812 default: 813 assert(false); 814 return ""; 815 } 816 } 817 818 /** 819 @brief 820 Recalculates the accumulated child mass and calls recalculateMassProps() 821 and notifies the parent of the change. 822 @Note 823 Called by a child WE 824 */ 825 void WorldEntity::notifyChildMassChanged() 826 { 827 // Note: CollisionShape changes of a child get handled over the internal CompoundCollisionShape already 828 // Recalculate mass 829 this->childrenMass_ = 0.0f; 830 for (std::set<WorldEntity*>::const_iterator it = this->children_.begin(); it != this->children_.end(); ++it) 831 this->childrenMass_ += (*it)->getMass(); 832 recalculateMassProps(); 833 // Notify parent WE 834 if (this->parent_) 835 parent_->notifyChildMassChanged(); 836 } 837 838 /** 839 @brief 840 Undertakes the necessary steps to change the collision shape in Bullet, even at runtime. 841 @Note 842 - called by this->collisionShape_ 843 - May have a REALLY big overhead when called continuously at runtime, because then we need 844 to remove the physical body from Bullet and add it again. 845 */ 846 void WorldEntity::notifyCollisionShapeChanged() 847 { 848 if (hasPhysics()) 849 { 850 // Bullet doesn't like sudden changes of the collision shape, so we remove and add it again 851 if (this->addedToPhysicalWorld()) 852 { 853 this->deactivatePhysics(); 854 this->physicalBody_->setCollisionShape(this->collisionShape_->getCollisionShape()); 855 this->activatePhysics(); 856 } 857 else 858 this->physicalBody_->setCollisionShape(this->collisionShape_->getCollisionShape()); 859 } 860 recalculateMassProps(); 861 } 862 863 //! Updates all mass dependent parameters (mass, inertia tensor and child mass) 864 void WorldEntity::recalculateMassProps() 865 { 866 // Store local inertia for faster access. Evaluates to (0,0,0) if there is no collision shape. 867 float totalMass = this->mass_ + this->childrenMass_; 868 this->collisionShape_->calculateLocalInertia(totalMass, this->localInertia_); 869 if (this->hasPhysics()) 870 { 871 if (this->isStatic()) 872 { 873 // Just set everything to zero 874 this->physicalBody_->setMassProps(0.0f, btVector3(0, 0, 0)); 875 } 876 else if ((this->mass_ + this->childrenMass_) == 0.0f) 877 { 878 // Use default values to avoid very large or very small values 879 CCOUT(4) << "Warning: Setting the internal physical mass to 1.0 because mass_ is 0.0" << std::endl; 880 btVector3 inertia(0, 0, 0); 881 this->collisionShape_->calculateLocalInertia(1.0f, inertia); 882 this->physicalBody_->setMassProps(1.0f, inertia); 883 } 884 else 885 { 886 this->physicalBody_->setMassProps(totalMass, this->localInertia_); 887 } 888 } 889 } 890 891 //! Copies our own parameters for restitution, angular factor, dampings and friction to the bullet rigid body. 892 void WorldEntity::internalSetPhysicsProps() 893 { 894 if (this->hasPhysics()) 895 { 896 this->physicalBody_->setRestitution(this->restitution_); 897 this->physicalBody_->setAngularFactor(this->angularFactor_); 898 this->physicalBody_->setDamping(this->linearDamping_, this->angularDamping_); 899 this->physicalBody_->setFriction(this->friction_); 900 } 901 } 156 902 } -
code/trunk/src/orxonox/objects/worldentities/WorldEntity.h
r2171 r2662 22 22 * Author: 23 23 * Fabian 'x3n' Landau 24 * Reto Grieder (physics) 24 25 * Co-authors: 25 26 * ... … … 32 33 #include "OrxonoxPrereqs.h" 33 34 34 #define OGRE_FORCE_ANGLE_TYPES 35 35 #ifdef _NDEBUG 36 36 #include <OgreSceneNode.h> 37 38 #include "network/Synchronisable.h" 37 #else 38 #include <OgrePrerequisites.h> 39 #endif 40 #include "LinearMath/btMotionState.h" 41 42 #include "util/Math.h" 39 43 #include "core/BaseObject.h" 40 #include " util/Math.h"44 #include "network/synchronisable/Synchronisable.h" 41 45 42 46 namespace orxonox 43 47 { 44 class _OrxonoxExport WorldEntity : public BaseObject, public Synchronisable 48 /** 49 @brief 50 The WorldEntity represents everything that can be put in a Scene at a certain location. 51 52 It is supposed to be the base class of everything you would call an 'object' in a Scene. 53 The class itself is abstract which means you cannot use it directly. You may use StaticEntity 54 as the simplest derivative or (derived from MobileEntity) MovableEntity and ControllableEntity 55 as more advanced ones. 56 57 The basic task of the WorldEntity is provide a location, a direction and a scaling and the possibility 58 to create an entire hierarchy of derivated objects. 59 It is also the basis for the physics interface to the Bullet physics engine. 60 Every WorldEntity can have a specific collision type: @see CollisionType 61 This would then imply that every scene object could have any collision type. To limit this, you can always 62 override this->isCollisionTypeLegal(CollisionType). Return false if the collision type is not supported 63 for a specific object. 64 There is also support for attaching WorldEntities with physics to each other. Currently, the collision shape 65 of both objects simply get merged into one larger shape (for static collision type). 66 The phyiscal body that is internally stored and administrated has the following supported properties: 67 - Restitution, angular factor, linear damping, angular damping, fricition, mass and collision shape. 68 You can get more information at the corresponding set function. 69 70 Collision shapes: These are controlled by the internal WorldEntityCollisionShape. @see WorldEntityCollisionShape. 71 */ 72 class _OrxonoxExport WorldEntity : public BaseObject, public Synchronisable, public btMotionState 45 73 { 74 friend class Scene; 75 46 76 public: 47 77 WorldEntity(BaseObject* creator); … … 51 81 void registerVariables(); 52 82 53 inline Ogre::SceneNode* getNode() const83 inline const Ogre::SceneNode* getNode() const 54 84 { return this->node_; } 55 85 … … 64 94 inline void setPosition(float x, float y, float z) 65 95 { this->setPosition(Vector3(x, y, z)); } 66 inline const Vector3& getPosition() const 67 { return this->node_->getPosition(); } 68 inline const Vector3& getWorldPosition() const 69 { return this->node_->getWorldPosition(); } 70 71 virtual void translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0; 72 inline void translate(float x, float y, float z, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) 96 const Vector3& getPosition() const; 97 const Vector3& getWorldPosition() const; 98 99 void translate(const Vector3& distance, TransformSpace::Enum relativeTo = TransformSpace::Parent); 100 inline void translate(float x, float y, float z, TransformSpace::Enum relativeTo = TransformSpace::Parent) 73 101 { this->translate(Vector3(x, y, z), relativeTo); } 102 103 virtual inline const Vector3& getVelocity() const 104 { return Vector3::ZERO; } 74 105 75 106 virtual void setOrientation(const Quaternion& orientation) = 0; … … 80 111 inline void setOrientation(const Vector3& axis, const Degree& angle) 81 112 { this->setOrientation(Quaternion(angle, axis)); } 82 inline const Quaternion& getOrientation() const 83 { return this->node_->getOrientation(); } 84 inline const Quaternion& getWorldOrientation() const 85 { return this->node_->getWorldOrientation(); } 86 87 virtual void rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0; 88 inline void rotate(const Vector3& axis, const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) 113 const Quaternion& getOrientation() const; 114 const Quaternion& getWorldOrientation() const; 115 116 void rotate(const Quaternion& rotation, TransformSpace::Enum relativeTo = TransformSpace::Local); 117 inline void rotate(const Vector3& axis, const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local) 89 118 { this->rotate(Quaternion(angle, axis), relativeTo); } 90 inline void rotate(const Vector3& axis, const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) 91 { this->rotate(Quaternion(angle, axis), relativeTo); } 92 93 virtual void yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0; 94 inline void yaw(const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) 95 { this->yaw(Degree(angle), relativeTo); } 96 virtual void pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0; 97 inline void pitch(const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) 98 { this->pitch(Degree(angle), relativeTo); } 99 virtual void roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) = 0; 100 inline void roll(const Radian& angle, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL) 101 { this->roll(Degree(angle), relativeTo); } 102 103 virtual void lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z) = 0; 104 virtual void setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z) = 0; 105 inline void setDirection(float x, float y, float z, Ogre::Node::TransformSpace relativeTo = Ogre::Node::TS_LOCAL, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z) 119 120 inline void yaw(const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local) 121 { this->rotate(Quaternion(angle, Vector3::UNIT_Y), relativeTo); } 122 inline void pitch(const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local) 123 { this->rotate(Quaternion(angle, Vector3::UNIT_X), relativeTo); } 124 inline void roll(const Degree& angle, TransformSpace::Enum relativeTo = TransformSpace::Local) 125 { this->rotate(Quaternion(angle, Vector3::UNIT_Z), relativeTo); } 126 127 void lookAt(const Vector3& target, TransformSpace::Enum relativeTo = TransformSpace::Parent, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); 128 void setDirection(const Vector3& direction, TransformSpace::Enum relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z); 129 inline void setDirection(float x, float y, float z, TransformSpace::Enum relativeTo = TransformSpace::Local, const Vector3& localDirectionVector = Vector3::NEGATIVE_UNIT_Z) 106 130 { this->setDirection(Vector3(x, y, z), relativeTo, localDirectionVector); } 107 131 108 inline void setScale3D(const Vector3& scale) 109 { this->node_->setScale(scale); } 132 virtual void setScale3D(const Vector3& scale); 110 133 inline void setScale3D(float x, float y, float z) 111 { this-> node_->setScale(x, y, z); }112 inline const Vector3& getScale3D(void) const113 { return this->node_->getScale(); }134 { this->setScale3D(Vector3(x, y, z)); } 135 const Vector3& getScale3D(void) const; 136 const Vector3& getWorldScale3D() const; 114 137 115 138 inline void setScale(float scale) 116 { this-> node_->setScale(scale, scale, scale); }139 { this->setScale3D(scale, scale, scale); } 117 140 inline float getScale() const 118 141 { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; } 142 float getWorldScale() const; 119 143 120 144 inline void scale3D(const Vector3& scale) 121 { this-> node_->scale(scale); }145 { this->setScale3D(this->getScale3D() * scale); } 122 146 inline void scale3D(float x, float y, float z) 123 { this-> node_->scale(x, y, z); }147 { this->scale3D(Vector3(x, y, z)); } 124 148 inline void scale(float scale) 125 { this->node_->scale(scale, scale, scale); } 149 { this->scale3D(scale, scale, scale); } 150 151 virtual void changedScale() {} 126 152 127 153 void attach(WorldEntity* object); 128 154 void detach(WorldEntity* object); 129 WorldEntity* getAttachedObject(unsigned int index) const;155 WorldEntity* getAttachedObject(unsigned int index); 130 156 inline const std::set<WorldEntity*>& getAttachedObjects() const 131 157 { return this->children_; } 158 159 void attachOgreObject(Ogre::MovableObject* object); 160 void detachOgreObject(Ogre::MovableObject* object); 161 Ogre::MovableObject* detachOgreObject(const Ogre::String& name); 132 162 133 163 inline void attachToParent(WorldEntity* parent) … … 138 168 { return this->parent_; } 139 169 170 void attachNode(Ogre::SceneNode* node); 171 void detachNode(Ogre::SceneNode* node); 172 void attachToNode(Ogre::SceneNode* node); 173 void detachFromNode(Ogre::SceneNode* node); 174 175 void notifyChildPropsChanged(); 176 140 177 protected: 141 178 Ogre::SceneNode* node_; 142 179 143 180 private: 144 void updateParent();145 146 181 inline void lookAt_xmlport(const Vector3& target) 147 182 { this->lookAt(target); } … … 155 190 { this->roll(angle); } 156 191 192 // network callbacks 193 void parentChanged(); 194 inline void scaleChanged() 195 { this->setScale3D(this->getScale3D()); } 196 157 197 WorldEntity* parent_; 158 198 unsigned int parentID_; 159 199 std::set<WorldEntity*> children_; 200 201 202 ///////////// 203 // Physics // 204 ///////////// 205 206 public: 207 /** 208 @brief 209 Denotes the possible types of physical objects in a Scene. 210 211 Dynamic: The object is influenced by its physical environment, like for instance little ball. 212 Kinematic: The object can only influence other dynamic objects. It's movement is coordinated by your own saying. 213 Static: Like kinematic but the object is not allowed to move during the simulation. 214 None: The object has no physics at all. 215 */ 216 enum CollisionType 217 { 218 Dynamic, 219 Kinematic, 220 Static, 221 None 222 }; 223 224 //! Tells whether the object has any connection to the Bullet physics engine. If hasPhysics() is false, the object may still have a velocity. 225 bool hasPhysics() const { return getCollisionType() != None ; } 226 //! @see CollisionType 227 bool isStatic() const { return getCollisionType() == Static ; } 228 //! @see CollisionType 229 bool isKinematic() const { return getCollisionType() == Kinematic; } 230 //! @see CollisionType 231 bool isDynamic() const { return getCollisionType() == Dynamic ; } 232 //! Tells whether physics has been activated (you can temporarily deactivate it) 233 bool isPhysicsActive() const { return this->bPhysicsActive_; } 234 bool addedToPhysicalWorld() const; 235 236 void activatePhysics(); 237 void deactivatePhysics(); 238 239 //! Returns the CollisionType. @see CollisionType. 240 inline CollisionType getCollisionType() const 241 { return this->collisionType_; } 242 void setCollisionType(CollisionType type); 243 244 void setCollisionTypeStr(const std::string& type); 245 std::string getCollisionTypeStr() const; 246 247 //! Sets the mass of this object. Note that the total mass may be influenced by attached objects! 248 inline void setMass(float mass) 249 { this->mass_ = mass; recalculateMassProps(); } 250 //! Returns the mass of this object without its children. 251 inline float getMass() const 252 { return this->mass_; } 253 254 //! Returns the total mass of this object with all its attached children. 255 inline float getTotalMass() const 256 { return this->mass_ + this->childrenMass_; } 257 258 /** 259 @brief 260 Returns the diagonal elements of the inertia tensor when calculated in local coordinates. 261 @Note 262 The local inertia tensor cannot be set, but is calculated by Bullet according to the collisionShape. 263 With compound collision shapes, an approximation is used. 264 */ 265 inline const btVector3& getLocalInertia() const 266 { return this->localInertia_; } 267 268 /** 269 @brief 270 Sets how much reaction is applied in a collision. 271 272 Consider two equal spheres colliding with equal velocities: 273 Restitution 1 means that both spheres simply reverse their velocity (no loss of energy) 274 Restitution 0 means that both spheres will immediately stop moving 275 (maximum loss of energy without violating of the preservation of momentum) 276 */ 277 inline void setRestitution(float restitution) 278 { this->restitution_ = restitution; internalSetPhysicsProps(); } 279 //! Returns the restitution parameter. @see setRestitution. 280 inline float getRestitution() const 281 { return this->restitution_; } 282 283 /** 284 @brief 285 Sets an artificial parameter that tells how much torque is applied when you apply a non-central force. 286 287 Normally the angular factor is 1, which means it's physically 'correct'. Howerver if you have a player 288 character that should not rotate when hit sideways, you can set the angular factor to 0. 289 */ 290 inline void setAngularFactor(float angularFactor) 291 { this->angularFactor_ = angularFactor; internalSetPhysicsProps(); } 292 //! Returns the angular factor. @see setAngularFactor. 293 inline float getAngularFactor() const 294 { return this->angularFactor_; } 295 296 //! Applies a mass independent damping. Velocities will simply diminish exponentially. 297 inline void setLinearDamping(float linearDamping) 298 { this->linearDamping_ = linearDamping; internalSetPhysicsProps(); } 299 //! Returns the linear damping. @see setLinearDamping. 300 inline float getLinearDamping() const 301 { return this->linearDamping_; } 302 303 //! Applies a tensor independent rotation damping. Angular velocities will simply diminish exponentially. 304 inline void setAngularDamping(float angularDamping) 305 { this->angularDamping_ = angularDamping; internalSetPhysicsProps(); } 306 //! Returns the angular damping. @see setAngularDamping. 307 inline float getAngularDamping() const 308 { return this->angularDamping_; } 309 310 //! Applies friction to the object. Friction occurs when two objects collide. 311 inline void setFriction(float friction) 312 { this->friction_ = friction; internalSetPhysicsProps(); } 313 //! Returns the amount of friction applied to the object. 314 inline float getFriction() const 315 { return this->friction_; } 316 317 void attachCollisionShape(CollisionShape* shape); 318 void detachCollisionShape(CollisionShape* shape); 319 CollisionShape* getAttachedCollisionShape(unsigned int index); 320 321 void notifyCollisionShapeChanged(); 322 void notifyChildMassChanged(); 323 324 /** 325 @brief 326 Virtual function that gets called when this object collides with another. 327 @param otherObject 328 The object this one has collided into. 329 @pram contactPoint 330 Contact point provided by Bullet. Holds more information and can me modified. See return value. 331 @Return 332 Returning false means that no modification to the contactPoint has been made. Return true otherwise! 333 @Note 334 Condition is that enableCollisionCallback() was called. 335 */ 336 virtual inline bool collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint) 337 { return false; } /* With false, Bullet assumes no modification to the collision objects. */ 338 339 //! Enables the collidesAgainst(.) function. The object doesn't respond to collision otherwise! 340 inline void enableCollisionCallback() 341 { this->bCollisionCallbackActive_ = true; this->collisionCallbackActivityChanged(); } 342 //! Disables the collidesAgainst(.) function. @see enableCollisionCallback() 343 inline void disableCollisionCallback() 344 { this->bCollisionCallbackActive_ = false; this->collisionCallbackActivityChanged(); } 345 //! Tells whether there could be a collision callback via collidesAgainst(.) 346 inline bool isCollisionCallbackActive() const 347 { return this->bCollisionCallbackActive_; } 348 349 //! Enables or disables collision response (default is of course on) 350 inline void setCollisionResponse(bool value) 351 { this->bCollisionResponseActive_ = value; this->collisionResponseActivityChanged(); } 352 //! Tells whether there could be a collision response 353 inline bool hasCollisionResponse() 354 { return this->bCollisionResponseActive_; } 355 356 protected: 357 /** 358 @brief 359 Function checks whether the requested collision type is legal to this object. 360 361 You can override this function in a derived class to constrain the collision to e.g. None or Dynamic. 362 A projectile may not prove very useful if there is no physical body. Simply set the CollisionType 363 in its constructor and override this method. But be careful that a derived classe's virtual functions 364 don't yet exist in the constructor if a base class. 365 */ 366 virtual bool isCollisionTypeLegal(CollisionType type) const = 0; 367 368 btRigidBody* physicalBody_; //!< Bullet rigid body. Everything physical is applied to this instance. 369 370 private: 371 void recalculateMassProps(); 372 void internalSetPhysicsProps(); 373 374 bool notifyBeingAttached(WorldEntity* newParent); 375 void notifyDetached(); 376 377 // network callbacks 378 void collisionTypeChanged(); 379 void physicsActivityChanged(); 380 void collisionCallbackActivityChanged(); 381 void collisionResponseActivityChanged(); 382 //! Network callback workaround to call a function when the value changes. 383 inline void massChanged() 384 { this->setMass(this->mass_); } 385 //! Network callback workaround to call a function when the value changes. 386 inline void restitutionChanged() 387 { this->setRestitution(this->restitution_); } 388 //! Network callback workaround to call a function when the value changes. 389 inline void angularFactorChanged() 390 { this->setAngularFactor(this->angularFactor_); } 391 //! Network callback workaround to call a function when the value changes. 392 inline void linearDampingChanged() 393 { this->setLinearDamping(this->linearDamping_); } 394 //! Network callback workaround to call a function when the value changes. 395 inline void angularDampingChanged() 396 { this->setAngularDamping(this->angularDamping_); } 397 //! Network callback workaround to call a function when the value changes. 398 inline void frictionChanged() 399 { this->setFriction(this->friction_); } 400 401 CollisionType collisionType_; //!< @see setCollisionType 402 CollisionType collisionTypeSynchronised_; //!< Network synchronised variable for collisionType_ 403 bool bPhysicsActive_; //!< @see isPhysicsActive 404 bool bPhysicsActiveSynchronised_; //!< Network synchronised variable for bPhysicsActive_ 405 //! When attaching objects hierarchically this variable tells this object (as child) whether physics was activated before attaching (because the deactivate physics while being attached). 406 bool bPhysicsActiveBeforeAttaching_; 407 WorldEntityCollisionShape* collisionShape_; //!< Attached collision shapes go here 408 btScalar mass_; //!< @see setMass 409 btVector3 localInertia_; //!< @see getLocalInertia 410 btScalar restitution_; //!< @see setRestitution 411 btScalar angularFactor_; //!< @see setAngularFactor 412 btScalar linearDamping_; //!< @see setLinearDamping 413 btScalar angularDamping_; //!< @see setAngularDamping 414 btScalar friction_; //!< @see setFriction 415 btScalar childrenMass_; //!< Sum of all the children's masses 416 bool bCollisionCallbackActive_; //!< @see enableCollisionCallback 417 bool bCollisionResponseActive_; //!< Tells whether the object should respond to collisions 160 418 }; 419 420 // Inline heavily used functions for release builds. In debug, we better avoid including OgreSceneNode here. 421 #ifdef _NDEBUG 422 inline const Vector3& WorldEntity::getPosition() const 423 { return this->node_->getPosition(); } 424 inline const Quaternion& WorldEntity::getOrientation() const 425 { return this->node_->getrOrientation(); } 426 inline const Vector3& WorldEntity::getScale3D(void) const 427 { return this->node_->getScale(); } 428 #endif 429 430 SUPER_FUNCTION(5, WorldEntity, changedScale, false); 161 431 } 162 432 -
code/trunk/src/orxonox/objects/worldentities/pawns/Pawn.cc
r2171 r2662 30 30 #include "Pawn.h" 31 31 32 #include "core/Core.h" 32 33 #include "core/CoreIncludes.h" 33 34 #include "core/XMLPort.h" 34 35 #include "util/Math.h" 36 #include "PawnManager.h" 35 37 #include "objects/infos/PlayerInfo.h" 36 38 #include "objects/gametypes/Gametype.h" 37 #include "objects/weaponSystem/WeaponSystem.h" 39 #include "objects/worldentities/ParticleSpawner.h" 40 #include "objects/worldentities/ExplosionChunk.h" 38 41 39 42 namespace orxonox … … 45 48 RegisterObject(Pawn); 46 49 47 this->bAlive_ = false; 50 PawnManager::touch(); 51 this->bAlive_ = true; 52 this->fire_ = 0x0; 53 this->firehack_ = 0x0; 48 54 49 55 this->health_ = 0; … … 52 58 53 59 this->lastHitOriginator_ = 0; 54 this->weaponSystem_ = 0; 55 56 /* 57 //WeaponSystem 58 weaponSystem_ = new WeaponSystem(); 59 WeaponSet * weaponSet1 = new WeaponSet(1); 60 this->weaponSystem_->attachWeaponSet(weaponSet1); 61 this->weaponSystem_->getWeaponSetPointer(0)->getWeaponSlotPointer(0)->setAmmoType(true); 62 */ 60 61 this->spawnparticleduration_ = 3.0f; 62 63 this->getPickUp().setPlayer(this); 64 65 if (Core::isMaster()) 66 { 67 this->weaponSystem_ = new WeaponSystem(this); 68 this->weaponSystem_->setParentPawn(this); 69 } 70 else 71 this->weaponSystem_ = 0; 72 73 this->setRadarObjectColour(ColourValue::Red); 74 this->setRadarObjectShape(RadarViewable::Dot); 63 75 64 76 this->registerVariables(); … … 67 79 Pawn::~Pawn() 68 80 { 81 if (this->isInitialized()) 82 { 83 for (ObjectList<PawnListener>::iterator it = ObjectList<PawnListener>::begin(); it != ObjectList<PawnListener>::end(); ++it) 84 it->destroyedPawn(this); 85 86 if (this->weaponSystem_) 87 delete this->weaponSystem_; 88 } 69 89 } 70 90 … … 73 93 SUPER(Pawn, XMLPort, xmlelement, mode); 74 94 75 XMLPortParam(Pawn, "health", setHealth, getHeal ht, xmlelement, mode).defaultValues(100);95 XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); 76 96 XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); 77 97 XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); 98 XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode); 99 XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f); 100 XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7); 101 102 XMLPortObject(Pawn, WeaponSlot, "weaponslots", setWeaponSlot, getWeaponSlot, xmlelement, mode); 103 XMLPortObject(Pawn, WeaponSet, "weaponsets", setWeaponSet, getWeaponSet, xmlelement, mode); 104 XMLPortObject(Pawn, WeaponPack, "weapons", setWeaponPack, getWeaponPack, xmlelement, mode); 78 105 } 79 106 80 107 void Pawn::registerVariables() 81 108 { 82 REGISTERDATA(this->bAlive_, direction::toclient); 83 REGISTERDATA(this->health_, direction::toclient); 109 registerVariable(this->bAlive_, variableDirection::toclient); 110 registerVariable(this->health_, variableDirection::toclient); 111 registerVariable(this->initialHealth_, variableDirection::toclient); 112 registerVariable(this->fire_, variableDirection::toserver); 84 113 } 85 114 … … 87 116 { 88 117 SUPER(Pawn, tick, dt); 118 119 if (this->weaponSystem_) 120 { 121 if (this->fire_ & WeaponMode::fire) 122 this->weaponSystem_->fire(WeaponMode::fire); 123 if (this->fire_ & WeaponMode::altFire) 124 this->weaponSystem_->fire(WeaponMode::altFire); 125 if (this->fire_ & WeaponMode::altFire2) 126 this->weaponSystem_->fire(WeaponMode::altFire2); 127 } 128 this->fire_ = this->firehack_; 129 this->firehack_ = 0x0; 89 130 90 131 if (this->health_ <= 0) … … 119 160 } 120 161 121 void Pawn::spawn ()162 void Pawn::spawneffect() 122 163 { 123 164 // play spawn effect 165 if (this->spawnparticlesource_ != "") 166 { 167 ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); 168 effect->setPosition(this->getPosition()); 169 effect->setOrientation(this->getOrientation()); 170 effect->setDestroyAfterLife(true); 171 effect->setSource(this->spawnparticlesource_); 172 effect->setLifetime(this->spawnparticleduration_); 173 } 124 174 } 125 175 126 176 void Pawn::death() 127 177 { 178 // Set bAlive_ to false and wait for PawnManager to do the destruction 128 179 this->bAlive_ = false; 180 181 this->setDestroyWhenPlayerLeft(false); 182 129 183 if (this->getGametype()) 130 184 this->getGametype()->pawnKilled(this, this->lastHitOriginator_); 185 131 186 if (this->getPlayer()) 132 187 this->getPlayer()->stopControl(this); 133 188 134 delete this; 135 189 if (Core::isMaster()) 190 this->deatheffect(); 191 } 192 193 void Pawn::deatheffect() 194 { 136 195 // play death effect 137 } 138 139 void Pawn::fire() 140 { 141 if (this->weaponSystem_) 142 this->weaponSystem_->fire(); 196 { 197 ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); 198 effect->setPosition(this->getPosition()); 199 effect->setOrientation(this->getOrientation()); 200 effect->setDestroyAfterLife(true); 201 effect->setSource("Orxonox/explosion2b"); 202 effect->setLifetime(4.0f); 203 } 204 { 205 ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); 206 effect->setPosition(this->getPosition()); 207 effect->setOrientation(this->getOrientation()); 208 effect->setDestroyAfterLife(true); 209 effect->setSource("Orxonox/smoke6"); 210 effect->setLifetime(4.0f); 211 } 212 { 213 ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); 214 effect->setPosition(this->getPosition()); 215 effect->setOrientation(this->getOrientation()); 216 effect->setDestroyAfterLife(true); 217 effect->setSource("Orxonox/sparks"); 218 effect->setLifetime(4.0f); 219 } 220 for (unsigned int i = 0; i < this->numexplosionchunks_; ++i) 221 { 222 ExplosionChunk* chunk = new ExplosionChunk(this->getCreator()); 223 chunk->setPosition(this->getPosition()); 224 225 } 226 } 227 228 void Pawn::fire(WeaponMode::Enum fireMode) 229 { 230 this->firehack_ |= fireMode; 143 231 } 144 232 … … 146 234 { 147 235 this->setHealth(this->initialHealth_); 148 this->spawn(); 236 if (Core::isMaster()) 237 this->spawneffect(); 238 } 239 240 void Pawn::dropItems() 241 { 242 pickUp.eraseAll(); 243 } 244 245 void Pawn::setWeaponSlot(WeaponSlot * wSlot) 246 { 247 this->attach(wSlot); 248 if (this->weaponSystem_) 249 this->weaponSystem_->attachWeaponSlot(wSlot); 250 } 251 252 WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const 253 { 254 if (this->weaponSystem_) 255 return this->weaponSystem_->getWeaponSlotPointer(index); 256 else 257 return 0; 258 } 259 260 void Pawn::setWeaponPack(WeaponPack * wPack) 261 { 262 if (this->weaponSystem_) 263 { 264 wPack->setParentWeaponSystem(this->weaponSystem_); 265 wPack->setParentWeaponSystemToAllWeapons(this->weaponSystem_); 266 this->weaponSystem_->attachWeaponPack( wPack,wPack->getFireMode() ); 267 wPack->attachNeededMunitionToAllWeapons(); 268 } 269 } 270 271 WeaponPack * Pawn::getWeaponPack(unsigned int firemode) const 272 { 273 if (this->weaponSystem_) 274 return this->weaponSystem_->getWeaponPackPointer(firemode); 275 else 276 return 0; 277 } 278 279 void Pawn::setWeaponSet(WeaponSet * wSet) 280 { 281 if (this->weaponSystem_) 282 this->weaponSystem_->attachWeaponSet(wSet); 283 } 284 285 WeaponSet * Pawn::getWeaponSet(unsigned int index) const 286 { 287 if (this->weaponSystem_) 288 return this->weaponSystem_->getWeaponSetPointer(index); 289 else 290 return 0; 291 } 292 293 294 /////////////////// 295 // Pawn Listener // 296 /////////////////// 297 PawnListener::PawnListener() 298 { 299 RegisterRootObject(PawnListener); 149 300 } 150 301 } -
code/trunk/src/orxonox/objects/worldentities/pawns/Pawn.h
r2098 r2662 31 31 32 32 #include "OrxonoxPrereqs.h" 33 33 #include "objects/pickup/ShipEquipment.h" 34 34 #include "objects/worldentities/ControllableEntity.h" 35 #include "objects/RadarViewable.h" 36 #include "objects/weaponSystem/WeaponSystem.h" 35 37 36 38 namespace orxonox 37 39 { 38 class _OrxonoxExport Pawn : public ControllableEntity 40 class _OrxonoxExport Pawn : public ControllableEntity, public RadarViewable 39 41 { 40 42 public: … … 54 56 inline void removeHealth(float health) 55 57 { this->setHealth(this->health_ - health); } 56 inline float getHeal ht() const58 inline float getHealth() const 57 59 { return this->health_; } 58 60 … … 74 76 virtual void kill(); 75 77 76 virtual void fire(); 77 78 virtual void fire(WeaponMode::Enum fireMode); 78 79 virtual void postSpawn(); 79 80 81 void setWeaponSlot(WeaponSlot * wSlot); 82 WeaponSlot * getWeaponSlot(unsigned int index) const; 83 void setWeaponPack(WeaponPack * wPack); 84 WeaponPack * getWeaponPack(unsigned int firemode) const; 85 void setWeaponSet(WeaponSet * wSet); 86 WeaponSet * getWeaponSet(unsigned int index) const; 87 88 inline const WorldEntity* getWorldEntity() const 89 { return const_cast<Pawn*>(this); } 90 91 inline void setSpawnParticleSource(const std::string& source) 92 { this->spawnparticlesource_ = source; } 93 inline const std::string& getSpawnParticleSource() const 94 { return this->spawnparticlesource_; } 95 96 inline void setSpawnParticleDuration(float duration) 97 { this->spawnparticleduration_ = duration; } 98 inline float getSpawnParticleDuration() const 99 { return this->spawnparticleduration_; } 100 101 inline void setExplosionChunks(unsigned int chunks) 102 { this->numexplosionchunks_ = chunks; } 103 inline unsigned int getExplosionChunks() const 104 { return this->numexplosionchunks_; } 105 106 inline ShipEquipment& getPickUp() 107 {return this->pickUp;} 108 109 virtual void dropItems(); 110 80 111 protected: 81 virtual void spawn();82 112 virtual void death(); 113 virtual void deatheffect(); 114 virtual void spawneffect(); 83 115 116 ShipEquipment pickUp; 84 117 bool bAlive_; 118 85 119 86 120 float health_; … … 91 125 92 126 WeaponSystem* weaponSystem_; 127 unsigned int fire_; 128 unsigned int firehack_; 129 130 std::string spawnparticlesource_; 131 float spawnparticleduration_; 132 unsigned int numexplosionchunks_; 133 }; 134 135 class _OrxonoxExport PawnListener : virtual public OrxonoxClass 136 { 137 friend class Pawn; 138 139 public: 140 PawnListener(); 141 virtual ~PawnListener() {} 142 143 protected: 144 virtual void destroyedPawn(Pawn* pawn) = 0; 93 145 }; 94 146 } -
code/trunk/src/orxonox/objects/worldentities/pawns/SpaceShip.cc
r2171 r2662 30 30 #include "SpaceShip.h" 31 31 32 #include "BulletDynamics/Dynamics/btRigidBody.h" 33 34 #include "util/Math.h" 35 #include "util/Exception.h" 32 36 #include "core/CoreIncludes.h" 33 37 #include "core/ConfigValueIncludes.h" 38 #include "core/Template.h" 34 39 #include "core/XMLPort.h" 35 #include " util/Math.h"40 #include "objects/items/Engine.h" 36 41 37 42 namespace orxonox 38 43 { 44 const float orientationGain = 100; 39 45 CreateFactory(SpaceShip); 40 46 … … 43 49 RegisterObject(SpaceShip); 44 50 45 this->zeroDegree_ = 0; 46 47 this->maxSpeed_ = 0; 48 this->maxSecondarySpeed_ = 0; 49 this->maxRotation_ = 0; 50 this->translationAcceleration_ = 0; 51 this->rotationAcceleration_ = 0; 52 this->translationDamping_ = 0; 53 54 this->yawRotation_ = 0; 55 this->pitchRotation_ = 0; 56 this->rollRotation_ = 0; 51 this->primaryThrust_ = 100; 52 this->auxilaryThrust_ = 30; 53 this->rotationThrust_ = 10; 54 55 this->localLinearAcceleration_.setValue(0, 0, 0); 56 this->localAngularAcceleration_.setValue(0, 0, 0); 57 this->bBoost_ = false; 58 this->bPermanentBoost_ = false; 59 this->steering_ = Vector3::ZERO; 60 this->engine_ = 0; 61 57 62 58 63 this->bInvertYAxis_ = false; 59 64 60 65 this->setDestroyWhenPlayerLeft(true); 66 67 // SpaceShip is always a physical object per default 68 // Be aware of this call: The collision type legality check will not reach derived classes! 69 this->setCollisionType(WorldEntity::Dynamic); 70 // Get notification about collisions 71 this->enableCollisionCallback(); 61 72 62 73 this->setConfigValues(); … … 66 77 SpaceShip::~SpaceShip() 67 78 { 79 if (this->isInitialized() && this->engine_) 80 delete this->engine_; 68 81 } 69 82 … … 72 85 SUPER(SpaceShip, XMLPort, xmlelement, mode); 73 86 74 XMLPortParam(SpaceShip, "maxspeed", setMaxSpeed, getMaxSpeed, xmlelement, mode); 75 XMLPortParam(SpaceShip, "maxsecondaryspeed", setMaxSecondarySpeed, getMaxSecondarySpeed, xmlelement, mode); 76 XMLPortParam(SpaceShip, "maxrotation", setMaxRotation, getMaxRotation, xmlelement, mode); 77 XMLPortParam(SpaceShip, "transacc", setTransAcc, getTransAcc, xmlelement, mode); 78 XMLPortParam(SpaceShip, "rotacc", setRotAcc, getRotAcc, xmlelement, mode); 79 XMLPortParam(SpaceShip, "transdamp", setTransDamp, getTransDamp, xmlelement, mode); 87 XMLPortParam(SpaceShip, "engine", setEngineTemplate, getEngineTemplate, xmlelement, mode); 88 XMLPortParamVariable(SpaceShip, "primaryThrust", primaryThrust_, xmlelement, mode); 89 XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode); 90 XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode); 80 91 } 81 92 82 93 void SpaceShip::registerVariables() 83 94 { 84 REGISTERDATA(this->maxSpeed_, direction::toclient); 85 REGISTERDATA(this->maxSecondarySpeed_, direction::toclient); 86 REGISTERDATA(this->maxRotation_, direction::toclient); 87 REGISTERDATA(this->translationAcceleration_, direction::toclient); 88 REGISTERDATA(this->rotationAcceleration_, direction::toclient); 89 REGISTERDATA(this->translationDamping_, direction::toclient); 95 registerVariable(this->primaryThrust_, variableDirection::toclient); 96 registerVariable(this->auxilaryThrust_, variableDirection::toclient); 97 registerVariable(this->rotationThrust_, variableDirection::toclient); 90 98 } 91 99 … … 95 103 } 96 104 105 bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const 106 { 107 if (type != WorldEntity::Dynamic) 108 { 109 CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl; 110 assert(false); // Only in debug mode 111 return false; 112 } 113 else 114 return true; 115 } 116 97 117 void SpaceShip::tick(float dt) 98 118 { 99 if (this->isLocallyControlled()) 119 SUPER(SpaceShip, tick, dt); 120 121 if (this->hasLocalController()) 100 122 { 101 // ##################################### 102 // ############# STEERING ############## 103 // ##################################### 104 105 Vector3 velocity = this->getVelocity(); 106 if (velocity.x > this->maxSecondarySpeed_) 107 velocity.x = this->maxSecondarySpeed_; 108 if (velocity.x < -this->maxSecondarySpeed_) 109 velocity.x = -this->maxSecondarySpeed_; 110 if (velocity.y > this->maxSecondarySpeed_) 111 velocity.y = this->maxSecondarySpeed_; 112 if (velocity.y < -this->maxSecondarySpeed_) 113 velocity.y = -this->maxSecondarySpeed_; 114 if (velocity.z > this->maxSecondarySpeed_) 115 velocity.z = this->maxSecondarySpeed_; 116 if (velocity.z < -this->maxSpeed_) 117 velocity.z = -this->maxSpeed_; 118 119 // normalize velocity and acceleration 120 for (size_t dimension = 0; dimension < 3; ++dimension) 123 /* 124 this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_); 125 this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_); 126 if (this->localLinearAcceleration_.z() > 0) 127 this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_); 128 else 129 this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_); 130 this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_); 131 this->localLinearAcceleration_.setValue(0, 0, 0); 132 */ 133 if (!this->isInMouseLook()) 121 134 { 122 if (this->acceleration_[dimension] == 0) 135 this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; 136 this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); 137 this->localAngularAcceleration_.setValue(0, 0, 0); 138 } 139 } 140 } 141 142 void SpaceShip::moveFrontBack(const Vector2& value) 143 { 144 this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); 145 this->steering_.z = -value.x; 146 } 147 148 void SpaceShip::moveRightLeft(const Vector2& value) 149 { 150 this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); 151 this->steering_.x = value.x; 152 } 153 154 void SpaceShip::moveUpDown(const Vector2& value) 155 { 156 this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); 157 this->steering_.y = value.x; 158 } 159 160 void SpaceShip::rotateYaw(const Vector2& value) 161 { 162 this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() - value.x); 163 164 Pawn::rotateYaw(value); 165 } 166 167 void SpaceShip::rotatePitch(const Vector2& value) 168 { 169 this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x); 170 171 Pawn::rotatePitch(value); 172 } 173 174 void SpaceShip::rotateRoll(const Vector2& value) 175 { 176 this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); 177 178 Pawn::rotateRoll(value); 179 } 180 181 void SpaceShip::fire() 182 { 183 } 184 185 void SpaceShip::boost() 186 { 187 this->bBoost_ = true; 188 } 189 190 void SpaceShip::loadEngineTemplate() 191 { 192 if (this->enginetemplate_ != "") 193 { 194 Template* temp = Template::getTemplate(this->enginetemplate_); 195 196 if (temp) 197 { 198 Identifier* identifier = temp->getBaseclassIdentifier(); 199 200 if (identifier) 123 201 { 124 if (velocity[dimension] > 0) 202 BaseObject* object = identifier->fabricate(this); 203 this->engine_ = dynamic_cast<Engine*>(object); 204 205 if (this->engine_) 125 206 { 126 velocity[dimension] -= (this->translationDamping_ * dt); 127 if (velocity[dimension] < 0) 128 velocity[dimension] = 0; 207 this->engine_->addTemplate(temp); 208 this->engine_->addToSpaceShip(this); 129 209 } 130 else if (velocity[dimension] < 0)210 else 131 211 { 132 velocity[dimension] += (this->translationDamping_ * dt); 133 if (velocity[dimension] > 0) 134 velocity[dimension] = 0; 212 delete object; 135 213 } 136 214 } 137 215 } 138 139 this->setVelocity(velocity);140 216 } 141 142 143 SUPER(SpaceShip, tick, dt); 144 145 146 if (this->isLocallyControlled()) 147 { 148 this->yaw(this->yawRotation_ * dt); 149 if (this->bInvertYAxis_) 150 this->pitch(Degree(-this->pitchRotation_ * dt)); 151 else 152 this->pitch(Degree( this->pitchRotation_ * dt)); 153 this->roll(this->rollRotation_ * dt); 154 155 this->acceleration_.x = 0; 156 this->acceleration_.y = 0; 157 this->acceleration_.z = 0; 158 159 this->yawRotation_ = this->zeroDegree_; 160 this->pitchRotation_ = this->zeroDegree_; 161 this->rollRotation_ = this->zeroDegree_; 162 } 163 } 164 165 void SpaceShip::moveFrontBack(const Vector2& value) 166 { 167 this->acceleration_.z = -this->translationAcceleration_ * value.x; 168 } 169 170 void SpaceShip::moveRightLeft(const Vector2& value) 171 { 172 this->acceleration_.x = this->translationAcceleration_ * value.x; 173 } 174 175 void SpaceShip::moveUpDown(const Vector2& value) 176 { 177 this->acceleration_.y = this->translationAcceleration_ * value.x; 178 } 179 180 void SpaceShip::rotateYaw(const Vector2& value) 181 { 182 Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_; 183 if (temp > this->maxRotation_) 184 temp = this->maxRotation_; 185 if (temp < -this->maxRotation_) 186 temp = -this->maxRotation_; 187 this->yawRotation_ = Degree(temp); 188 } 189 190 void SpaceShip::rotatePitch(const Vector2& value) 191 { 192 Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_; 193 if (temp > this->maxRotation_) 194 temp = this->maxRotation_; 195 if (temp < -this->maxRotation_) 196 temp = -this->maxRotation_; 197 this->pitchRotation_ = Degree(temp); 198 } 199 200 void SpaceShip::rotateRoll(const Vector2& value) 201 { 202 Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_; 203 if (temp > this->maxRotation_) 204 temp = this->maxRotation_; 205 if (temp < -this->maxRotation_) 206 temp = -this->maxRotation_; 207 this->rollRotation_ = Degree(temp); 208 } 209 210 void SpaceShip::fire() 211 { 217 } 218 219 void SpaceShip::setEngine(Engine* engine) 220 { 221 this->engine_ = engine; 222 if (engine && engine->getShip() != this) 223 engine->addToSpaceShip(this); 212 224 } 213 225 } -
code/trunk/src/orxonox/objects/worldentities/pawns/SpaceShip.h
r2087 r2662 32 32 #include "OrxonoxPrereqs.h" 33 33 34 #include "LinearMath/btVector3.h" 35 34 36 #include "Pawn.h" 35 37 … … 56 58 57 59 virtual void fire(); 60 virtual void boost(); 58 61 59 void setMaxSpeed(float value) 60 { this->maxSpeed_ = value; } 61 void setMaxSecondarySpeed(float value) 62 { this->maxSecondarySpeed_ = value; } 63 void setMaxRotation(const Degree& value) 64 { this->maxRotation_ = value; } 65 void setTransAcc(float value) 66 { this->translationAcceleration_ = value; } 67 void setRotAcc(const Degree& value) 68 { this->rotationAcceleration_ = value; } 69 void setTransDamp(float value) 70 { this->translationDamping_ = value; } 62 void setEngine(Engine* engine); 63 inline Engine* getEngine() const 64 { return this->engine_; } 71 65 72 inline float getMaxSpeed() const 73 { return this->maxSpeed_; } 74 inline float getMaxSecondarySpeed() const 75 { return this->maxSecondarySpeed_; } 76 inline float getMaxRotation() const 77 { return this->maxRotation_.valueDegrees(); } 78 inline float getTransAcc() const 79 { return this->translationAcceleration_; } 80 inline float getRotAcc() const 81 { return this->rotationAcceleration_.valueDegrees(); } 82 inline float getTransDamp() const 83 { return this->translationDamping_; } 66 inline void setSteeringDirection(const Vector3& direction) 67 { this->steering_ = direction; } 68 inline const Vector3& getSteeringDirection() const 69 { return this->steering_; } 70 71 inline void setBoost(bool bBoost) 72 { this->bBoost_ = bBoost; } 73 inline bool getBoost() const 74 { return this->bBoost_; } 75 76 inline void setEngineTemplate(const std::string& temp) 77 { this->enginetemplate_ = temp; this->loadEngineTemplate(); } 78 inline const std::string& getEngineTemplate() const 79 { return this->enginetemplate_; } 80 81 inline void setPermanentBoost(bool bPermanent) 82 { this->bPermanentBoost_ = bPermanent; } 83 inline bool getPermanentBoost() const 84 { return this->bPermanentBoost_; } 84 85 85 86 protected: 86 87 bool bInvertYAxis_; 87 88 88 float maxSpeed_; 89 float maxSecondarySpeed_; 90 float translationAcceleration_; 91 float translationDamping_; 89 bool bBoost_; 90 bool bPermanentBoost_; 91 Vector3 steering_; 92 float primaryThrust_; 93 float auxilaryThrust_; 94 float rotationThrust_; 95 btVector3 localLinearAcceleration_; 96 btVector3 localAngularAcceleration_; 92 97 93 Degree maxRotation_;94 Degree rotationAcceleration_;98 private: 99 virtual bool isCollisionTypeLegal(WorldEntity::CollisionType type) const; 95 100 96 Degree zeroDegree_; 97 Degree pitchRotation_; 98 Degree yawRotation_; 99 Degree rollRotation_; 101 private: 102 void loadEngineTemplate(); 103 104 std::string enginetemplate_; 105 Engine* engine_; 100 106 }; 101 107 } -
code/trunk/src/orxonox/objects/worldentities/pawns/Spectator.cc
r2171 r2662 30 30 #include "Spectator.h" 31 31 32 #include <OgreBillboardSet.h> 33 32 34 #include "core/CoreIncludes.h" 35 #include "core/ConfigValueIncludes.h" 33 36 #include "core/Core.h" 34 37 #include "objects/worldentities/Model.h" … … 49 52 RegisterObject(Spectator); 50 53 51 this->speed_ = 100; 52 this->rotationSpeed_ = 3; 54 this->speed_ = 200; 53 55 54 56 this->yaw_ = 0; 55 57 this->pitch_ = 0; 56 58 this->roll_ = 0; 59 this->localVelocity_ = Vector3::ZERO; 57 60 this->setHudTemplate("spectatorhud"); 58 this-> hudmode_ = 0;61 this->greetingFlare_ = 0; 59 62 60 63 this->setDestroyWhenPlayerLeft(true); 61 64 62 this->greetingFlare_ = new BillboardSet(); 63 this->greetingFlare_->setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 1.0, 0.8), Vector3(0, 20, 0), 1); 64 if (this->greetingFlare_->getBillboardSet()) 65 this->getNode()->attachObject(this->greetingFlare_->getBillboardSet()); 66 this->greetingFlare_->setVisible(false); 65 if (Core::showsGraphics()) 66 { 67 this->greetingFlare_ = new BillboardSet(); 68 this->greetingFlare_->setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 1.0, 0.8), Vector3(0, 20, 0), 1); 69 if (this->greetingFlare_->getBillboardSet()) 70 this->attachOgreObject(this->greetingFlare_->getBillboardSet()); 71 this->greetingFlare_->setVisible(false); 72 } 73 67 74 this->bGreetingFlareVisible_ = false; 68 75 this->bGreeting_ = false; 69 76 77 this->setConfigValues(); 70 78 this->registerVariables(); 71 79 } … … 78 86 { 79 87 if (this->greetingFlare_->getBillboardSet()) 80 this->getNode()->detachObject(this->greetingFlare_->getBillboardSet()); 88 this->detachOgreObject(this->greetingFlare_->getBillboardSet()); 89 81 90 delete this->greetingFlare_; 82 91 } … … 84 93 } 85 94 95 void Spectator::setConfigValues() 96 { 97 SetConfigValue(speed_, 200.0f); 98 } 99 86 100 void Spectator::registerVariables() 87 101 { 88 REGISTERDATA(this->bGreetingFlareVisible_, direction::toclient, new NetworkCallback<Spectator>(this, &Spectator::changedFlareVisibility)); 89 REGISTERDATA(this->bGreeting_, direction::toserver, new NetworkCallback<Spectator>(this, &Spectator::changedGreeting)); 90 REGISTERDATA(this->hudmode_, direction::toclient); 102 registerVariable(this->bGreetingFlareVisible_, variableDirection::toclient, new NetworkCallback<Spectator>(this, &Spectator::changedFlareVisibility)); 103 registerVariable(this->bGreeting_, variableDirection::toserver, new NetworkCallback<Spectator>(this, &Spectator::changedGreeting)); 91 104 } 92 105 … … 99 112 void Spectator::changedFlareVisibility() 100 113 { 101 this->greetingFlare_->setVisible(this->bGreetingFlareVisible_); 114 if ( this->greetingFlare_ ) 115 this->greetingFlare_->setVisible(this->bGreetingFlareVisible_); 102 116 } 103 117 104 118 void Spectator::tick(float dt) 105 119 { 106 this->updateHUD(); 107 108 if (this->isLocallyControlled()) 109 { 110 Vector3 velocity = this->getVelocity(); 111 velocity.normalise(); 112 this->setVelocity(velocity * this->speed_); 113 114 this->yaw(Radian(this->yaw_ * this->rotationSpeed_)); 115 this->pitch(Radian(this->pitch_ * this->rotationSpeed_)); 116 this->roll(Radian(this->roll_ * this->rotationSpeed_)); 120 if (this->hasLocalController()) 121 { 122 float localSpeedSquared = this->localVelocity_.squaredLength(); 123 float localSpeed; 124 if (localSpeedSquared > 1.0) 125 localSpeed = this->speed_ / sqrtf(localSpeedSquared); 126 else 127 localSpeed = this->speed_; 128 129 this->localVelocity_.x *= localSpeed; 130 this->localVelocity_.y *= localSpeed; 131 this->localVelocity_.z *= localSpeed; 132 this->setVelocity(this->getOrientation() * this->localVelocity_); 133 this->localVelocity_.x = 0; 134 this->localVelocity_.y = 0; 135 this->localVelocity_.z = 0; 136 137 if (!this->isInMouseLook()) 138 { 139 this->yaw(Radian(this->yaw_ * this->getMouseLookSpeed())); 140 this->pitch(Radian(this->pitch_ * this->getMouseLookSpeed())); 141 this->roll(Radian(this->roll_ * this->getMouseLookSpeed())); 142 } 117 143 118 144 this->yaw_ = this->pitch_ = this->roll_ = 0; … … 120 146 121 147 SUPER(Spectator, tick, dt); 122 123 if (this->isLocallyControlled())124 {125 this->setVelocity(Vector3::ZERO);126 }127 148 } 128 149 … … 131 152 ControllableEntity::setPlayer(player); 132 153 133 // this->setObjectMode(direction::toclient); 134 } 135 136 void Spectator::startLocalControl() 137 { 138 ControllableEntity::startLocalControl(); 139 // if (this->isLocallyControlled()) 140 // this->testmesh_->setVisible(false); 154 // this->setObjectMode(objectDirection::toclient); 155 } 156 157 void Spectator::startLocalHumanControl() 158 { 159 ControllableEntity::startLocalHumanControl(); 141 160 } 142 161 143 162 void Spectator::moveFrontBack(const Vector2& value) 144 163 { 145 this-> setVelocity(this->getVelocity() + value.y * this->speed_ * WorldEntity::FRONT);164 this->localVelocity_.z -= value.x; 146 165 } 147 166 148 167 void Spectator::moveRightLeft(const Vector2& value) 149 168 { 150 this-> setVelocity(this->getVelocity() + value.y * this->speed_ * WorldEntity::RIGHT);169 this->localVelocity_.x += value.x; 151 170 } 152 171 153 172 void Spectator::moveUpDown(const Vector2& value) 154 173 { 155 this-> setVelocity(this->getVelocity() + value.y * this->speed_ * WorldEntity::UP);174 this->localVelocity_.y += value.x; 156 175 } 157 176 158 177 void Spectator::rotateYaw(const Vector2& value) 159 178 { 160 this->yaw_ = value.y; 179 this->yaw_ -= value.y; 180 181 ControllableEntity::rotateYaw(value); 161 182 } 162 183 163 184 void Spectator::rotatePitch(const Vector2& value) 164 185 { 165 this->pitch_ = value.y; 186 this->pitch_ += value.y; 187 188 ControllableEntity::rotatePitch(value); 166 189 } 167 190 168 191 void Spectator::rotateRoll(const Vector2& value) 169 192 { 170 this->roll_ = value.y; 171 } 172 173 void Spectator::fire() 193 this->roll_ += value.y; 194 195 ControllableEntity::rotateRoll(value); 196 } 197 198 void Spectator::fire(WeaponMode::Enum fireMode) 174 199 { 175 200 if (this->getPlayer()) … … 187 212 } 188 213 } 189 190 void Spectator::updateHUD()191 {192 // <hack>193 if (Core::isMaster())194 {195 if (this->getPlayer() && this->getGametype())196 {197 if (!this->getGametype()->hasStarted() && !this->getGametype()->isStartCountdownRunning())198 {199 if (!this->getPlayer()->isReadyToSpawn())200 this->hudmode_ = 0;201 else202 this->hudmode_ = 1;203 }204 else if (!this->getGametype()->hasEnded())205 {206 if (this->getGametype()->isStartCountdownRunning())207 this->hudmode_ = 2 + 10*(int)ceil(this->getGametype()->getStartCountdown());208 else209 this->hudmode_ = 3;210 }211 else212 this->hudmode_ = 4;213 }214 else215 return;216 }217 218 if (this->getHUD())219 {220 std::string text;221 int hudmode = this->hudmode_ % 10;222 223 switch (hudmode)224 {225 case 0:226 text = "Press [Fire] to start the match";227 break;228 case 1:229 text = "Waiting for other players";230 break;231 case 2:232 text = convertToString((this->hudmode_ - 2) / 10);233 break;234 case 3:235 text = "Press [Fire] to respawn";236 break;237 case 4:238 text = "Game has ended";239 break;240 default:;241 }242 243 std::map<std::string, OrxonoxOverlay*>::const_iterator it = this->getHUD()->getOverlays().begin();244 for (; it != this->getHUD()->getOverlays().end(); ++it)245 {246 if (it->second->isA(Class(OverlayText)) && it->second->getName() == "state")247 {248 OverlayText* overlay = dynamic_cast<OverlayText*>(it->second);249 if (overlay)250 overlay->setCaption(text);251 break;252 }253 }254 }255 // </hack>256 }257 214 } -
code/trunk/src/orxonox/objects/worldentities/pawns/Spectator.h
r2087 r2662 42 42 virtual ~Spectator(); 43 43 44 void setConfigValues(); 44 45 void registerVariables(); 45 46 virtual void tick(float dt); 46 47 47 48 virtual void setPlayer(PlayerInfo* player); 48 virtual void startLocal Control();49 virtual void startLocalHumanControl(); 49 50 50 51 virtual void moveFrontBack(const Vector2& value); … … 56 57 virtual void rotateRoll(const Vector2& value); 57 58 58 virtual void fire( );59 virtual void fire(WeaponMode::Enum fireMode); 59 60 virtual void greet(); 60 61 … … 62 63 void changedGreeting(); 63 64 void changedFlareVisibility(); 64 void updateHUD();65 65 66 66 BillboardSet* greetingFlare_; … … 69 69 70 70 float speed_; 71 float rotationSpeed_;72 71 73 72 float yaw_; … … 75 74 float roll_; 76 75 77 int hudmode_;76 Vector3 localVelocity_; 78 77 }; 79 78 } -
code/trunk/src/orxonox/objects/worldentities/triggers/DistanceTrigger.cc
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/objects/worldentities/triggers/DistanceTrigger.h
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/objects/worldentities/triggers/PlayerTrigger.cc
r2262 r2662 28 28 29 29 /** 30 @file PlayerTrigger.cc30 @file 31 31 @brief 32 32 Implementation of the PlayerTrigger class. 33 33 */ 34 34 … … 38 38 #include "core/CoreIncludes.h" 39 39 40 namespace orxonox {41 40 namespace orxonox 41 { 42 42 /** 43 43 @brief … … 47 47 { 48 48 RegisterObject(PlayerTrigger); 49 49 50 50 this->player_ = NULL; 51 51 this->isForPlayer_ = true; 52 52 } 53 53 54 54 /** 55 55 @brief … … 66 66 void PlayerTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) 67 67 { 68 68 SUPER(PlayerTrigger, XMLPort, xmlelement, mode); 69 69 } 70 71 70 } -
code/trunk/src/orxonox/objects/worldentities/triggers/PlayerTrigger.h
r2262 r2662 28 28 29 29 /** 30 @file PlayerTrigger.h30 @file 31 31 @brief 32 32 Definition of the PlayerTrigger class. 33 33 */ 34 34 … … 40 40 #include "Trigger.h" 41 41 42 namespace orxonox {43 42 namespace orxonox 43 { 44 44 /** 45 45 @brief … … 50 50 class _OrxonoxExport PlayerTrigger : public Trigger 51 51 { 52 public: 53 PlayerTrigger(BaseObject* creator); 54 virtual ~PlayerTrigger(); 55 56 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a PlayerTrigger object through XML. 52 public: 53 PlayerTrigger(BaseObject* creator); 54 virtual ~PlayerTrigger(); 55 56 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a PlayerTrigger object through XML. 57 58 /** 59 @brief Returns the player that triggered the PlayerTrigger. 60 @return Returns a pointer to the ControllableEntity that triggered the PlayerTrigger. 61 */ 62 inline ControllableEntity* getTriggeringPlayer(void) const 63 { return this->player_; } 64 65 /** 66 @brief Checks whether the PlayerTrigger normally returns a ControllableEntity. 67 @return Returns true if the PlayerTrigger normally returns a ControllableEntity. 68 */ 69 inline bool isForPlayer(void) const 70 { return this->isForPlayer_; } 57 71 58 /** 59 @brief Returns the player that triggered the PlayerTrigger. 60 @return Returns a pointer to the ControllableEntity that triggered the PlayerTrigger. 61 */ 62 inline ControllableEntity* getTriggeringPlayer(void) const 63 { return this->player_; } 64 65 /** 66 @brief Checks whether the PlayerTrigger normally returns a ControllableEntity. 67 @return Returns true if the PlayerTrigger normally returns a ControllableEntity. 68 */ 69 inline bool isForPlayer(void) const 70 { return this->isForPlayer_; } 71 72 protected: 73 virtual bool isTriggered(TriggerMode mode) = 0; 74 75 /** 76 @brief Set the player that triggered the PlayerTrigger. This is normally done by classes inheriting vom PlayerTrigger. 77 @param player A pointer to the ControllableEntity that triggered the PlayerTrigger. 78 */ 79 inline void setTriggeringPlayer(ControllableEntity* player) 80 { this->player_ = player; } 72 protected: 73 virtual bool isTriggered(TriggerMode mode) = 0; 74 75 /** 76 @brief Set the player that triggered the PlayerTrigger. This is normally done by classes inheriting vom PlayerTrigger. 77 @param player A pointer to the ControllableEntity that triggered the PlayerTrigger. 78 */ 79 inline void setTriggeringPlayer(ControllableEntity* player) 80 { this->player_ = player; } 81 81 82 82 /** … … 84 84 @param isForPlayer Should be true when the PlayerTrigger schould be set to normally be triggered by ControllableEntities, false if not. 85 85 */ 86 87 88 89 90 91 92 86 inline void setForPlayer(bool isForPlayer) 87 { this->isForPlayer_ = isForPlayer; } 88 89 private: 90 ControllableEntity* player_; //!< The player that triggered the PlayerTrigger. 91 bool isForPlayer_; //!< Is true when the PlayerTrigger schould be set to normally be triggered by ControllableEntities. 92 93 93 }; 94 94 -
code/trunk/src/orxonox/objects/worldentities/triggers/Trigger.cc
- Property svn:mergeinfo changed
r2261 r2662 31 31 32 32 #include <OgreBillboard.h> 33 #include <OgreBillboardSet.h> 33 34 #include "util/Debug.h" 34 35 #include "core/CoreIncludes.h" 35 36 #include "core/ConsoleCommand.h" 36 37 #include "core/XMLPort.h" 38 #include "core/Core.h" 37 39 #include "objects/Scene.h" 38 40 … … 44 46 CreateFactory(Trigger); 45 47 46 Trigger::Trigger(BaseObject* creator) : PositionableEntity(creator)48 Trigger::Trigger(BaseObject* creator) : StaticEntity(creator) 47 49 { 48 50 RegisterObject(Trigger); … … 65 67 // this->bUpdating_ = false; 66 68 67 if (this->getScene() && this->getScene()->getSceneManager())69 if (this->getScene() && Core::showsGraphics()) 68 70 { 69 71 this->debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); … … 71 73 72 74 if (this->debugBillboard_.getBillboardSet()) 73 this->getNode()->attachObject(this->debugBillboard_.getBillboardSet());75 this->attachOgreObject(this->debugBillboard_.getBillboardSet()); 74 76 } 75 77 … … 106 108 if (!this->BaseObject::isActive()) 107 109 return; 110 111 SUPER(Trigger, tick, dt); 108 112 109 113 bool newTriggered = this->isTriggered() ^ this->bInvertMode_; -
code/trunk/src/orxonox/objects/worldentities/triggers/Trigger.h
- Property svn:mergeinfo changed
r2261 r2662 36 36 37 37 #include "objects/Tickable.h" 38 #include "objects/worldentities/ PositionableEntity.h"38 #include "objects/worldentities/StaticEntity.h" 39 39 #include "tools/BillboardSet.h" 40 40 … … 48 48 }; 49 49 50 class _OrxonoxExport Trigger : public PositionableEntity, public Tickable50 class _OrxonoxExport Trigger : public StaticEntity, public Tickable 51 51 { 52 52 public:
Note: See TracChangeset
for help on using the changeset viewer.