From f0f3ec986ff7737441e9a64632fabfe2d3facc8e Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Mon, 21 Dec 2020 00:30:18 +0100 Subject: [PATCH] NPC stops in certain radius --- core/src/com/trs/main/MovingNpc.java | 16 ++++++++++------ core/src/com/trs/main/StaticMath.java | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/core/src/com/trs/main/MovingNpc.java b/core/src/com/trs/main/MovingNpc.java index 89e3462..0ffd05c 100644 --- a/core/src/com/trs/main/MovingNpc.java +++ b/core/src/com/trs/main/MovingNpc.java @@ -57,7 +57,6 @@ public class MovingNpc extends Actor{ POI = new Vector2(area.getX() + ((float) Math.random() * (float) area.getWidth()), area.getY() + ((float) Math.random() * (float) area.getHeight())); } Vector2 movement = new Vector2(speed,0); - //movement.setAngleRad((float)Math.atan((double)(POI.y-getY())/(double)(POI.x-getX()))); movement.setAngleRad(StaticMath.calculateAngle(getX(), getY(), POI.x, POI.y)); if(movement.angleDeg() < 135 && movement.angleDeg() >= 45) { @@ -73,12 +72,17 @@ public class MovingNpc extends Actor{ facing = 3; } - System.out.println(movement.angleDeg()); - System.out.println(POI.x + " " + POI.y); - System.out.println(); - movementX = movement.x; - movementY = movement.y; + if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y, movement.angleRad()) < 10f) { + movementX = 0; + movementY = 0; + } + else { + movementX = movement.x; + movementY = movement.y; + } + if(movementX == 0 && movementY == 0){ + } else if(movementX == 0 && movementY != 0){ setY(getY()+movementY); diff --git a/core/src/com/trs/main/StaticMath.java b/core/src/com/trs/main/StaticMath.java index 55f791f..06cf1db 100644 --- a/core/src/com/trs/main/StaticMath.java +++ b/core/src/com/trs/main/StaticMath.java @@ -42,11 +42,11 @@ public class StaticMath { return (float) alpha; } - public static double calculateDistance(int xPos1, int yPos1, int xPos2, int yPos2, double angle){ + public static float calculateDistance(float xPos1, float yPos1, float xPos2, float yPos2, float angle){ float deltaX = xPos2 - xPos1; float deltaY = yPos2 - yPos1; double distance = Math.abs((deltaY / Math.sin(angle))); - return distance; + return (float) distance; } }