- Timestamp:
- Dec 2, 2008, 4:59:36 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 {
Note: See TracChangeset
for help on using the changeset viewer.