Browse Source

fps controll

vlod 7 months ago
parent
commit
b199950d90
3 changed files with 171 additions and 51 deletions
  1. 127 32
      Pika/gameplay/containers/milk/milk.h
  2. 7 6
      Pika/gameplay/containers/milk/physics.cpp
  3. 37 13
      Pika/resources/logs.txt

+ 127 - 32
Pika/gameplay/containers/milk/milk.h

@@ -54,6 +54,9 @@ struct Milk: public Container
 
 	gl3d::Model createCubeModel(glm::vec3 size);
 
+	int playerPhysics = 0;
+	glm::vec3 playerSize = {0.5, 1.8, 0.5};
+
 
 	void addNewCube(glm::vec3 size, glm::vec3 position, glm::vec3 rotation = {})
 	{
@@ -148,6 +151,40 @@ struct Milk: public Container
 
 	}
 
+	void load(RequestedContainerInfo &requestedInfo)
+	{
+		sfs::SafeSafeKeyValueData loadedData;
+		std::vector<char> binaryData;
+
+		requestedInfo.readEntireFileBinary(PIKA_RESOURCES_PATH "milk/level.bin", binaryData);
+
+		loadedData.loadFromFileData(binaryData.data(), binaryData.size());
+
+		while (cubes.size())
+		{
+			deleteCube(cubes.size() - 1);
+		}
+
+		for (auto &e : loadedData.entries)
+		{
+			sfs::SafeSafeKeyValueData cube;
+
+			if (loadedData.getKeyValueData(e.first, cube) == sfs::noError)
+			{
+				glm::vec3 size = {};
+				glm::vec3 position = {};
+				glm::vec3 rotation = {};
+
+				cube.getVec3("size", size.x, size.y, size.z);
+				cube.getVec3("position", position.x, position.y, position.z);
+				cube.getVec3("rotation", rotation.x, rotation.y, rotation.z);
+
+				addNewCube(size, position, rotation);
+			}
+
+		}
+	}
+
 	bool create(RequestedContainerInfo &requestedInfo, pika::StaticString<256> commandLineArgument)
 	{
 	
@@ -205,7 +242,24 @@ struct Milk: public Container
 		milkModel = renderer.loadModel(PIKA_RESOURCES_PATH "milk/milk.glb", gl3d::TextureLoadQuality::maxQuality, 1);
 		milkEntity = renderer.createEntity(milkModel, {{66.9,4.9,-9.5}}, false);
 
+		//optional make milk bottle shiny
+		//gl3d::MaterialValues mat;
+		//mat.roughness = 0.1;
+		//renderer.setEntityMeshMaterialValues(milkEntity, 0, mat);
+		//renderer.setEntityMeshMaterialValues(milkEntity, 1, mat);
 
+		load(requestedInfo);
+
+		{
+			auto box = createBox({0, 2, 0}, playerSize);
+			box.mass = 50;
+			box.bouncyness = 0;
+
+			playerPhysics = simulator.getIdAndIncrement();
+
+			simulator.bodies.insert({playerPhysics, box});
+		}
+		
 
 		return true;
 	}
@@ -230,7 +284,67 @@ struct Milk: public Container
 		renderer.camera.aspectRatio = (float)windowState.windowW / windowState.windowH; //todo do this in update
 
 		
