00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <physics/Capsule>
00023
00024 #include <base/Externalizer>
00025 #include <gfx/VisualTriangles>
00026 #include <physics/Material>
00027 #include <physics/OBBCollisionModel>
00028 #include <physics/GJKCollisionModel>
00029
00030 #include <osg/Group>
00031 #include <osg/Geode>
00032 #include <osg/LOD>
00033 #include <osg/ShapeDrawable>
00034
00035 using physics::Capsule;
00036
00037 using base::Transform;
00038 using base::dom::DOMNode;
00039 using base::dom::DOMElement;
00040
00041 using gfx::Segment3;
00042 using gfx::VisualTriangles;
00043
00044 using physics::MassProperties;
00045 using physics::CollisionModel;
00046 using physics::OBBCollisionModel;
00047 using physics::GJKCollisionModel;
00048
00049
00050 using osg::Node;
00051 using osg::Group;
00052 using osg::Geode;
00053 using osg::LOD;
00054 using osg::Vec3;
00055 using osg::Vec2;
00056
00057
00058 Capsule::Capsule()
00059 : _height(1.0), _radius(1.0), massPropertiesCached(false)
00060 {
00061 }
00062
00063 Capsule::Capsule(Real height, Real radius)
00064 : _height(height), _radius(radius), massPropertiesCached(false)
00065 {
00066 if (_radius<0) _radius=consts::epsilon;
00067 }
00068
00069 Capsule::Capsule(const Capsule& c)
00070 : _height(c._height), _radius(c._radius), massPropertiesCached(false)
00071 {
00072 }
00073
00074 Capsule::~Capsule()
00075 {
00076 }
00077
00078
00079
00080 const MassProperties& Capsule::getMassProperties(ref<const Material> material) const
00081 {
00082 if (massPropertiesCached && (density == material->density()))
00083 return massProperties;
00084
00085 density = material->density();
00086 Real volume = consts::Pi*Math::sqr(_radius)*_height;
00087 massProperties.mass = volume*density;
00088
00089 const Real m = massProperties.mass;
00090 Matrix3 Ibody;
00091 Ibody.e(1,1) = m*Math::sqr(_radius)/4.0 + m*Math::sqr(_height)/12.0;
00092 Ibody.e(2,2) = Ibody.e(1,1);
00093 Ibody.e(3,3) = m*Math::sqr(_radius)/2.0;
00094
00095 massProperties.setIbody(Ibody);
00096 massProperties.centerOfMass = Point3(0.0,0.0,0.0);
00097
00098 massPropertiesCached = true;
00099 return massProperties;
00100 }
00101
00102
00103
00104 Segment3 Capsule::shortestSegmentBetween(const base::Transform& t, const Point3& p) const
00105 {
00106 Unimplemented;
00107 }
00108
00109
00110 Segment3 Capsule::shortestSegmentBetween(const base::Transform& t, const gfx::Segment3& s) const
00111 {
00112
00113 Point3 axisstart(0,0,-height()/2.0); axisstart = t.transform(axisstart);
00114 Point3 axisend(0,0,height()/2.0); axisend = t.transform(axisend);
00115 Segment3 axisseg( axisstart, axisend);
00116 Segment3 toaxis = s.shortestSegmentBetween(axisseg);
00117
00118
00119
00120 ref<Sphere> sphere( NewObj Sphere(radius()) );
00121 Transform center(toaxis.e);
00122 return sphere->shortestSegmentBetween(center, s);
00123 }
00124
00125
00126 Segment3 Capsule::shortestSegmentBetween(const base::Transform& t, const gfx::Triangle3& tri) const
00127 {
00128 Unimplemented;
00129 }
00130
00131
00132 Segment3 Capsule::shortestSegmentBetween(const base::Transform& t, const gfx::Quad3& q) const
00133 {
00134 Unimplemented;
00135 }
00136
00137
00138 Segment3 Capsule::shortestSegmentBetween(const base::Transform& t1, ref<const Shape> s, const base::Transform& t2) const
00139 {
00140 Unimplemented;
00141 }
00142
00143
00144
00145
00146 osg::Node* Capsule::createOSGCapsule(Visual::Attributes visualAttributes,
00147 Int slices, Int stacks) const
00148 {
00149
00150 bool onlyVerts = ((visualAttributes & Visual::VerticesOnly) != 0);
00151
00152
00153 osg::ref_ptr<osg::ShapeDrawable> shapeDrawable = new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(0.0f,0.0f,0.0f),radius(),height()));
00154 osg::ref_ptr<osg::TessellationHints> tessHints = new osg::TessellationHints();
00155 tessHints->setTargetNumFaces(slices*stacks);
00156 tessHints->setTessellationMode(osg::TessellationHints::USE_TARGET_NUM_FACES);
00157 tessHints->setCreateNormals(!onlyVerts);
00158 tessHints->setCreateTextureCoords(!onlyVerts);
00159 shapeDrawable->setTessellationHints(&(*tessHints));
00160
00161 osg::Geode* geode = new osg::Geode();
00162 geode->addDrawable(&(*shapeDrawable));
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 return geode;
00311 }
00312
00313
00314
00315 osg::Node* Capsule::createOSGVisual(Visual::Attributes visualAttributes) const
00316 {
00317 if ((node!=0) && (attributes==visualAttributes))
00318 return &(*node);
00319
00320 Real r = Math::maximum(0.5,radius());
00321 Real h = Math::maximum(1.0,height());
00322
00323
00324
00325
00326
00327
00328
00329 osg::Node* node0 = createOSGCapsule(visualAttributes, Int(104*r), (h<40)?Int(12*h):2000);
00330 osg::Node* node1 = createOSGCapsule(visualAttributes, Int(80*r), (h<40)?Int(8*h):1300);
00331 osg::Node* node2 = createOSGCapsule(visualAttributes, Int(40*r), (h<40)?Int(4*h):700);
00332 osg::Node* node3 = createOSGCapsule(visualAttributes, 16, 1);
00333
00334
00335 osg::LOD* lod = new osg::LOD();
00336 lod->setName("Capsule");
00337 lod->addChild(node0);
00338 lod->addChild(node1);
00339 lod->addChild(node2);
00340 lod->addChild(node3);
00341
00342 lod->setRange(0,0,2.0);
00343 lod->setRange(1,2.0,16.0);
00344 lod->setRange(2,16.0,120.0*Math::maximum(r,h));
00345 lod->setRange(3,120.0*Math::maximum(r,h),consts::Infinity);
00346
00347 if (!(visualAttributes & Visual::ShowAxes))
00348 node = lod;
00349 else {
00350 Group* group = new Group();
00351 group->addChild( lod );
00352 group->addChild( createOSGAxes(base::Dimension3(2.0*radius(),2.0*radius(),height())) );
00353 node = group;
00354 }
00355
00356 attributes = visualAttributes;
00357 return &(*node);
00358 }
00359
00360
00361
00362 base::ref<CollisionModel> Capsule::getCollisionModel(CollisionModel::CollisionModelType modelType) const
00363 {
00364 if ((collisionModel!=0) &&
00365 ((this->modelType==modelType) || (modelType==CollisionModel::AnyModel)))
00366 return collisionModel;
00367
00368 collisionModel = Shape::getCollisionModel(modelType);
00369 this->modelType=modelType;
00370
00371 return collisionModel;
00372 }
00373
00374
00375 void Capsule::serialize(base::Serializer& s)
00376 {
00377 s(_height)(_radius);
00378
00379 if (s.isInput()) {
00380 massPropertiesCached = false;
00381 node = 0;
00382 collisionModel = ref<CollisionModel>(0);
00383 }
00384 }
00385
00386
00387
00388 bool Capsule::formatSupported(String format, Real version, ExternalizationType type) const
00389 {
00390 return ( (format=="xml") && (version==1.0) );
00391 }
00392
00393
00394 void Capsule::externalize(base::Externalizer& e, String format, Real version)
00395 {
00396 if (format == "") format = "xml";
00397
00398 if (!formatSupported(format,version,e.ioType()))
00399 throw std::invalid_argument(Exception(String("format ")+format+" v"+base::realToString(version)+" unsupported"));
00400
00401 if (e.isOutput()) {
00402
00403 DOMElement* capsuleElem = e.createElement("capsule");
00404 e.setElementAttribute(capsuleElem,"height",base::realToString(_height));
00405 e.setElementAttribute(capsuleElem,"radius",base::realToString(_radius));
00406
00407 e.appendElement(capsuleElem);
00408 }
00409 else {
00410
00411 massPropertiesCached = false;
00412 node = 0;
00413 collisionModel = ref<CollisionModel>(0);
00414
00415 DOMNode* context = e.context();
00416
00417 DOMElement* capsuleElem = e.getFirstElement(context, "capsule");
00418
00419 _height = e.toReal( e.getDefaultedElementAttribute(capsuleElem, "height", "1") );
00420 _radius = e.toReal( e.getDefaultedElementAttribute(capsuleElem, "radius", "1") );
00421
00422 e.removeElement(capsuleElem);
00423
00424 }
00425 }
00426
00427