Browse Source

added COllisionManager

server-collision
FatBoy-DOTC 12 months ago
parent
commit
c25fdf3823
  1. 4
      src/engine/collision/Mesh.java
  2. 2
      src/engine/gameManager/BuildingManager.java
  3. 27
      src/engine/gameManager/CollisionManager.java
  4. 31
      src/engine/net/client/handlers/MoveToPointHandler.java

4
src/engine/collision/Mesh.java

@ -26,14 +26,14 @@ public class Mesh {
return false; return false;
//character moving is higher than the max Y of this mesh //character moving is higher than the max Y of this mesh
if(charHeight < this.meshHeight) if(charHeight > this.meshHeight)
return false; return false;
for(Triangle tri : triangles) for(Triangle tri : triangles)
if(tri.collides(line)) if(tri.collides(line))
return true; return true;
//characters movement path did not intersect this triangle //characters movement path did not intersect this mesh
return false; return false;
} }
} }

2
src/engine/gameManager/BuildingManager.java

@ -30,7 +30,6 @@ import engine.objects.*;
import engine.server.MBServerStatics; import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
import java.awt.*;
import java.awt.geom.Line2D; import java.awt.geom.Line2D;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
@ -1042,5 +1041,4 @@ public enum BuildingManager {
Logger.info("Failed To Bake Building Mesh Data For Structure: " + building.meshUUID); Logger.info("Failed To Bake Building Mesh Data For Structure: " + building.meshUUID);
} }
} }
} }

27
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;
}
}

31
src/engine/net/client/handlers/MoveToPointHandler.java

@ -9,15 +9,11 @@
package engine.net.client.handlers; package engine.net.client.handlers;
import engine.Enum;
import engine.InterestManagement.WorldGrid; import engine.InterestManagement.WorldGrid;
import engine.collision.Mesh;
import engine.collision.Triangle;
import engine.exception.MsgSendException; import engine.exception.MsgSendException;
import engine.gameManager.BuildingManager;
import engine.gameManager.ChatManager; import engine.gameManager.ChatManager;
import engine.gameManager.CollisionManager;
import engine.gameManager.MovementManager; import engine.gameManager.MovementManager;
import engine.math.Vector3f;
import engine.math.Vector3fImmutable; import engine.math.Vector3fImmutable;
import engine.net.client.ClientConnection; import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg; import engine.net.client.msg.ClientNetMsg;
@ -25,7 +21,6 @@ import engine.net.client.msg.MoveToPointMsg;
import engine.objects.*; import engine.objects.*;
import engine.server.MBServerStatics; import engine.server.MBServerStatics;
import java.awt.*;
import java.awt.geom.Line2D; import java.awt.geom.Line2D;
import java.util.HashSet; import java.util.HashSet;
@ -49,29 +44,17 @@ public class MoveToPointHandler extends AbstractClientMsgHandler {
Vector3fImmutable endLoc = new Vector3fImmutable(msg.getEndLat(),msg.getEndAlt(),msg.getEndLon()); Vector3fImmutable endLoc = new Vector3fImmutable(msg.getEndLat(),msg.getEndAlt(),msg.getEndLon());
travelLine.setLine(pc.loc.x,pc.loc.z,endLoc.x,endLoc.z); travelLine.setLine(pc.loc.x,pc.loc.z,endLoc.x,endLoc.z);
float movementDistance = 185 + pc.loc.distance(endLoc); float movementDistance = 185 + pc.loc.distance(endLoc);
boolean collided = false;
HashSet<AbstractWorldObject> awoList = WorldGrid.getObjectsInRangePartial(pc.loc, movementDistance, MBServerStatics.MASK_BUILDING); HashSet<AbstractWorldObject> awoList = WorldGrid.getObjectsInRangePartial(pc.loc, movementDistance, MBServerStatics.MASK_BUILDING);
for(AbstractWorldObject awo : awoList){ for(AbstractWorldObject awo : awoList){
Building building = (Building)awo; Building building = (Building)awo;
Rectangle.Float boundsRect = new Rectangle.Float(); if(CollisionManager.CollisionDetected(building, travelLine,pc.getCharacterHeight())){
float rectCenterX = building.loc.x + building.getBounds().getHalfExtents().x; ChatManager.chatSystemInfo(pc, "Collision Detected");
float rectCenterZ = building.loc.z + building.getBounds().getHalfExtents().y; //msg.setEndCoord();
boundsRect.setRect(rectCenterX, rectCenterZ, building.getBounds().getHalfExtents().x * 2,building.getBounds().getHalfExtents().y * 2); MovementManager.movement(msg, pc);
if(travelLine.intersects(boundsRect)){ return true;
//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;
}
}
} }
} }
ChatManager.chatSystemInfo(pc, "No Collision Detected"); ChatManager.chatSystemInfo(pc, "No Collision Detected");
MovementManager.movement(msg, pc); MovementManager.movement(msg, pc);
return true; return true;
} }

Loading…
Cancel
Save