- Timestamp:
- Dec 10, 2008, 1:38:17 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/physics/src/orxonox/objects/worldentities/WorldEntity.cc
r2313 r2374 14 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 16 * GNU General Public License for more details. 17 17 * 18 18 * You should have received a copy of the GNU General Public License … … 73 73 // Default behaviour does not include physics 74 74 this->physicalBody_ = 0; 75 this->collisionShape_ = 0;75 this->collisionShape_ = new CompoundCollisionShape(this); 76 76 this->mass_ = 0; 77 77 this->childMass_ = 0; 78 78 this->collisionType_ = None; 79 this->collisionTypeSynchronised_ = None; 79 80 80 81 this->registerVariables(); … … 113 114 XMLPortParam(WorldEntity, "scale", setScale, getScale, xmlelement, mode); 114 115 116 // Physics 115 117 XMLPortParam(WorldEntity, "collisionType", setCollisionTypeStr, getCollisionTypeStr, xmlelement, mode); 116 //XMLPortParam(WorldEntity, "collisionRadius", setCollisionRadius, getCollisionRadius, xmlelement, mode);117 118 XMLPortParam(WorldEntity, "mass", setMass, getMass, xmlelement, mode); 118 119 120 // Other attached WorldEntities 119 121 XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode); 122 // Attached collision shapes 120 123 XMLPortObject(WorldEntity, CollisionShape, "collisionShapes", attachCollisionShape, getAttachedCollisionShape, xmlelement, mode); 121 124 } … … 123 126 void WorldEntity::registerVariables() 124 127 { 125 REGISTERDATA(this->bActive_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity)); 126 REGISTERDATA(this->bVisible_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility)); 127 128 REGISTERDATA(this->getScale3D().x, network::direction::toclient); 129 REGISTERDATA(this->getScale3D().y, network::direction::toclient); 130 REGISTERDATA(this->getScale3D().z, network::direction::toclient); 128 REGISTERDATA(this->bActive_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity)); 129 REGISTERDATA(this->bVisible_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility)); 130 131 // HACK: Removed the call because it gets called the first time as well 132 REGISTERDATA(this->getScale3D(), network::direction::toclient);//, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::scaleChanged)); 133 134 int* collisionType = (int*)&this->collisionTypeSynchronised_; 135 REGISTERDATA(*collisionType, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::collisionTypeChanged)); 136 REGISTERDATA(this->mass_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::massChanged)); 131 137 132 138 REGISTERDATA(this->parentID_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::updateParent)); … … 138 144 if (parent) 139 145 this->attachToParent(parent); 146 } 147 148 void WorldEntity::collisionTypeChanged() 149 { 150 if (this->collisionTypeSynchronised_ != Dynamic && 151 this->collisionTypeSynchronised_ != Kinematic && 152 this->collisionTypeSynchronised_ != Static && 153 this->collisionTypeSynchronised_ != None) 154 { 155 CCOUT(1) << "Error when collsion Type was received over network. Unknown enum value:" << this->collisionTypeSynchronised_ << std::endl; 156 } 157 else if (this->collisionTypeSynchronised_ != collisionType_) 158 { 159 if (this->parent_) 160 CCOUT(2) << "Warning: Network connection tried to set the collision type of an attached WE. Ignoring." << std::endl; 161 else 162 this->setCollisionType(this->collisionTypeSynchronised_); 163 } 164 } 165 166 void WorldEntity::massChanged() 167 { 168 this->setMass(this->mass_); 140 169 } 141 170 … … 210 239 void WorldEntity::attachCollisionShape(CollisionShape* shape) 211 240 { 212 this->attachedShapes_.push_back(shape); 213 214 if (!this->collisionShape_ && shape->hasNoTransform()) 215 { 216 // Set local scaling right when adding it. It can include both scaling parameters 217 // and shape parameters (like sphere radius) 218 shape->getCollisionShape()->setLocalScaling(shape->getTotalScaling()); 219 this->collisionShape_ = shape; 220 // recalculate inertia tensor 221 if (this->isDynamic()) 222 internalSetMassProps(); 223 } 224 else 225 { 226 if (this->collisionShape_ && !this->collisionShape_->isCompoundShape()) 227 { 228 // We have to create a new compound shape and add the old one first. 229 CollisionShape* thisShape = this->collisionShape_; 230 this->collisionShape_ = 0; 231 this->mergeCollisionShape(thisShape); 232 } 233 this->mergeCollisionShape(shape); 234 } 241 this->collisionShape_->addChildShape(shape); 235 242 236 243 if (this->physicalBody_) … … 241 248 removeFromPhysicalWorld(); 242 249 } 243 this->physicalBody_->setCollisionShape(this->collisionShape_->getCollisionShape()); 244 } 245 246 addToPhysicalWorld(); 250 if (this->collisionShape_->getCollisionShape()) 251 this->physicalBody_->setCollisionShape(this->collisionShape_->getCollisionShape()); 252 // recalculate inertia tensor 253 internalSetMassProps(); 254 addToPhysicalWorld(); 255 } 247 256 } 248 257 249 258 CollisionShape* WorldEntity::getAttachedCollisionShape(unsigned int index) const 250 259 { 251 if (index < this->attachedShapes_.size()) 252 return attachedShapes_[index]; 253 else 254 return 0; 255 } 256 257 void WorldEntity::mergeCollisionShape(CollisionShape* shape) 258 { 259 if (!this->collisionShape_) 260 this->collisionShape_ = new CompoundCollisionShape(this); 261 assert(this->collisionShape_->isCompoundShape()); 262 263 // merge with transform 264 CompoundCollisionShape* compoundShape = static_cast<CompoundCollisionShape*>(this->collisionShape_); 265 assert(compoundShape); 266 compoundShape->addChildShape(shape); 267 268 // recalculate inertia tensor 269 internalSetMassProps(); 260 return this->collisionShape_->getChildShape(index); 270 261 } 271 262 … … 302 293 this->mass_ = mass; 303 294 if (!this->hasPhysics()) 304 COUT(3) << "Warning: Setting the mass of a WorldEntity with no physics has no effect." << std::endl; 295 return; 296 // TODO: Add this again when new network callbacks work properly 297 //COUT(3) << "Warning: Setting the mass of a WorldEntity with no physics has no effect." << std::endl; 305 298 else 306 299 { … … 320 313 assert(hasPhysics()); 321 314 322 if ( (this->isKinematic() || this->isStatic()) && (this->mass_ + this->childMass_) != 0.0f)323 { 324 // Mass non zero is a bad idea for kinematic and static objects315 if (this->isStatic()) 316 { 317 // Just set everything to zero 325 318 this->physicalBody_->setMassProps(0.0f, btVector3(0, 0, 0)); 326 319 } 327 else if (this->isDynamic() && (this->mass_ + this->childMass_) == 0.0f) 328 { 329 // Mass zero is not such a good idea for dynamic objects 320 else if ((this->mass_ + this->childMass_) == 0.0f) 321 { 322 // Use default values to avoid very large or very small values 323 CCOUT(2) << "Warning: Setting the internal physical mass to 1.0 because mass_ is 0.0." << std::endl; 330 324 this->physicalBody_->setMassProps(1.0f, this->getLocalInertia(1.0f)); 331 325 } … … 337 331 { 338 332 btVector3 inertia(0, 0, 0); 339 if (this->collisionShape_ )333 if (this->collisionShape_->getCollisionShape()) 340 334 this->collisionShape_->getCollisionShape()->calculateLocalInertia(mass, inertia); 341 335 return inertia; … … 368 362 this->physicalBody_ = new btRigidBody(bodyConstructionInfo); 369 363 this->physicalBody_->setUserPointer(this); 364 this->physicalBody_->setActivationState(DISABLE_DEACTIVATION); 370 365 } 371 366 else if (type == None && this->collisionType_ != None) … … 377 372 this->physicalBody_ = 0; 378 373 this->collisionType_ = None; 374 this->collisionTypeSynchronised_ = None; 379 375 return; 380 376 } … … 401 397 402 398 // update mass and inertia tensor 403 internalSetMassProps(); // type is not None! See case None :in switch399 internalSetMassProps(); // type is not None! See case None in switch 404 400 405 401 addToPhysicalWorld(); … … 451 447 else 452 448 this->collisionType_ = Dynamic; 449 this->collisionTypeSynchronised_ = this->collisionType_; 450 } 451 452 bool WorldEntity::isPhysicsRunning() const 453 { 454 return this->physicalBody_ && this->physicalBody_->isInWorld(); 453 455 } 454 456
Note: See TracChangeset
for help on using the changeset viewer.