diff --git a/.gitignore b/.gitignore index eef246c0..8b161aed 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ replay_pid* *.idea/ Server.iml *.gitignore -prestonbane.iml \ No newline at end of file +prestonbane.iml +qodana.yaml \ No newline at end of file diff --git a/src/engine/gameManager/MovementManager.java b/src/engine/gameManager/MovementManager.java index f38b502e..ae5a9d9d 100644 --- a/src/engine/gameManager/MovementManager.java +++ b/src/engine/gameManager/MovementManager.java @@ -88,7 +88,7 @@ public enum MovementManager { // if (msg.getEndLat() < 0) // msg.setEndLat(0); -// +// // if (msg.getEndLon() > 0) // msg.setEndLon(0); @@ -216,6 +216,16 @@ public enum MovementManager { } + if (toMove.getObjectType().equals(GameObjectType.PlayerCharacter)) { + Vector3fImmutable playerCollidePoint = Bounds.PlayerCollisionPoint((PlayerCharacter) toMove, toMove.getLoc(), endLocation); + + if (playerCollidePoint != null) { + msg.setEndCoord(playerCollidePoint); + endLocation = playerCollidePoint; + collide = true; + } + } + if (toMove.getObjectType() == GameObjectType.PlayerCharacter && ((PlayerCharacter) toMove).isTeleportMode()) { toMove.teleport(endLocation); return; diff --git a/src/engine/math/Bounds.java b/src/engine/math/Bounds.java index 04998e1c..8be08bc4 100644 --- a/src/engine/math/Bounds.java +++ b/src/engine/math/Bounds.java @@ -81,7 +81,7 @@ public class Bounds { Bounds identityBounds = Bounds.borrow(); identityBounds.setBounds(location); - collisionState = collide(targetBounds, identityBounds, 0.0f); + collisionState = collide(targetBounds, identityBounds, 0.1f); identityBounds.release(); return collisionState; } @@ -267,6 +267,45 @@ public class Bounds { return collidePoint; } + public static Vector3fImmutable PlayerCollisionPoint(PlayerCharacter player, Vector3fImmutable start, Vector3fImmutable end) { + Vector3fImmutable collidePoint = null; + float distance = player.getLoc().distance2D(end); + + // Check for building collisions first + collidePoint = PlayerBuildingCollisionPoint(player, start, end); + if (collidePoint != null) { + return collidePoint; + } + + // Now check for player collisions + HashSet nearbyPlayers = WorldGrid.getObjectsInRangePartial(player, distance + 2, MBServerStatics.MASK_PLAYER); + float minDistance = 1.0f; // Minimum distance between players + + for (AbstractWorldObject awo : nearbyPlayers) { + PlayerCharacter otherPlayer = (PlayerCharacter) awo; + if (otherPlayer == player) continue; + + Vector3fImmutable otherLoc = otherPlayer.getLoc(); + Vector3fImmutable closestPoint = getClosestPointOnLine(start, end, otherLoc); + + if (closestPoint.distance2D(otherLoc) < minDistance) { + // Collision detected, adjust end point + Vector3fImmutable pushVector = closestPoint.subtract(otherLoc).normalize(); + collidePoint = closestPoint.add(pushVector.scaleAdd(minDistance, Vector3fImmutable.ZERO)); + break; + } + } + + return collidePoint != null ? collidePoint : end; + } + + private static Vector3fImmutable getClosestPointOnLine(Vector3fImmutable lineStart, Vector3fImmutable lineEnd, Vector3fImmutable point) { + Vector3fImmutable line = lineEnd.subtract(lineStart); + float t = point.subtract(lineStart).dot(line) / line.dot(line); + t = Math.max(0, Math.min(1, t)); + return lineStart.add(line.scaleAdd(t, Vector3fImmutable.ZERO)); + } + public static boolean linesTouching(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { float denominator = ((x2 - x1) * (y4 - y3)) - ((y2 - y1) * (x4 - x3)); float numerator1 = ((y1 - y3) * (x4 - x3)) - ((x1 - x3) * (y4 - y3)); diff --git a/src/engine/objects/PlayerCharacter.java b/src/engine/objects/PlayerCharacter.java index b7015c73..f80a7c91 100644 --- a/src/engine/objects/PlayerCharacter.java +++ b/src/engine/objects/PlayerCharacter.java @@ -26,7 +26,6 @@ import engine.job.JobScheduler; import engine.jobs.DeferredPowerJob; import engine.jobs.FinishSpireEffectJob; import engine.jobs.NoTimeJob; -import engine.math.Bounds; import engine.math.FastMath; import engine.math.Vector3fImmutable; import engine.net.ByteBufferWriter; @@ -174,7 +173,6 @@ public class PlayerCharacter extends AbstractCharacter { private boolean isTeleporting = false; private boolean dirtyLoad = true; private final ReadWriteLock dirtyLock = new ReentrantReadWriteLock(true); - private Bounds playerBounds; /** * No Id Constructor @@ -208,9 +206,8 @@ public class PlayerCharacter extends AbstractCharacter { this.guildStatus = new AtomicInteger(0); this.bindBuildingID = -1; - this.playerBounds = Bounds.borrow(); - playerBounds.setBounds(this.getLoc()); - this.playerBounds.setBounds(this); + Vector3fImmutable center = new Vector3fImmutable(0, 0, 0); + Vector3fImmutable extents = new Vector3fImmutable(0.5f, 1.0f, 0.5f); } /** @@ -274,58 +271,6 @@ public class PlayerCharacter extends AbstractCharacter { this.hash = rs.getString("hash"); - this.playerBounds = Bounds.borrow(); - playerBounds.setBounds(this.getLoc()); - this.playerBounds.setBounds(this); - - - // For debugging skills - // CharacterSkill.printSkills(this); - } - - public void updateBounds() { - this.playerBounds.setBounds(this); - this.checkCollisionsWithOtherPlayers(); - } - - public void checkCollisionsWithOtherPlayers() { - HashSet nearbyObjects = WorldGrid.getObjectsInRangePartial(this, MBServerStatics.CHARACTER_LOAD_RANGE, MBServerStatics.MASK_PLAYER); - for (AbstractWorldObject obj : nearbyObjects) { - if (obj instanceof PlayerCharacter && obj != this) { - PlayerCharacter otherPlayer = (PlayerCharacter) obj; - if (Bounds.collide(this.getBounds(), otherPlayer.getBounds(), 0.1f)) { - System.out.println("Collision detected with player: " + otherPlayer.getFirstName()); - ChatManager.chatSystemInfo(otherPlayer, "Has Collided with YOU"); - // Handle collision with other player - handleCollisionWithPlayer(otherPlayer); - } - } - } - } - - private void handleCollisionWithPlayer(PlayerCharacter otherPlayer) { - Vector3fImmutable myPos = this.getLoc(); - Vector3fImmutable otherPlayerPos = otherPlayer.getLoc(); - - // Calculate direction vector - Vector3fImmutable direction = myPos.subtract(otherPlayerPos).normalize(); - - // Move players apart - float separationDistance = 1.0f; // Adjust this value as needed - Vector3fImmutable myNewPos = myPos.add(direction.mult(separationDistance / 2)); - Vector3fImmutable otherPlayerNewPos = otherPlayerPos.subtract(direction.mult(separationDistance / 2.0f)); - - // Update positions - this.setLoc(myNewPos); - otherPlayer.setLoc(otherPlayerNewPos); - - // Refresh both players in the world - WorldGrid.updateObject(this); - WorldGrid.updateObject(otherPlayer); - - // Refresh both players in the world - WorldGrid.updateObject(this); - WorldGrid.updateObject(otherPlayer); } public static Building getUpdatedBindBuilding(PlayerCharacter player) { @@ -4619,9 +4564,6 @@ public class PlayerCharacter extends AbstractCharacter { this.charItemManager = new CharacterItemManager(this); - Bounds playerBounds = Bounds.borrow(); - playerBounds.setBounds(this.getLoc()); - this.setBounds(playerBounds); } @Override @@ -4936,7 +4878,6 @@ public class PlayerCharacter extends AbstractCharacter { @Override public void updateLocation() { - this.updateBounds(); if (!this.isMoving()) return; @@ -5418,6 +5359,8 @@ public class PlayerCharacter extends AbstractCharacter { moveToMsg.setSourceType(GameObjectType.PlayerCharacter.ordinal()); moveToMsg.setSourceID(this.getObjectUUID()); + ChatManager.chatSystemInfo(this, "This is a test"); + Dispatch dispatch = Dispatch.borrow(this, moveToMsg); DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.PRIMARY);