vlod 1 year ago
parent
commit
bd5d3d18ed

+ 13 - 8
Pika/gameplay/containers/physicsTest/physicsTest.h

@@ -37,7 +37,7 @@ struct PhysicsTest: public Container
 	{
 		renderer.create(requestedInfo.requestedFBO.fbo);
 	
-		for (int i = 0; i < 20; i++)
+		for (int i = 0; i < 0; i++)
 		{
 			//if (i == 1) { mass = 0; }
 
@@ -51,7 +51,7 @@ struct PhysicsTest: public Container
 				physicsEngine.bodies.back().motionState.rotation = ((rand() % 800) / 800.f) * 3.14159f;
 			}
 
-			for (int j = 0; j < 2; j++)
+			for (int j = 0; j < 1; j++)
 			{
 				float r = rand() % 35 + 10;
 
@@ -67,14 +67,17 @@ struct PhysicsTest: public Container
 		//physicsEngine.addBody({700, 700}, ph2d::createBoxCollider({300, 300}));
 
 
-		//physicsEngine.addBody({600, 600}, ph2d::createBoxCollider({350, 100}));
+		//physicsEngine.addBody({600, 600}, ph2d::createBoxCollider({350, 350}));
 		//physicsEngine.bodies[1].motionState.rotation = glm::radians(30.f);
 
 		//physicsEngine.addBody({500, 500}, ph2d::createCircleCollider({75}));
 		//physicsEngine.addBody({800, 100}, ph2d::createCircleCollider({55}));
-		//physicsEngine.addBody({900, 500}, ph2d::createCircleCollider({25}));
+		physicsEngine.addBody({900, 500}, ph2d::createCircleCollider({40}));
 		//physicsEngine.addBody({550, 700}, ph2d::createCircleCollider({25}));
 
+		//physicsEngine.addBody({600, 600}, ph2d::createBoxCollider({50, 50}));
+
+
 		//std::cout << ph2d::vectorToRotation({0,1}) << "\n";
 		//std::cout << ph2d::vectorToRotation({-1,1}) << "\n";
 		//std::cout << ph2d::vectorToRotation({-1,0}) << "\n";
@@ -88,7 +91,7 @@ struct PhysicsTest: public Container
 		//std::cout << ph2d::rotationToVector(ph2d::vectorToRotation({0,-1})).x << " " << ph2d::rotationToVector(ph2d::vectorToRotation({0,-1})).y << "\n";
 		//std::cout << ph2d::rotationToVector(ph2d::vectorToRotation({1,0}) ).x << " " << ph2d::rotationToVector(ph2d::vectorToRotation({1,0}) ).y  << "\n";
 
-		physicsEngine.addHalfSpaceStaticObject({0, floorPos}, {0.1, 1});
+		physicsEngine.addHalfSpaceStaticObject({0, floorPos}, {0.2, 1});
 		//physicsEngine.addBody({500, floorPos}, ph2d::createBoxCollider({900, 50}));
 		//physicsEngine.bodies.back().motionState.mass = 0;
 		//physicsEngine.bodies.back().motionState.momentOfInertia = 0;
@@ -147,7 +150,9 @@ struct PhysicsTest: public Container
 
 		ImGui::DragInt("Speed", &simulationSpeed);
 
-		ImGui::SliderAngle("ANgle", &physicsEngine.bodies[0].motionState.rotation);
+		ImGui::SliderAngle("Angle", &physicsEngine.bodies[0].motionState.rotation);
+		ImGui::SliderAngle("angular velocity", &physicsEngine.bodies[0].motionState.angularVelocity);
+		//physicsEngine.bodies[0].motionState.angularVelocity = 1.5;
 
 		ImGui::Checkbox("Simulate", &simulate);
 
@@ -195,8 +200,8 @@ struct PhysicsTest: public Container
 			force *= 2.f;
 
 			physicsEngine.bodies[selected].motionState.applyImpulseWorldPosition(force,
-				physicsEngine.bodies[selected].motionState.pos
-				//pressedPosition
+				//physicsEngine.bodies[selected].motionState.pos
+				pressedPosition
 			);
 
 			//physicsEngine.bodies[selected].motionState.angularVelocity = 10;

+ 2 - 2
Pika/resources/logs.txt

@@ -1,2 +1,2 @@
-#2024-11-21 16:21:17: Created container: PhysicsTest
-#2024-11-21 16:22:30: Destroyed continer: PhysicsTest #1
+#2024-11-21 18:35:18: Created container: PhysicsTest
+#2024-11-21 18:35:27: Destroyed continer: PhysicsTest #1

+ 2 - 2
Pika/thirdparty/ph2d/include/ph2d/ph2d.h

@@ -242,8 +242,8 @@ namespace ph2d
 		Collider collider = {};
 
 		float elasticity = 0.2;
-		float staticFriction = 0.5;
-		float dynamicFriction = 0.4;
+		float staticFriction = 0.7;
+		float dynamicFriction = 0.6;
 
 		AABB getAABB();
 

+ 22 - 13
Pika/thirdparty/ph2d/src/ph2d.cpp

@@ -943,7 +943,7 @@ namespace ph2d
 		switch (type)
 		{
 			case ColliderCircle:
-			return (collider.circle.radius * collider.circle.radius * 3.1415);
+			return (collider.circle.radius * collider.circle.radius * 3.1415926);
 			
 			case ColliderBox:
 			return collider.box.size.x * collider.box.size.y;
@@ -1015,7 +1015,7 @@ void ph2d::MotionState::applyImpulseObjectPosition(glm::vec2 impulse, glm::vec2
 
 	if (momentOfInertia != 0 && momentOfInertia != INFINITY)
 	{
-		angularVelocity -= (1.0f / momentOfInertia) * cross(contactVector, impulse) * 0.95f;
+		angularVelocity -= (1.0f / momentOfInertia) * cross(contactVector, impulse) * 0.99f;
 	}
 }
 
@@ -1083,17 +1083,25 @@ void ph2d::PhysicsEngine::runSimulation(float deltaTime)
 			if (A.motionState.momentOfInertia == 0 || A.motionState.momentOfInertia == INFINITY) { momentOfInertiaInverseA = 0; }
 			if (B.motionState.momentOfInertia == 0 || B.motionState.momentOfInertia == INFINITY) { momentOfInertiaInverseB = 0; }
 
+			float jt = -glm::dot(rv, tangent);
+
+			float inertiaDivisorA = std::pow(cross(rContactA, tangent), 2) * momentOfInertiaInverseA;
+			float inertiaDivisorB = std::pow(cross(rContactB, tangent), 2) * momentOfInertiaInverseB;
+
+			if (aInverseMass == 0 && bInverseMass == 0
+				&& inertiaDivisorA == 0 && inertiaDivisorB == 0
+				)
+			{
+				//nothing will move, no need to compute anything
+				return;
+			}
+
 			//remove moment of inertia
 			//momentOfInertiaInverseA = 0;
 			//momentOfInertiaInverseB = 0;
 
 			// Solve for magnitude to apply along the friction vector
-			float jt = -glm::dot(rv, tangent);
-			jt = jt / (aInverseMass + bInverseMass)
-				+
-				(std::pow(cross(rContactA, rv), 2) * momentOfInertiaInverseA) +
-				(std::pow(cross(rContactB, rv), 2) * momentOfInertiaInverseB)
-				;
+			jt = jt / (aInverseMass + bInverseMass + inertiaDivisorA + inertiaDivisorB);
 
 			// PythagoreanSolve = A^2 + B^2 = C^2, solving for C given A and B
 			// Use to approximate mu given friction coefficients of each body
@@ -1116,8 +1124,8 @@ void ph2d::PhysicsEngine::runSimulation(float deltaTime)
 			//A.motionState.velocity -= (aInverseMass)*frictionImpulse;
 			//B.motionState.velocity += (bInverseMass)*frictionImpulse;
 
-			//A.motionState.applyImpulseWorldPosition(-frictionImpulse, contactPoint);
-			//B.motionState.applyImpulseWorldPosition( frictionImpulse, contactPoint);
+			A.motionState.applyImpulseWorldPosition(-frictionImpulse, contactPoint);
+			B.motionState.applyImpulseWorldPosition( frictionImpulse, contactPoint);
 
 
 		};
@@ -1181,11 +1189,12 @@ void ph2d::PhysicsEngine::runSimulation(float deltaTime)
 				// Re-calculate relative velocity after normal impulse
 				// is applied (impulse from first article, this code comes
 				// directly thereafter in the same resolve function)
-
-				glm::vec2 rv = B.motionState.velocity - A.motionState.velocity;
+				// rv also holds the rotational velocity contribution
+				glm::vec2 rv = (B.motionState.velocity + cross(-B.motionState.angularVelocity, rContactB)) -
+					(A.motionState.velocity + cross(-A.motionState.angularVelocity, rContactA));
 
 				// Solve for the tangent vector
-				glm::vec2 tangent = rv - glm::dot(rv, normal) * normal;
+				glm::vec2 tangent = (rv - glm::dot(rv, normal) * normal);
 
 				float tangentSize = glm::length(tangent);