From c25fdf3823d8e269b90a09c086bc3a7efb14928b Mon Sep 17 00:00:00 2001 From: FatBoy-DOTC Date: Fri, 5 Jan 2024 19:21:24 -0600 Subject: [PATCH] added COllisionManager --- src/engine/collision/Mesh.java | 4 +-- src/engine/gameManager/BuildingManager.java | 2 -- src/engine/gameManager/CollisionManager.java | 27 ++++++++++++++++ .../client/handlers/MoveToPointHandler.java | 31 +++++-------------- 4 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 src/engine/gameManager/CollisionManager.java diff --git a/src/engine/collision/Mesh.java b/src/engine/collision/Mesh.java index f0a8868d..fd84424d 100644 --- a/src/engine/collision/Mesh.java +++ b/src/engine/collision/Mesh.java @@ -26,14 +26,14 @@ public class Mesh { return false; //character moving is higher than the max Y of this mesh - if(charHeight < this.meshHeight) + if(charHeight > this.meshHeight) return false; for(Triangle tri : triangles) if(tri.collides(line)) return true; - //characters movement path did not intersect this triangle + //characters movement path did not intersect this mesh return false; } } diff --git a/src/engine/gameManager/BuildingManager.java b/src/engine/gameManager/BuildingManager.java index ddcf6580..a339f36a 100644 --- a/src/engine/gameManager/BuildingManager.java +++ b/src/engine/gameManager/BuildingManager.java @@ -30,7 +30,6 @@ import engine.objects.*; import engine.server.MBServerStatics; import org.pmw.tinylog.Logger; -import java.awt.*; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; @@ -1042,5 +1041,4 @@ public enum BuildingManager { Logger.info("Failed To Bake Building Mesh Data For Structure: " + building.meshUUID); } } - } diff --git a/src/engine/gameManager/CollisionManager.java b/src/engine/gameManager/CollisionManager.java new file mode 100644 index 00000000..ab6c30b2 --- /dev/null +++ b/src/engine/gameManager/CollisionManager.java @@ -0,0 +1,27 @@ +package engine.gameManager; + +import engine.collision.Mesh; +import engine.objects.Building; + +import java.awt.*; +import java.awt.geom.Line2D; + +public class CollisionManager { + public static boolean CollisionDetected(Building building, Line2D travelLine, float charHeight){ + Rectangle.Float boundsRect = new Rectangle.Float(); + float rectCenterX = building.loc.x + building.getBounds().getHalfExtents().x; + float rectCenterZ = building.loc.z + building.getBounds().getHalfExtents().y; + boundsRect.setRect(rectCenterX, rectCenterZ, building.getBounds().getHalfExtents().x * 2,building.getBounds().getHalfExtents().y * 2); + if(travelLine.intersects(boundsRect)){ + //collided with building + for(Mesh mesh : building.buildingMeshes) { + if (mesh.MeshCollides(travelLine,charHeight)) { + //ChatManager.chatSystemInfo(pc, "Collision Detected"); + //MovementManager.movement(msg, pc); + return true; + } + } + } + return false; + } +} diff --git a/src/engine/net/client/handlers/MoveToPointHandler.java b/src/engine/net/client/handlers/MoveToPointHandler.java index bae389a1..a1e45a68 100644 --- a/src/engine/net/client/handlers/MoveToPointHandler.java +++ b/src/engine/net/client/handlers/MoveToPointHandler.java @@ -9,15 +9,11 @@ package engine.net.client.handlers; -import engine.Enum; import engine.InterestManagement.WorldGrid; -import engine.collision.Mesh; -import engine.collision.Triangle; import engine.exception.MsgSendException; -import engine.gameManager.BuildingManager; import engine.gameManager.ChatManager; +import engine.gameManager.CollisionManager; import engine.gameManager.MovementManager; -import engine.math.Vector3f; import engine.math.Vector3fImmutable; import engine.net.client.ClientConnection; import engine.net.client.msg.ClientNetMsg; @@ -25,7 +21,6 @@ import engine.net.client.msg.MoveToPointMsg; import engine.objects.*; import engine.server.MBServerStatics; -import java.awt.*; import java.awt.geom.Line2D; import java.util.HashSet; @@ -49,29 +44,17 @@ public class MoveToPointHandler extends AbstractClientMsgHandler { Vector3fImmutable endLoc = new Vector3fImmutable(msg.getEndLat(),msg.getEndAlt(),msg.getEndLon()); travelLine.setLine(pc.loc.x,pc.loc.z,endLoc.x,endLoc.z); float movementDistance = 185 + pc.loc.distance(endLoc); - boolean collided = false; HashSet awoList = WorldGrid.getObjectsInRangePartial(pc.loc, movementDistance, MBServerStatics.MASK_BUILDING); for(AbstractWorldObject awo : awoList){ Building building = (Building)awo; - Rectangle.Float boundsRect = new Rectangle.Float(); - float rectCenterX = building.loc.x + building.getBounds().getHalfExtents().x; - float rectCenterZ = building.loc.z + building.getBounds().getHalfExtents().y; - boundsRect.setRect(rectCenterX, rectCenterZ, building.getBounds().getHalfExtents().x * 2,building.getBounds().getHalfExtents().y * 2); - if(travelLine.intersects(boundsRect)){ - //collided with building - for(Mesh mesh : building.buildingMeshes) { - if (mesh.MeshCollides(travelLine,pc.getCharacterHeight())) - for (Triangle tri : mesh.triangles) - if (tri.collides(travelLine)){ - ChatManager.chatSystemInfo(pc, "Collision Detected"); - MovementManager.movement(msg, pc); - return true; - } - - } + if(CollisionManager.CollisionDetected(building, travelLine,pc.getCharacterHeight())){ + ChatManager.chatSystemInfo(pc, "Collision Detected"); + //msg.setEndCoord(); + MovementManager.movement(msg, pc); + return true; } } - ChatManager.chatSystemInfo(pc, "No Collision Detected"); + ChatManager.chatSystemInfo(pc, "No Collision Detected"); MovementManager.movement(msg, pc); return true; }