Răsfoiți Sursa

Fixed up 3d physics constraints, added point2point constraint

Ivan Safrin 10 ani în urmă
părinte
comite
36c6602022

+ 1 - 1
Bindings/Scripts/create_lua_library/create_lua_library.py

@@ -154,7 +154,7 @@ def createLUABindings(inputPath, prefix, mainInclude, libSmallName, libName, api
 	# Iterate, process each input file
 	for fileName in filteredFiles:
 		# "Package owned" classes that ship with Polycode
-		inheritInModule = ["PhysicsEntity", "CollisionScene", "CollisionEntity", "UIElement", "UIWindow", "UIMenuItem", "UIImage", "UIRect"]
+		inheritInModule = ["PhysicsGenericConstraint", "PhysicsHingeConstraint", "PhysicsPointToPointConstraint", "PhysicsConstraint", "PhysicsEntity", "CollisionScene", "CollisionEntity", "UIElement", "UIWindow", "UIMenuItem", "UIImage", "UIRect"]
 		
 		# A file or comma-separated list of files can be given to specify classes which are "package owned"
 		# and should not be inherited out of Polycode/. The files should contain one class name per line,

+ 2 - 0
Modules/Contents/3DPhysics/CMakeLists.txt

@@ -1,6 +1,7 @@
 INCLUDE(PolycodeIncludes)
 
 SET(polycode3DPhysics_SRCS
+    Source/PolyPhysicsConstraint.cpp
     Source/PolyPhysicsSceneEntity.cpp
     Source/PolyPhysicsScene.cpp
     Source/PolyCollisionSceneEntity.cpp
@@ -8,6 +9,7 @@ SET(polycode3DPhysics_SRCS
 )
 
 SET(polycode3DPhysics_HDRS
+    Include/PolyPhysicsConstraint.h
     Include/PolyPhysicsSceneEntity.h
     Include/Polycode3DPhysics.h
     Include/PolyCollisionScene.h

+ 39 - 0
Modules/Contents/3DPhysics/Include/PolyPhysicsConstraint.h

@@ -0,0 +1,39 @@
+/*
+Copyright (C) 2015 by Ivan Safrin
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#pragma once
+#include "PolyGlobals.h"
+#include "PolyQuaternion.h"
+#include "PolyCollisionSceneEntity.h"
+#include "btBulletCollisionCommon.h"
+#include "btBulletDynamicsCommon.h"
+#include <vector>
+
+namespace Polycode {
+
+    class _PolyExport PhysicsConstraint {
+        public:
+            virtual ~PhysicsConstraint();
+            btTypedConstraint *btConstraint;
+    };
+
+}

+ 15 - 7
Modules/Contents/3DPhysics/Include/PolyPhysicsScene.h

@@ -23,6 +23,7 @@ THE SOFTWARE.
 #pragma once
 #include "PolyGlobals.h"
 #include "PolyCollisionScene.h"
+#include "PolyPhysicsConstraint.h"
 #include <vector>
 
 class btDiscreteDynamicsWorld;
@@ -31,6 +32,7 @@ class btSequentialImpulseConstraintSolver;
 class btGhostPairCallback;
 class btTypedConstraint;
 class btHingeConstraint;
+class btPoint2PointConstraint;
 class btGeneric6DofConstraint;
 
 namespace Polycode {
@@ -61,7 +63,7 @@ namespace Polycode {
 			Vector3 worldNormalOnB;				
 	};
 	
-	class _PolyExport PhysicsGenericConstraint {
+    class _PolyExport PhysicsGenericConstraint : public PhysicsConstraint {
 		public:
 			
 			void setLinearLowerLimit(Vector3 limit);
@@ -70,17 +72,21 @@ namespace Polycode {
 			void setAngularLowerLimit(Vector3 limit);
 			void setAngularUpperLimit(Vector3 limit);
 					
-			btGeneric6DofConstraint *btConstraint;
+			btGeneric6DofConstraint *btGenericConstraint;
 	};
 
-	class _PolyExport PhysicsHingeConstraint  {
+	class _PolyExport PhysicsHingeConstraint  : public PhysicsConstraint {
 		public:
-			~PhysicsHingeConstraint();
 			void setLimits(Number minLimit, Number maxLimit);
 			Number getAngle();
 			
-			btHingeConstraint *btConstraint;
+			btHingeConstraint *btHingeConstraint;
 	};
+    
+    class _PolyExport PhysicsPointToPointConstraint : public PhysicsConstraint {
+        public:
+            btPoint2PointConstraint *btPointToPointConstraint;
+    };
 
 	/**
 	* A scene subclass that simulates physics for its children.
@@ -116,7 +122,9 @@ namespace Polycode {
 		PhysicsCharacter *trackCharacterChild(Entity *newEntity, Number mass, Number friction, Number stepSize, int group  = 1);
         
 		void removeCharacterChild(PhysicsCharacter *character);
-		
+
+        PhysicsPointToPointConstraint *createPointToPointConstraint(Entity *entity1, Entity *entity2, const Vector3 &pivot1, const Vector3 &pivot2);
+        
 		PhysicsHingeConstraint *createHingeConstraint(Entity *entity, Vector3 pivot, Vector3 axis, Number minLimit, Number maxLimit);
 
 		PhysicsHingeConstraint *createHingeJoint(Entity *entity1, Entity *entity2, Vector3 pivot1, Vector3 axis1, Vector3 pivot2, Vector3 axis2, Number minLimit, Number maxLimit);
@@ -128,7 +136,7 @@ namespace Polycode {
 				
 		void warpEntity(Entity *entity, Vector3 position, bool resetRotation = false);
 		
-		void removeConstraint(PhysicsHingeConstraint *constraint);
+		void removeConstraint(PhysicsConstraint *constraint);
 		
 		void applyImpulse(Entity *entity, Vector3 force, Vector3 point);
 		

+ 29 - 0
Modules/Contents/3DPhysics/Source/PolyPhysicsConstraint.cpp

@@ -0,0 +1,29 @@
+/*
+Copyright (C) 2015 by Ivan Safrin
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include "PolyPhysicsConstraint.h"
+
+using namespace Polycode;
+
+PhysicsConstraint::~PhysicsConstraint() {
+    delete btConstraint;
+}

+ 38 - 18
Modules/Contents/3DPhysics/Source/PolyPhysicsScene.cpp

@@ -313,40 +313,37 @@ void PhysicsScene::removeEntity(Entity *entity) {
 	}
 }
 
-void PhysicsScene::removeConstraint(PhysicsHingeConstraint *constraint) {
-	physicsWorld->removeConstraint(constraint->btConstraint);
-}
 
-PhysicsHingeConstraint::~PhysicsHingeConstraint() {
-	delete btConstraint;
+void PhysicsScene::removeConstraint(PhysicsConstraint *constraint) {
+	physicsWorld->removeConstraint(constraint->btConstraint);
 }
 
 void PhysicsHingeConstraint::setLimits(Number minLimit, Number maxLimit) {
-	btConstraint->setLimit(minLimit, maxLimit);
+	btHingeConstraint->setLimit(minLimit, maxLimit);
 }
 
 Number PhysicsHingeConstraint::getAngle() {
-	return btConstraint->getHingeAngle();
+	return btHingeConstraint->getHingeAngle();
 }
 
 void PhysicsGenericConstraint::setLinearLowerLimit(Vector3 limit) {
 	btVector3 btLimit = btVector3(limit.x, limit.y, limit.z);
-	btConstraint->setLinearLowerLimit(btLimit);
+	btGenericConstraint->setLinearLowerLimit(btLimit);
 }
 
 void PhysicsGenericConstraint::setLinearUpperLimit(Vector3 limit) {
 	btVector3 btLimit = btVector3(limit.x, limit.y, limit.z);
-	btConstraint->setLinearUpperLimit(btLimit);
+	btGenericConstraint->setLinearUpperLimit(btLimit);
 }
 
 void PhysicsGenericConstraint::setAngularLowerLimit(Vector3 limit) {
 	btVector3 btLimit = btVector3(limit.x, limit.y, limit.z);
-	btConstraint->setAngularLowerLimit(btLimit);
+	btGenericConstraint->setAngularLowerLimit(btLimit);
 }
 
 void PhysicsGenericConstraint::setAngularUpperLimit(Vector3 limit) {
 	btVector3 btLimit = btVector3(limit.x, limit.y, limit.z);
-	btConstraint->setAngularUpperLimit(btLimit);
+	btGenericConstraint->setAngularUpperLimit(btLimit);
 }
 
 PhysicsGenericConstraint *PhysicsScene::createGenericConstraint(Entity *entity) {
@@ -361,10 +358,33 @@ PhysicsGenericConstraint *PhysicsScene::createGenericConstraint(Entity *entity)
 	btTransform frame;
 	frame.setIdentity();
 	
-	constraint->btConstraint = new btGeneric6DofConstraint(*pEnt->rigidBody, frame, true);
+	constraint->btGenericConstraint = new btGeneric6DofConstraint(*pEnt->rigidBody, frame, true);
+    constraint->btConstraint = constraint->btGenericConstraint;
 	physicsWorld->addConstraint(constraint->btConstraint);
-	
-	return constraint;
+    return constraint;
+}
+
+PhysicsPointToPointConstraint *PhysicsScene::createPointToPointConstraint(Entity *entity1, Entity *entity2, const Vector3 &pivot1, const Vector3 &pivot2) {
+    
+    PhysicsPointToPointConstraint *constraint = new PhysicsPointToPointConstraint();
+    
+    PhysicsEntity *pEnt1 = getPhysicsEntityByEntity(entity1);
+    PhysicsEntity *pEnt2 = getPhysicsEntityByEntity(entity2);
+    
+    if(!pEnt1 || !pEnt2) {
+        return NULL;
+    }
+    
+    btVector3 btPivot1 = btVector3(pivot1.x, pivot1.y, pivot1.z);
+    btVector3 btPivot2 = btVector3(pivot2.x, pivot2.y, pivot2.z);
+    
+    constraint->btPointToPointConstraint = new btPoint2PointConstraint(*pEnt1->rigidBody,
+                            *pEnt2->rigidBody,
+                            btPivot1,
+                            btPivot2);
+    constraint->btConstraint = constraint->btPointToPointConstraint;
+	physicsWorld->addConstraint(constraint->btConstraint);
+    return constraint;
 }
 
 PhysicsHingeConstraint * PhysicsScene::createHingeConstraint(Entity *entity, Vector3 pivot, Vector3 axis, Number minLimit, Number maxLimit) {
@@ -379,10 +399,11 @@ PhysicsHingeConstraint * PhysicsScene::createHingeConstraint(Entity *entity, Vec
 	
 	btHingeConstraint *hingeConstraint = new btHingeConstraint( *pEnt->rigidBody, btPivot, btAxis );
 	hingeConstraint->setLimit(minLimit, maxLimit);
-	physicsWorld->addConstraint(hingeConstraint);
 	
-	constraint->btConstraint = hingeConstraint;
+	constraint->btHingeConstraint = hingeConstraint;
 	
+    constraint->btConstraint = constraint->btHingeConstraint;
+	physicsWorld->addConstraint(hingeConstraint);
 	return constraint;
 }
 
@@ -404,10 +425,9 @@ PhysicsHingeConstraint *PhysicsScene::createHingeJoint(Entity *entity1, Entity *
 
 	btHingeConstraint *hingeConstraint = new btHingeConstraint(*pEnt1->rigidBody, *pEnt2->rigidBody, btPivot1, btPivot2, btAxis1, btAxis2 );
 	hingeConstraint->setLimit(minLimit, maxLimit);
-	physicsWorld->addConstraint(hingeConstraint);
 	
 	constraint->btConstraint = hingeConstraint;
-	
+	physicsWorld->addConstraint(hingeConstraint);
 	return constraint;	
 	
 }