Jump to content

isCharacterFacing(Character<?> character, RS2Object object)


Recommended Posts

Posted (edited)

Quickly wrote this, feel free to improve!

Precision: 45°

You could probably calculate the angle much more precisely, I'll maybe give that a shot another time ^^

public static boolean isCharacterFacing(Character<?> character, Position position) {
		
		int rotation = character.getRotation();
		Position charPos = character.getPosition();
		int xDiff = charPos.getX() - position.getX();
		int yDiff = charPos.getY() - position.getY();
		
		// Character position equals position.
		if (xDiff == 0 && yDiff == 0) {
			return false; 
		}
		
		// Position is east of character.
		if (xDiff < 0) {
			if (rotation < 1024) {
				return false;
			}
		}
		// Position is west of character.	
		else if (xDiff > 0) {
			if (rotation > 1024) {
				return false;
			}
		}
		// Position is on the same vertical axis.
		else {
			// Position is exactly south of character.
			if (yDiff > 0) {
				return rotation < 256 || rotation > 1792;
			}
			// Position is exactly north of character.
			else {
				return rotation > 762 && rotation < 1286;
			}
		}
		
		// Position is south of character.
		if (yDiff > 0) {
			if (rotation > 512 && rotation < 1536) {
				return false;
			}
		}
		// Position is north of character.
		else if (yDiff < 0) {
			if (rotation < 512 || rotation > 1536) {
				return false;
			}
		}
		// Position is on the same horizontal axis.
		else {
			// Position is exactly east of character.
			if (xDiff < 0) {
				return rotation > 1286 && rotation < 1792;
			}
			// Position is exactly west of character.
			else {
				return rotation > 256 && rotation < 762;
			}
		}
		
		return true;
	}
	
	public static boolean isCharacterFacing(Character<?> character, RS2Object object) {
		return isCharacterFacing(character, object.getPosition());
	}
Edited by Botre
  • Like 2

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...