- Joined
- Jun 30, 2018
- Messages
- 1
- Thread Author
- #1
So I saw this code, but I thought it could be better. With this camera function you can give a Coordinate of a Npc, Object or just a random coordinate to watch. It determines the viewing angle within respect to the player and what the current viewing angle is, after which the mouse wheel makes the correction. The code can be improved a lot, but I'm happy in how it works now. I just want to share this method so you can give your own swing to it. I am also curious what you think.
JavaScript:
if (target.distanceTo(player.getPosition())>5 || !target.isVisible()) { //turn camera to target
if (Random.nextBoolean()){
mouseWheelTurnTo(target.getPosition());
} else {
Camera.concurrentlyTurnTo(target);
}
}
JavaScript:
private static void mouseWheelTurnTo(Coordinate Tc){
if ( Tc == null ) return;
Player player = Players.getLocal();
Coordinate Pc = player.getPosition();
int Dx = Tc.getX() - Pc.getX();
int Dy = Tc.getY() - Pc.getY();
int Yaw = Camera.getYaw();
int Beta = (int)( Math.atan2( - Dx, Dy ) * 180 / Math.PI ); //atan2 is in radians so 180/PI wil transform it to degrees, like the game.
if ( Beta < 0 ) Beta = 360 + Beta;
int deltaYaw = Beta - Yaw;
if ( deltaYaw > 180 ) {
deltaYaw = deltaYaw - 360;
} else if ( deltaYaw < - 180 ) {
deltaYaw = deltaYaw + 360;
}
int deltaMouseMoveX = (int) (-deltaYaw*2.5);
Area hoverArea = new Area.Circular(Players.getLocal().getPosition(), 3);
hoverArea.getRandomCoordinate().hover();
Mouse.press(Mouse.Button.WHEEL);
Mouse.move(new InteractablePoint((int) (Mouse.getPosition().getX() + deltaMouseMoveX), (int) (Mouse.getPosition().getY() + Random.nextInt(-10,10))));
Mouse.release(Mouse.Button.WHEEL);
}
Last edited: