Bladeren bron

Added PhysicsMaterial

BearishSun 10 jaren geleden
bovenliggende
commit
61de8ea6b2

+ 3 - 0
BansheeCore/BansheeCore.vcxproj

@@ -307,6 +307,8 @@
     <ClInclude Include="Include\BsMeshUtility.h" />
     <ClInclude Include="Include\BsMeshUtility.h" />
     <ClInclude Include="Include\BsParamBlocks.h" />
     <ClInclude Include="Include\BsParamBlocks.h" />
     <ClInclude Include="Include\BsPhysics.h" />
     <ClInclude Include="Include\BsPhysics.h" />
+    <ClInclude Include="Include\BsPhysicsMaterial.h" />
+    <ClInclude Include="Include\BsPhysicsMaterialRTTI.h" />
     <ClInclude Include="Include\BsPrefab.h" />
     <ClInclude Include="Include\BsPrefab.h" />
     <ClInclude Include="Include\BsPrefabDiff.h" />
     <ClInclude Include="Include\BsPrefabDiff.h" />
     <ClInclude Include="Include\BsPrefabDiffRTTI.h" />
     <ClInclude Include="Include\BsPrefabDiffRTTI.h" />
@@ -472,6 +474,7 @@
     <ClCompile Include="Source\BsMeshUtility.cpp" />
     <ClCompile Include="Source\BsMeshUtility.cpp" />
     <ClCompile Include="Source\BsPhysics.cpp" />
     <ClCompile Include="Source\BsPhysics.cpp" />
     <ClCompile Include="Source\BsPhysicsManager.cpp" />
     <ClCompile Include="Source\BsPhysicsManager.cpp" />
+    <ClCompile Include="Source\BsPhysicsMaterial.cpp" />
     <ClCompile Include="Source\BsPrefab.cpp" />
     <ClCompile Include="Source\BsPrefab.cpp" />
     <ClCompile Include="Source\BsPrefabDiff.cpp" />
     <ClCompile Include="Source\BsPrefabDiff.cpp" />
     <ClCompile Include="Source\BsPrefabUtility.cpp" />
     <ClCompile Include="Source\BsPrefabUtility.cpp" />

+ 9 - 0
BansheeCore/BansheeCore.vcxproj.filters

@@ -593,6 +593,12 @@
     <ClInclude Include="Include\BsPhysics.h">
     <ClInclude Include="Include\BsPhysics.h">
       <Filter>Header Files\Physics</Filter>
       <Filter>Header Files\Physics</Filter>
     </ClInclude>
     </ClInclude>
+    <ClInclude Include="Include\BsPhysicsMaterial.h">
+      <Filter>Header Files\Physics</Filter>
+    </ClInclude>
+    <ClInclude Include="Include\BsPhysicsMaterialRTTI.h">
+      <Filter>Header Files\RTTI</Filter>
+    </ClInclude>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsCoreApplication.cpp">
     <ClCompile Include="Source\BsCoreApplication.cpp">
@@ -931,5 +937,8 @@
     <ClCompile Include="Source\BsPhysics.cpp">
     <ClCompile Include="Source\BsPhysics.cpp">
       <Filter>Source Files\Physics</Filter>
       <Filter>Source Files\Physics</Filter>
     </ClCompile>
     </ClCompile>
+    <ClCompile Include="Source\BsPhysicsMaterial.cpp">
+      <Filter>Source Files\Physics</Filter>
+    </ClCompile>
   </ItemGroup>
   </ItemGroup>
 </Project>
 </Project>

+ 5 - 1
BansheeCore/Include/BsCorePrerequisites.h

@@ -251,6 +251,7 @@ namespace BansheeEngine
 	class ResourceMetaData;
 	class ResourceMetaData;
 	class OSDropTarget;
 	class OSDropTarget;
 	class StringTable;
 	class StringTable;
+	class PhysicsMaterial;
 	// Scene
 	// Scene
 	class SceneObject;
 	class SceneObject;
 	class Component;
 	class Component;
