Repository navigation
Expand file tree
/
Copy pathMouseAiming.java
More file actions
53 lines (44 loc) · 1.51 KB
/
Copy pathMouseAiming.java
File metadata and controls
53 lines (44 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.Robot;
public class MouseAiming implements MouseMotionListener {
// stores data for mouse movement
// sensitivity can be changed
public double rot, pitch, sensitivity = 0.1;
// to avoid useless checks if mouse hasn't moved
public boolean mouseMoved;
public MouseAiming() {
// sets mouse in center of screen
rot = 0;
pitch = 0;
mouseMoved = false;
}
@Override
public void mouseMoved(MouseEvent e) {
// gets location of moused relative to center of screen
rot = e.getXOnScreen() - (GamePanel.windowWidth / 2);
// adjusts the rotaion based on sensitivity
rot = rot * sensitivity;
// gets location of moused relative to center of screen
pitch = e.getYOnScreen() - (GamePanel.windowHeight / 2);
// adjusts the pitch based on sensitivity
pitch = - pitch * sensitivity;
// sets mouse moved to true
mouseMoved = true;
}
public void recenterMouse() {
try {
// creates a mouse robot to move mouse
Robot robot = new Robot();
// sets mouse back to center of screen
robot.mouseMove(GamePanel.windowWidth / 2, GamePanel.windowHeight / 2);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void mouseDragged(MouseEvent e) {
// act as if mouse is moved
mouseMoved(e);
}
}