-		editor.update(requestedInfo.requestedImguiIds, renderer, input, 5, requestedInfo, {windowState.windowW,windowState.windowH});
+		editor.update(requestedInfo.requestedImguiIds, renderer, input, 0, requestedInfo, {windowState.windowW,windowState.windowH});
+
+		auto &playerObject = simulator.bodies[playerPhysics];
+
+		{
+			float speed = 10;
+			glm::vec3 dir = {};
+			if (input.buttons[pika::Button::W].held())
+			{
+				dir.z -= speed * input.deltaTime;
+			}
+			if (input.buttons[pika::Button::S].held())
+			{
+				dir.z += speed * input.deltaTime;
+			}
+
+			if (input.buttons[pika::Button::A].held())
+			{
+				dir.x -= speed * input.deltaTime;
+			}
+			if (input.buttons[pika::Button::D].held())
+			{
+				dir.x += speed * input.deltaTime;
+			}
+
+			//if (input.buttons[pika::Button::Q].held())
+			//{
+			//	dir.y -= speed * input.deltaTime;
+			//}
+			//if (input.buttons[pika::Button::E].held())
+			//{
+			//	dir.y += speed * input.deltaTime;
+			//}
+
+			if (dir.x != 0 || dir.y != 0 || dir.z != 0)
+			{
+				glm::vec3 viewDirection = glm::normalize(renderer.camera.viewDirection);
+
+				//forward
+				float forward = -dir.z;
+				float leftRight = dir.x;
+				float upDown = dir.y;
+
+				glm::vec3 move = {};
+
+				move += glm::vec3(0, 1, 0) * upDown;
+				move += glm::normalize(glm::cross(viewDirection, glm::vec3(0, 1, 0))) * leftRight;
+				move += viewDirection * forward;
+
+				playerObject.velocity += move;
+			}
+
+			if (input.buttons[pika::Button::Space].pressed())
+			{
+				playerObject.velocity.y += 10.f;
+			}
+
+		}
+
+		renderer.camera.position = playerObject.position + glm::vec3(0, 0.7, 0);
+
 
 		if (input.buttons[pika::Button::Escape].released())
 		{
@@ -295,37 +409,7 @@ struct Milk: public Container
 
 			if (ImGui::Button("Load"))
 			{
-				sfs::SafeSafeKeyValueData loadedData;
-				std::vector<char> binaryData;
-
-				requestedInfo.readEntireFileBinary(PIKA_RESOURCES_PATH "milk/level.bin", binaryData);
-
-				loadedData.loadFromFileData(binaryData.data(), binaryData.size());
-
-				while (cubes.size())
-				{
-					deleteCube(cubes.size() - 1);
-				}
-
-				for (auto &e : loadedData.entries)
-				{
-					sfs::SafeSafeKeyValueData cube;
-
-					if (loadedData.getKeyValueData(e.first, cube) == sfs::noError)
-					{
-						glm::vec3 size = {};
-						glm::vec3 position = {};
-						glm::vec3 rotation = {};
-
-						cube.getVec3("size", size.x, size.y, size.z);
-						cube.getVec3("position", position.x, position.y, position.z);
-						cube.getVec3("rotation", rotation.x, rotation.y, rotation.z);
-
-						addNewCube(size, position, rotation);
-					}
-
-				}
-
+				load(requestedInfo);
 			}
 
 			ImGui::Separator();
@@ -406,6 +490,17 @@ struct Milk: public Container
 			}
 		}
 
+		for (auto &c : cubes)
+		{
+			auto found = simulator.bodies.find(c.physicsID);
+			if (found != simulator.bodies.end())
+			{
+				auto transform = renderer.getEntityTransform(c.entity);
+				transform.position = found->second.position;
+				renderer.setEntityTransform(c.entity, transform);
+			}
+		}
+
 		{
 			static float timer = 0;
 			static glm::vec3 posOriginal = {};

+ 7 - 6
Pika/gameplay/containers/milk/physics.cpp

@@ -178,8 +178,8 @@ void positionalCorrection(PhysicsObject &A, PhysicsObject &B, glm::vec3 n,
 
 	glm::vec3 correction = (glm::max(penetrationDepth - slop, 0.0f) / (aInverseMass + bInverseMass)) * percent * n;
 
-	A.position -= aInverseMass * correction;
-	B.position += bInverseMass * correction;
+	if(aInverseMass) A.position -= aInverseMass * correction;
+	if(bInverseMass) B.position += bInverseMass * correction;
 };
 
 float pythagoreanSolve(float fA, float fB)
@@ -212,8 +212,8 @@ void applyFriction(PhysicsObject &A, PhysicsObject &B, glm::vec3 tangent, glm::v
 	}
 
 	// Apply
-	A.velocity -= (aInverseMass)*frictionImpulse;
-	B.velocity += (bInverseMass)*frictionImpulse;
+	if(aInverseMass) A.velocity -= (aInverseMass)*frictionImpulse;
+	if(bInverseMass) B.velocity += (bInverseMass)*frictionImpulse;
 
 };
 