@@ -335,6 +336,7 @@ namespace BansheeEngine
 	typedef std::shared_ptr<PrefabDiff> PrefabDiffPtr;
 	typedef std::shared_ptr<PrefabDiff> PrefabDiffPtr;
 	typedef std::shared_ptr<RendererMeshData> RendererMeshDataPtr;
 	typedef std::shared_ptr<RendererMeshData> RendererMeshDataPtr;
 	typedef std::shared_ptr<RenderAPIFactory> RenderAPIFactoryPtr;
 	typedef std::shared_ptr<RenderAPIFactory> RenderAPIFactoryPtr;
+	typedef std::shared_ptr<PhysicsMaterial> PhysicsMaterialPtr;
 }
 }
 
 
 /************************************************************************/
 /************************************************************************/
@@ -403,7 +405,8 @@ namespace BansheeEngine
 		TID_TextureParamData = 1088,
 		TID_TextureParamData = 1088,
 		TID_StructParamData = 1089,
 		TID_StructParamData = 1089,
 		TID_MaterialParams = 1090,
 		TID_MaterialParams = 1090,
-		TID_MaterialRTTIParam = 1091
+		TID_MaterialRTTIParam = 1091,
+		TID_PhysicsMaterial = 1092
 	};
 	};
 }
 }
 
 
@@ -425,6 +428,7 @@ namespace BansheeEngine
 	typedef ResourceHandle<Shader> HShader;
 	typedef ResourceHandle<Shader> HShader;
 	typedef ResourceHandle<Prefab> HPrefab;
 	typedef ResourceHandle<Prefab> HPrefab;
 	typedef ResourceHandle<StringTable> HStringTable;
 	typedef ResourceHandle<StringTable> HStringTable;
+	typedef ResourceHandle<PhysicsMaterial> HPhysicsMaterial;
 }
 }
 
 
 namespace BansheeEngine
 namespace BansheeEngine

+ 7 - 0
BansheeCore/Include/BsPhysics.h

@@ -1,3 +1,5 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 #pragma once
 #pragma once
 
 
 #include "BsCorePrerequisites.h"
 #include "BsCorePrerequisites.h"
@@ -13,6 +15,8 @@ namespace BansheeEngine
 
 
 		virtual void update() = 0;
 		virtual void update() = 0;
 
 
+		virtual SPtr<PhysicsMaterial> createMaterial(float staticFriction, float dynamicFriction, float restitution) = 0;
+
 		void toggleCollision(UINT64 groupA, UINT64 groupB, bool enabled);
 		void toggleCollision(UINT64 groupA, UINT64 groupB, bool enabled);
 		bool isCollisionEnabled(UINT64 groupA, UINT64 groupB) const;
 		bool isCollisionEnabled(UINT64 groupA, UINT64 groupB) const;
 
 
@@ -21,4 +25,7 @@ namespace BansheeEngine
 		mutable Mutex mMutex;
 		mutable Mutex mMutex;
 		bool mCollisionMap[CollisionMapSize][CollisionMapSize];
 		bool mCollisionMap[CollisionMapSize][CollisionMapSize];
 	};
 	};
+
+	/** Provides easier access to Physics. */
+	BS_CORE_EXPORT Physics& gPhysics();
 }
 }

+ 2 - 0
BansheeCore/Include/BsPhysicsManager.h

@@ -1,3 +1,5 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 #pragma once
 #pragma once
 
 
 #include "BsCorePrerequisites.h"
 #include "BsCorePrerequisites.h"

+ 35 - 0
BansheeCore/Include/BsPhysicsMaterial.h

