/
HeadCamRot.java
161 lines (134 loc) · 3.96 KB
/
HeadCamRot.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package vrserver;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import static java.lang.Math.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HeadCamRot extends JFrame {
static double DegToRad = (Math.PI / 180);
static int Width = 1024;
static int Height = 800;
static int Radius = 200;
static int CX = Width / 2;
static int CY = Height / 2;
private static final int HeadNaxRot = 70;
static boolean Clamp = true;
float bodyRot = 0;
// don't touch this variable
float camRot = 0;
private DrawPanel drawPanel;
public HeadCamRot() {
setLayout(new BorderLayout());
drawPanel = new DrawPanel();
add(drawPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
HeadCamRot frame = new HeadCamRot();
frame.setSize(Width, Height);
frame.setTitle("HeadCamRot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setFocusable(true);
Listener listener = frame.new Listener();
frame.addKeyListener(listener);
}
class Listener implements KeyListener {
private static final float Step = 5;
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_KP_LEFT || key == KeyEvent.VK_LEFT) {
camRot += Step;
if (Clamp && camRot > 360) {
camRot -= 360;
}
} else if (key == KeyEvent.VK_KP_RIGHT || key == KeyEvent.VK_RIGHT) {
camRot -= Step;
if (Clamp && camRot < 0) {
camRot += 360;
}
} else if (key == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
private double calcAngles() {
System.out.println("------\npaint camRot=" + camRot);
float delta = camRot- bodyRot;
boolean locked = abs(delta) > HeadNaxRot;
boolean flipped = abs(delta) > 180;
if (locked && !flipped ) { // && !flipped
System.out.println("locked=" + delta);
if (delta > HeadNaxRot) {
bodyRot = camRot - HeadNaxRot;
}
if (delta < HeadNaxRot) {
bodyRot = camRot + HeadNaxRot;
}
}
System.out.println("camRot=" + camRot + " body=" + bodyRot);
if (flipped ) {
System.out.println("flipped bodyRot=" + bodyRot);
float orgRot = bodyRot;
bodyRot = bodyRot - (360 - 2 * HeadNaxRot);
delta = camRot - bodyRot;
locked = abs(delta) > HeadNaxRot;
if (locked) {
System.out.println("flipped locked delta=" + delta);
if (delta < HeadNaxRot) {
bodyRot = camRot - 2 * HeadNaxRot;
}
if (delta > HeadNaxRot) {
bodyRot = camRot + 2 * HeadNaxRot;
}
} else {
System.out.println("flipped unlocked");
//bodyRot = orgRot;
}
System.out.println("flipped bodyRot=" + bodyRot);
// bodyRot %= 360;
if ( bodyRot > 360 ) {
System.out.println("recursion");
return calcAngles();
}
}
return delta;
}
class DrawPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(0, 0, Width, Height);
g.setColor(Color.white);
double delta = calcAngles();
double camangle = camRot * DegToRad;
int x = (int) (CX + (cos(camangle) * Radius));
int y = (int) (CY + (sin(camangle) * Radius));
g.setColor(Color.gray);
g.fillArc(CX - Radius / 2, CY - Radius / 2, Radius, Radius,
(int) (bodyRot - HeadNaxRot), (int) HeadNaxRot * 2);
g.setColor(Color.white);
g.drawLine(CX, CY, x, Height - y);
g.drawString("cam=" + (int) camRot, x, Height - y);
g.drawString("delta=" + (int) delta, 10, CY);
g.drawString("camRot=" + (int) camRot, 10, CY + 20);
double bodyangle = bodyRot * DegToRad;
int xb = (int) (CX + (cos(bodyangle) * Radius));
int yb = (int) (CY + (sin(bodyangle) * Radius));
g.setColor(Color.orange);
g.drawLine(CX, CY, xb, Height - yb);
g.drawString("body=" + (int) bodyRot, xb, Height - yb);
}
}
}