@@ -238,8 +238,8 @@ void impulseResolution(PhysicsObject &A, PhysicsObject &B, glm::vec3 normal,
 
 	// Apply impulse
 	glm::vec3 impulse = j * normal;
-	A.velocity -= massInverseA * impulse;
-	B.velocity += massInverseB * impulse;
+	if(massInverseA) A.velocity -= massInverseA * impulse;
+	if(massInverseB) B.velocity += massInverseB * impulse;
 
 	positionalCorrection(A, B, normal, penetrationDepth, massInverseA, massInverseB);
 
@@ -668,6 +668,7 @@ void Simulator::update(float deltaTime)
 		//gravity
 		b.acceleration += glm::vec3{0, -9.81, 0};
 
+		if(b.mass != 0 && b.mass != INFINITY)
 		applyDrag(b);
 
 		//detect colisions

+ 37 - 13
Pika/resources/logs.txt

@@ -1,13 +1,37 @@
-#2025-05-13 11:35:44: Created container: Milk
-#2025-05-13 11:36:34[warning]: Couldn't reloaded dll
-#2025-05-13 11:36:36: Reloaded dll
-#2025-05-13 11:37:46[warning]: Couldn't reloaded dll
-#2025-05-13 11:37:49[warning]: Couldn't reloaded dll
-#2025-05-13 11:37:49: Reloaded dll
-#2025-05-13 11:38:24[warning]: Couldn't reloaded dll
-#2025-05-13 11:38:26[warning]: Couldn't reloaded dll
-#2025-05-13 11:38:27: Reloaded dll
-#2025-05-13 11:39:13: Destroyed continer: Milk #1
-#2025-05-13 11:39:20[warning]: Couldn't reloaded dll
-#2025-05-13 11:39:23[warning]: Couldn't reloaded dll
-#2025-05-13 11:39:23: Reloaded dll
+#2025-05-16 19:28:49: Created container: Milk
+#2025-05-16 19:32:53[warning]: Couldn't reloaded dll
+#2025-05-16 19:32:56: Reloaded dll
+#2025-05-16 19:33:04: Destroyed continer: Milk #1
+#2025-05-16 19:33:18[warning]: Couldn't reloaded dll
+#2025-05-16 19:33:20: Reloaded dll
+#2025-05-16 19:33:23: Created container: Milk
+#2025-05-16 19:33:28: Destroyed continer: Milk #2
+#2025-05-16 19:33:43[warning]: Couldn't reloaded dll
+#2025-05-16 19:33:45[warning]: Couldn't reloaded dll
+#2025-05-16 19:33:46: Reloaded dll
+#2025-05-16 19:33:49: Created container: Milk
+#2025-05-16 19:34:35[warning]: Couldn't reloaded dll
+#2025-05-16 19:34:37[warning]: Couldn't reloaded dll
+#2025-05-16 19:34:37: Reloaded dll
+#2025-05-16 19:34:55: Destroyed continer: Milk #3
+#2025-05-16 19:34:57: Created container: Milk
+#2025-05-16 19:35:03: Destroyed continer: Milk #4
+#2025-05-16 19:35:28[warning]: Couldn't reloaded dll
+#2025-05-16 19:35:30: Reloaded dll
+#2025-05-16 19:35:35: Created container: Milk
+#2025-05-16 19:36:36[warning]: Couldn't reloaded dll
+#2025-05-16 19:36:39: Reloaded dll
+#2025-05-16 19:37:37[warning]: Couldn't reloaded dll
+#2025-05-16 19:37:39[warning]: Couldn't reloaded dll
+#2025-05-16 19:37:40: Reloaded dll
+#2025-05-16 19:38:12[warning]: Couldn't reloaded dll
+#2025-05-16 19:38:15[warning]: Couldn't reloaded dll
+#2025-05-16 19:38:15: Reloaded dll
+#2025-05-16 19:38:45: Destroyed continer: Milk #5
+#2025-05-16 19:39:18[warning]: Couldn't reloaded dll
+#2025-05-16 19:39:20: Reloaded dll
+#2025-05-16 19:39:23: Created container: Milk
+#2025-05-16 19:40:03[warning]: Couldn't reloaded dll
+#2025-05-16 19:40:05[warning]: Couldn't reloaded dll
+#2025-05-16 19:40:05: Reloaded dll
+#2025-05-16 19:40:50: Destroyed continer: Milk #6