@@ -0,0 +1,35 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsCorePrerequisites.h"
+#include "BsResource.h"
+
+namespace BansheeEngine
+{
+	class BS_CORE_EXPORT PhysicsMaterial : public Resource
+	{
+	public:
+		virtual ~PhysicsMaterial() { }
+
+		virtual void setStaticFriction(float value) = 0;
+		virtual float getStaticFriction() const = 0;
+
+		virtual void setDynamicFriction(float value) = 0;
+		virtual float getDynamicFriction() const = 0;
+
+		virtual void setRestitutionCoefficient(float value) = 0;
+		virtual float getRestitutionCoefficient() const = 0;
+
+		static HPhysicsMaterial create(float staticFriction = 0.0f, float dynamicFriction = 0.0f, float restitution = 0.0f);
+		static PhysicsMaterialPtr _createPtr(float staticFriction = 0.0f, float dynamicFriction = 0.0f, float restitution = 0.0f);
+
+		/************************************************************************/
+		/* 								SERIALIZATION                      		*/
+		/************************************************************************/
+	public:
+		friend class PhysicsMaterialRTTI;
+		static RTTITypeBase* getRTTIStatic();
+		RTTITypeBase* getRTTI() const override;
+	};
+}

+ 100 - 0
BansheeCore/Include/BsPhysicsMaterialRTTI.h

@@ -0,0 +1,100 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsCorePrerequisites.h"
+#include "BsRTTIType.h"
+#include "BsPhysicsMaterial.h"
+
+namespace BansheeEngine
+{
+	/** @cond RTTI */
+	/** @addtogroup RTTI-Impl-Core
+	 *  @{
+	 */
+
+	class BS_CORE_EXPORT PhysicsMaterialRTTI : public RTTIType<PhysicsMaterial, Resource, PhysicsMaterialRTTI>
+	{
+		struct PhysicsMaterialData
+		{
+			float staticFriction;
+			float dynamicFriction;
+			float restitutionCoefficient;
+		};
+
+	private:
+		float& getStaticFriction(PhysicsMaterial* obj)
+		{
+			PhysicsMaterialData& materialData = any_cast_ref<PhysicsMaterialData>(obj->mRTTIData);
+
+			return materialData.staticFriction;
+		}
+
+		void setStaticFriction(PhysicsMaterial* obj, float& size) { obj->setStaticFriction(size); }
+
+		float& getDynamicFriction(PhysicsMaterial* obj)
+		{
+			PhysicsMaterialData& materialData = any_cast_ref<PhysicsMaterialData>(obj->mRTTIData);
+
+			return materialData.dynamicFriction;
+		}
+
+		void setDynamicFriction(PhysicsMaterial* obj, float& size) { obj->setDynamicFriction(size); }
+
+		float& getRestitutionCoefficient(PhysicsMaterial* obj)
+		{
+			PhysicsMaterialData& materialData = any_cast_ref<PhysicsMaterialData>(obj->mRTTIData);
+
+			return materialData.restitutionCoefficient;
+		}
+
+		void setRestitutionCoefficient(PhysicsMaterial* obj, float& size) { obj->setRestitutionCoefficient(size); }
+
+	public:
+		PhysicsMaterialRTTI()
+		{
+			addPlainField("staticFriction", 0, &PhysicsMaterialRTTI::getStaticFriction, &PhysicsMaterialRTTI::setStaticFriction);
+			addPlainField("dynamicFriction", 1, &PhysicsMaterialRTTI::getDynamicFriction, &PhysicsMaterialRTTI::setDynamicFriction);
+			addPlainField("restitutionCoefficient", 2, &PhysicsMaterialRTTI::getRestitutionCoefficient, &PhysicsMaterialRTTI::setRestitutionCoefficient);
+		}
+
+		void onSerializationStarted(IReflectable* obj) override
+		{
+			PhysicsMaterial* material = static_cast<PhysicsMaterial*>(obj);
+
+			PhysicsMaterialData materialData = 
+			{ 
+				material->getStaticFriction(), 
+				material->getDynamicFriction(), 
+				material->getRestitutionCoefficient() 
+			};
+
+			material->mRTTIData = materialData;
+		}
+
+		void onSerializationEnded(IReflectable* obj) override
+		{
+			PhysicsMaterial* material = static_cast<PhysicsMaterial*>(obj);
+			material->mRTTIData = nullptr;
+		}
+
+		const String& getRTTIName() override
+		{
+			static String name = "PhysicsMaterial";
+			return name;
+		}
+
+		UINT32 getRTTIId() override
+		{
+			return TID_PhysicsMaterial;
+		}
+
+		std::shared_ptr<IReflectable> newRTTIObject() override
+		{
+			return PhysicsMaterial::_createPtr();
+		}
+	};
+
+	/** @} */
+	/** @endcond */
+}

