Changeset 2306 for code/branches/physics/src/orxonox/objects/worldentities
- Timestamp:
- Dec 2, 2008, 4:59:36 PM (16 years ago)
- Location:
- code/branches/physics/src/orxonox/objects/worldentities
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/physics/src/orxonox/objects/worldentities/MovableEntity.cc
r2304 r2306 32 32 #include "BulletDynamics/Dynamics/btRigidBody.h" 33 33 34 #include "util/Debug.h" 34 35 #include "util/Exception.h" 35 36 #include "core/CoreIncludes.h" … … 220 221 this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z())); 221 222 const btVector3& velocity = this->physicalBody_->getLinearVelocity(); 222 this->velocity_ = Vector3(velocity.x(), velocity.y(), velocity.z()); 223 this->velocity_.x = velocity.x(); 224 this->velocity_.y = velocity.y(); 225 this->velocity_.z = velocity.z(); 223 226 velocityChanged(); 224 227 positionChanged(); -
code/branches/physics/src/orxonox/objects/worldentities/WorldEntity.cc
r2304 r2306 75 75 this->collisionShape_ = 0; 76 76 this->mass_ = 0; 77 updateCollisionType(); 77 this->childMass_ = 0; 78 this->collisionType_ = None; 78 79 79 80 this->registerVariables(); … … 151 152 { 152 153 // static to static/kinematic/dynamic --> merge shapes 154 this->childMass_ += object->getMass(); 153 155 this->attachCollisionShape(object->getCollisionShape()); 154 156 // Remove the btRigidBody from the child object. … … 195 197 } 196 198 197 void WorldEntity::mergeCollisionShape(CollisionShape* shape)198 {199 if (!this->collisionShape_)200 this->collisionShape_ = new CompoundCollisionShape(this);201 assert(this->collisionShape_->isCompoundShape());202 203 // merge with transform204 CompoundCollisionShape* compoundShape = static_cast<CompoundCollisionShape*>(this->collisionShape_);205 assert(compoundShape);206 compoundShape->addChildShape(shape);207 }208 209 199 void WorldEntity::attachCollisionShape(CollisionShape* shape) 210 200 { … … 213 203 if (!this->collisionShape_ && shape->hasNoTransform()) 214 204 { 215 // Simply add the shape as is. 205 // Set local scaling right when adding it. It can include both scaling parameters 206 // and shape parameters (like sphere radius) 216 207 shape->getCollisionShape()->setLocalScaling(shape->getTotalScaling()); 217 208 this->collisionShape_ = shape; 209 // recalculate inertia tensor 210 if (this->isDynamic()) 211 { 212 internalSetMassProps(); 213 } 218 214 } 219 215 else … … 246 242 } 247 243 248 //BlinkingBillboard* WorldEntity::getAttachedAsdfObject(unsigned int index) const 249 //{ 250 // return 0; 251 //} 244 void WorldEntity::mergeCollisionShape(CollisionShape* shape) 245 { 246 if (!this->collisionShape_) 247 this->collisionShape_ = new CompoundCollisionShape(this); 248 assert(this->collisionShape_->isCompoundShape()); 249 250 // merge with transform 251 CompoundCollisionShape* compoundShape = static_cast<CompoundCollisionShape*>(this->collisionShape_); 252 assert(compoundShape); 253 compoundShape->addChildShape(shape); 254 255 // recalculate inertia tensor 256 internalSetMassProps(); 257 } 252 258 253 259 void WorldEntity::setScale3D(const Vector3& scale) … … 270 276 { 271 277 this->mass_ = mass; 272 if (! hasPhysics())273 COUT( 2) << "Warning: Setting the mass of a WorldEntity with no physics has no effect." << std::endl;278 if (!this->hasPhysics()) 279 COUT(3) << "Warning: Setting the mass of a WorldEntity with no physics has no effect." << std::endl; 274 280 else if (this->physicalBody_->isInWorld()) 275 COUT(2) << "Warning: Cannot set the physical mass at run time. Storing temporarily." << std::endl; 276 else 277 { 278 if (this->collisionType_ != Dynamic) 279 COUT(2) << "Warning: Cannot set the physical mass of a static or kinematic object. Storing temporarily." << std::endl; 280 else if (mass == 0.0f) 281 COUT(2) << "Warning: Cannot set physical mass of a dynamic object to zero. Storing temporarily." << std::endl; 282 else 283 this->physicalBody_->setMassProps(mass, btVector3(0,0,0)); 284 } 281 COUT(2) << "Warning: Cannot set the physical mass at run time. Storing new mass." << std::endl; 282 else 283 internalSetMassProps(); 284 } 285 286 void WorldEntity::internalSetMassProps() 287 { 288 assert(hasPhysics()); 289 290 if ((this->isKinematic() || this->isStatic()) && (this->mass_ + this->childMass_) != 0.0f) 291 { 292 // Mass non zero is a bad idea for kinematic and static objects 293 this->physicalBody_->setMassProps(0.0f, btVector3(0, 0, 0)); 294 } 295 else if (this->isDynamic() && (this->mass_ + this->childMass_) == 0.0f) 296 { 297 // Mass zero is not such a good idea for dynamic objects 298 this->physicalBody_->setMassProps(1.0f, this->getLocalInertia(1.0f)); 299 } 300 else 301 this->physicalBody_->setMassProps(this->mass_, this->getLocalInertia(this->mass_ + this->childMass_)); 302 } 303 304 btVector3 WorldEntity::getLocalInertia(btScalar mass) const 305 { 306 btVector3 inertia(0, 0, 0); 307 if (this->collisionShape_) 308 this->collisionShape_->getCollisionShape()->calculateLocalInertia(mass, inertia); 309 return inertia; 285 310 } 286 311 … … 300 325 if (type != None && this->collisionType_ == None) 301 326 { 327 // Check whether there was some scaling applied. 328 if (!this->node_->getScale().positionEquals(Vector3(1, 1, 1), 0.001)) 329 ThrowException(NotImplemented, "Cannot create a physical body if there is scaling applied to the node: Not yet implemented."); 330 302 331 // Create new rigid body 303 332 btCollisionShape* temp = 0; … … 337 366 // update our variable for faster checks 338 367 updateCollisionType(); 339 340 // Mass non zero is a bad idea for kinematic and static objects 341 if ((type == Kinematic || type == Static) && this->mass_ != 0.0f) 342 this->setMass(0.0f); 343 // Mass zero is not such a good idea for dynamic objects 344 else if (type == Dynamic && this->mass_ == 0.0f) 345 this->setMass(1.0f); 346 else if (hasPhysics()) 347 this->physicalBody_->setMassProps(this->mass_, btVector3(0,0,0)); 348 } 349 350 void WorldEntity::updateCollisionType() 351 { 352 if (!this->physicalBody_) 353 this->collisionType_ = None; 354 else if (this->physicalBody_->isKinematicObject()) 355 this->collisionType_ = Kinematic; 356 else if (this->physicalBody_->isStaticObject()) 357 this->collisionType_ = Static; 358 else 359 this->collisionType_ = Dynamic; 368 assert(this->collisionType_ == type); 369 370 // update mass and inertia tensor 371 internalSetMassProps(); // type is not None! See case None: in switch 360 372 } 361 373 … … 395 407 } 396 408 409 void WorldEntity::updateCollisionType() 410 { 411 if (!this->physicalBody_) 412 this->collisionType_ = None; 413 else if (this->physicalBody_->isKinematicObject()) 414 this->collisionType_ = Kinematic; 415 else if (this->physicalBody_->isStaticObject()) 416 this->collisionType_ = Static; 417 else 418 this->collisionType_ = Dynamic; 419 } 420 397 421 bool WorldEntity::checkPhysics() const 398 422 { -
code/branches/physics/src/orxonox/objects/worldentities/WorldEntity.h
r2304 r2306 108 108 { this->setDirection(Vector3(x, y, z), relativeTo, localDirectionVector); } 109 109 110 v oid setScale3D(const Vector3& scale);110 virtual void setScale3D(const Vector3& scale); 111 111 inline void setScale3D(float x, float y, float z) 112 112 { this->setScale3D(Vector3(x, y, z)); } … … 119 119 { Vector3 scale = this->getScale3D(); return (scale.x == scale.y && scale.x == scale.z) ? scale.x : 1; } 120 120 121 v oid scale3D(const Vector3& scale);121 virtual void scale3D(const Vector3& scale); 122 122 inline void scale3D(float x, float y, float z) 123 123 { this->scale3D(Vector3(x, y, z)); } … … 182 182 }; 183 183 184 bool hasPhysics() const { return getCollisionType() != None; } 185 186 CollisionType getCollisionType() const { return this->collisionType_; } 187 void setCollisionType(CollisionType type); 188 189 void setCollisionTypeStr(const std::string& type); 190 std::string getCollisionTypeStr() const; 191 184 bool hasPhysics() const { return getCollisionType() != None ; } 192 185 bool isStatic() const { return getCollisionType() == Static ; } 193 186 bool isKinematic() const { return getCollisionType() == Kinematic; } 194 187 bool isDynamic() const { return getCollisionType() == Dynamic ; } 195 188 189 inline CollisionType getCollisionType() const 190 { return this->collisionType_; } 191 void setCollisionType(CollisionType type); 192 193 void setCollisionTypeStr(const std::string& type); 194 std::string getCollisionTypeStr() const; 195 196 196 void setMass(float mass); 197 197 inline float getMass() const … … 201 201 CollisionShape* getAttachedCollisionShape(unsigned int index) const; 202 202 203 CollisionShape* getCollisionShape() { return this->collisionShape_; } 204 btRigidBody* getPhysicalBody() { return this->physicalBody_; } 203 inline CollisionShape* getCollisionShape() 204 { return this->collisionShape_; } 205 inline btRigidBody* getPhysicalBody() 206 { return this->physicalBody_; } 205 207 206 208 protected: 207 //virtual btCollisionShape* getCollisionShape() = 0;208 //virtual void attachPhysicalObject(WorldEntity* object);209 210 209 virtual bool isCollisionTypeLegal(CollisionType type) const = 0; 210 211 btRigidBody* physicalBody_; 212 213 private: 214 void updateCollisionType(); 215 void mergeCollisionShape(CollisionShape* shape); 216 void internalSetMassProps(); 217 btVector3 getLocalInertia(btScalar mass) const; 211 218 bool checkPhysics() const; 212 void updateCollisionType();213 214 btRigidBody* physicalBody_;215 216 private:217 void mergeCollisionShape(CollisionShape* shape);218 219 219 220 CollisionType collisionType_; … … 221 222 CollisionShape* collisionShape_; 222 223 btScalar mass_; 224 btScalar childMass_; 223 225 }; 224 226 } -
code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/CollisionShape.cc
r2304 r2306 78 78 return omni_cast<btVector3>(this->node_->getScale()); 79 79 } 80 81 void CollisionShape::setScale3D(const Vector3& scale) 82 { 83 ThrowException(NotImplemented, "Cannot set the scale of a collision shape: Not yet implemented."); 84 } 85 86 void CollisionShape::scale3D(const Vector3& scale) 87 { 88 ThrowException(NotImplemented, "Cannot set the scale of a collision shape: Not yet implemented."); 89 } 80 90 } -
code/branches/physics/src/orxonox/objects/worldentities/collisionshapes/CollisionShape.h
r2304 r2306 55 55 56 56 private: 57 virtual void setScale3D(const Vector3& scale); 58 virtual void scale3D(const Vector3& scale); 59 57 60 bool isCollisionTypeLegal(WorldEntity::CollisionType type) const; 58 61 };
Note: See TracChangeset
for help on using the changeset viewer.