Bladeren bron

Merge remote-tracking branch 'feltech/custom_physics'

Lasse Öörni 9 jaren geleden
bovenliggende
commit
13ec83ad5f

+ 8 - 0
Source/Urho3D/Physics/CollisionShape.cpp

@@ -1159,6 +1159,7 @@ void CollisionShape::UpdateShape()
             break;
 
         default:
+        	shape_ = this->UpdateDerivedShape(shapeType_, newWorldScale);
             break;
         }
 
@@ -1178,6 +1179,13 @@ void CollisionShape::UpdateShape()
     retryCreation_ = false;
 }
 
+btCollisionShape* CollisionShape::UpdateDerivedShape(int shapeType, const Vector3& newWorldScale)
+{
+    // To be overridden in derived classes.
+	return NULL;
+}
+
+
 void CollisionShape::HandleTerrainCreated(StringHash eventType, VariantMap& eventData)
 {
     if (shapeType_ == SHAPE_TERRAIN)

+ 8 - 0
Source/Urho3D/Physics/CollisionShape.h

@@ -244,6 +244,14 @@ protected:
     virtual void OnSceneSet(Scene* scene);
     /// Handle node transform being dirtied.
     virtual void OnMarkedDirty(Node* node);
+    /**
+     * Called when instantiating a collision shape that is not one of ShapeType (default no-op).
+     *
+     * Useful for custom shape types that subclass CollisionShape and use a non-standard underlying
+     * btCollisionShape. UpdateDerivedShape can then be overridden to create the required
+     * btCollisionShape subclass.
+     */
+    virtual btCollisionShape* UpdateDerivedShape(int shapeType, const Vector3& newWorldScale);
 
 private:
     /// Find the parent rigid body component and return its compound collision shape.

+ 9 - 1
Source/Urho3D/Physics/PhysicsWorld.cpp

@@ -58,6 +58,8 @@ static const int MAX_SOLVER_ITERATIONS = 256;
 static const int DEFAULT_FPS = 60;
 static const Vector3 DEFAULT_GRAVITY = Vector3(0.0f, -9.81f, 0.0f);
 
+PhysicsWorldConfig PhysicsWorld::config;
+
 static bool CompareRaycastResults(const PhysicsRaycastResult& lhs, const PhysicsRaycastResult& rhs)
 {
     return lhs.distance_ < rhs.distance_;
@@ -114,6 +116,7 @@ struct PhysicsQueryCallback : public btCollisionWorld::ContactResultCallback
     unsigned collisionMask_;
 };
 
+
 PhysicsWorld::PhysicsWorld(Context* context) :
     Component(context),
     collisionConfiguration_(0),
@@ -135,7 +138,11 @@ PhysicsWorld::PhysicsWorld(Context* context) :
 {
     gContactAddedCallback = CustomMaterialCombinerCallback;
 
-    collisionConfiguration_ = new btDefaultCollisionConfiguration();
+    if (PhysicsWorld::config.collisionConfig)
+        collisionConfiguration_ = PhysicsWorld::config.collisionConfig;
+    else
+        collisionConfiguration_ = new btDefaultCollisionConfiguration();
+
     collisionDispatcher_ = new btCollisionDispatcher(collisionConfiguration_);
     broadphase_ = new btDbvtBroadphase();
     solver_ = new btSequentialImpulseConstraintSolver();
@@ -149,6 +156,7 @@ PhysicsWorld::PhysicsWorld(Context* context) :
     world_->setInternalTickCallback(InternalTickCallback, static_cast<void*>(this), false);
 }
 
+
 PhysicsWorld::~PhysicsWorld()
 {
     if (scene_)

+ 16 - 0
Source/Urho3D/Physics/PhysicsWorld.h

@@ -96,6 +96,19 @@ struct DelayedWorldTransform
     Quaternion worldRotation_;
 };
 
+/**
+ * Custom overrides of physics internals.
+ *
+ * Must be modified before the physics component is created.
+ */
+struct PhysicsWorldConfig
+{
+    /// Override for the collision configuration (default btDefaultCollisionConfiguration).
+    btCollisionConfiguration*    collisionConfig;
+
+    PhysicsWorldConfig() : collisionConfig(0) {}
+};
+
 static const float DEFAULT_MAX_NETWORK_ANGULAR_VELOCITY = 100.0f;
 
 /// Physics simulation world component. Should be added only to the root scene node.
@@ -107,6 +120,9 @@ class URHO3D_API PhysicsWorld : public Component, public btIDebugDraw
     friend void InternalTickCallback(btDynamicsWorld* world, btScalar timeStep);
 
 public:
+    /// Allow overrides of the internal configuration.
+    static struct PhysicsWorldConfig config;
+
     /// Construct.
     PhysicsWorld(Context* scontext);
     /// Destruct.