+ 5 - 0
BansheeCore/Source/BsPhysics.cpp

@@ -22,4 +22,9 @@ namespace BansheeEngine
 		mMutex.lock();
 		mMutex.lock();
 		return mCollisionMap[groupA][groupB];
 		return mCollisionMap[groupA][groupB];
 	}
 	}
+
+	Physics& gPhysics()
+	{
+		return Physics::instance();
+	}
 }
 }

+ 33 - 0
BansheeCore/Source/BsPhysicsMaterial.cpp

@@ -0,0 +1,33 @@
+#include "BsPhysicsMaterial.h"
+#include "BsPhysicsMaterialRTTI.h"
+#include "BsResources.h"
+#include "BsPhysics.h"
+
+namespace BansheeEngine
+{
+	RTTITypeBase* PhysicsMaterial::getRTTIStatic()
+	{
+		return PhysicsMaterialRTTI::instance();
+	}
+
+	RTTITypeBase* PhysicsMaterial::getRTTI() const
+	{
+		return getRTTIStatic();
+	}
+
+	HPhysicsMaterial PhysicsMaterial::create(float staticFriction, float dynamicFriction, float restitution)
+	{
+		PhysicsMaterialPtr newMaterial = _createPtr(staticFriction, dynamicFriction, restitution);
+
+		return static_resource_cast<PhysicsMaterial>(gResources()._createResourceHandle(newMaterial));
+	}
+
+	PhysicsMaterialPtr PhysicsMaterial::_createPtr(float staticFriction, float dynamicFriction, float restitution)
+	{
+		PhysicsMaterialPtr newMaterial = gPhysics().createMaterial(staticFriction, dynamicFriction, restitution);
+		newMaterial->_setThisPtr(newMaterial);
+		newMaterial->initialize();
+
+		return newMaterial;
+	}
+}

+ 2 - 0
BansheePhysX/BansheePhysX.vcxproj

@@ -246,10 +246,12 @@
   </ItemDefinitionGroup>
   </ItemDefinitionGroup>
   <ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsPhysX.cpp" />
     <ClCompile Include="Source\BsPhysX.cpp" />
+    <ClCompile Include="Source\BsPhysXMaterial.cpp" />
     <ClCompile Include="Source\BsPhysXPlugin.cpp" />
     <ClCompile Include="Source\BsPhysXPlugin.cpp" />
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <ClInclude Include="Include\BsPhysX.h" />
     <ClInclude Include="Include\BsPhysX.h" />
+    <ClInclude Include="Include\BsPhysXMaterial.h" />
     <ClInclude Include="Include\BsPhysXPrerequisites.h" />
     <ClInclude Include="Include\BsPhysXPrerequisites.h" />
   </ItemGroup>
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

+ 6 - 0
BansheePhysX/BansheePhysX.vcxproj.filters

@@ -17,6 +17,9 @@
     <ClCompile Include="Source\BsPhysX.cpp">
     <ClCompile Include="Source\BsPhysX.cpp">
       <Filter>Source Files</Filter>
       <Filter>Source Files</Filter>
     </ClCompile>
     </ClCompile>
+    <ClCompile Include="Source\BsPhysXMaterial.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <ClInclude Include="Include\BsPhysXPrerequisites.h">
     <ClInclude Include="Include\BsPhysXPrerequisites.h">
@@ -25,5 +28,8 @@
     <ClInclude Include="Include\BsPhysX.h">
     <ClInclude Include="Include\BsPhysX.h">
       <Filter>Header Files</Filter>
       <Filter>Header Files</Filter>
     </ClInclude>
     </ClInclude>
+    <ClInclude Include="Include\BsPhysXMaterial.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   </ItemGroup>
 </Project>
 </Project>

+ 3 - 0
BansheePhysX/Include/BsPhysX.h

@@ -1,3 +1,5 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 #pragma once
 #pragma once
 
 
 #include "BsPhysXPrerequisites.h"
 #include "BsPhysXPrerequisites.h"
@@ -15,6 +17,7 @@ namespace BansheeEngine
 		~PhysX();
 		~PhysX();
 
 
 		void update() override;
 		void update() override;
+		SPtr<PhysicsMaterial> createMaterial(float staticFriction, float dynamicFriction, float restitution) override;
 
 
 	private:
 	private:
 		float mSimulationStep = 1.0f/60.0f;
 		float mSimulationStep = 1.0f/60.0f;

+ 29 - 0
BansheePhysX/Include/BsPhysXMaterial.h

@@ -0,0 +1,29 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsPhysXPrerequisites.h"
+#include "BsPhysicsMaterial.h"
+#include "PxMaterial.h"
+
+namespace BansheeEngine
+{
+	class PhysXMaterial : public PhysicsMaterial
+	{
+	public:
+		PhysXMaterial(physx::PxPhysics* physx, float staFric, float dynFriction, float restitution);
+		~PhysXMaterial();
+
+		void setStaticFriction(float value) override;
+		float getStaticFriction() const override;
+
+		void setDynamicFriction(float value) override;
+		float getDynamicFriction() const override;
+
+		void setRestitutionCoefficient(float value) override;
+		float getRestitutionCoefficient() const override;
+
+	private:
+		physx::PxMaterial* mInternal;
+	};
+}

+ 6 - 0
BansheePhysX/Source/BsPhysX.cpp

@@ -1,5 +1,6 @@
 #include "BsPhysX.h"
 #include "BsPhysX.h"
 #include "PxPhysicsAPI.h"
 #include "PxPhysicsAPI.h"
+#include "BsPhysXMaterial.h"
 #include "BsTaskScheduler.h"
 #include "BsTaskScheduler.h"
 #include "BsTime.h"
 #include "BsTime.h"
 #include "Bsvector3.h"
 #include "Bsvector3.h"
@@ -231,4 +232,9 @@ namespace BansheeEngine
 
 
 		mLastSimulationTime = curFrameTime; 
 		mLastSimulationTime = curFrameTime; 
 	}
 	}
+
+	SPtr<PhysicsMaterial> PhysX::createMaterial(float staticFriction, float dynamicFriction, float restitution)
+	{
+		return bs_shared_ptr_new<PhysXMaterial>(mPhysics, staticFriction, dynamicFriction, restitution);
+	}
 }
 }

+ 46 - 0
BansheePhysX/Source/BsPhysXMaterial.cpp

@@ -0,0 +1,46 @@
+#include "BsPhysXMaterial.h"
+#include "PxPhysics.h"
+
+namespace BansheeEngine
+{
+	PhysXMaterial::PhysXMaterial(physx::PxPhysics* physx, float staFric, float dynFriction, float restitution)
+		:mInternal(nullptr)
+	{
+		mInternal = physx->createMaterial(staFric, dynFriction, restitution);
+	}
+
+	PhysXMaterial::~PhysXMaterial()
+	{
+		mInternal->release();
+	}
+
+	void PhysXMaterial::setStaticFriction(float value)
+	{
+		mInternal->setStaticFriction(value);
+	}
+
+	float PhysXMaterial::getStaticFriction() const
+	{
+		return mInternal->getStaticFriction();
+	}
+
+	void PhysXMaterial::setDynamicFriction(float value)
+	{
+		mInternal->setDynamicFriction(value);
+	}
+
+	float PhysXMaterial::getDynamicFriction() const
+	{
+		return mInternal->getDynamicFriction();
+	}
+
+	void PhysXMaterial::setRestitutionCoefficient(float value)
+	{
+		mInternal->setRestitution(value);
+	}
+
+	float PhysXMaterial::getRestitutionCoefficient() const
+	{
+		return mInternal->getRestitution();
+	}
+}