-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.java
More file actions
303 lines (268 loc) · 13.3 KB
/
Manager.java
File metadata and controls
303 lines (268 loc) · 13.3 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//imports
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.util.Random;
import java.util.ArrayList;
//@SuppressWarnings("unused")
public class Manager extends JPanel implements KeyListener , MouseMotionListener {
private JFrame myFrame;
private double mouseSensitivity=0.01;
private int timeBetweenGameTicks = 10; //milliseconds
private Random r = new Random();
Geometry geo = new Geometry();//geometry.java instance
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = (int)size.getWidth();
int screenHeight = (int)size.getHeight();
int screenCenterWidth = screenWidth/2;
int screenCenterHeight = screenHeight/2;
private double maxTriangleDistance;
private double minTriangleDistance;
private double pixelsPerUnit;
private Vector3 camDirection;
private double renderPlaneWidth;
private Plane renderPlane;
private Quaternion pointRotationQuaternion;
private Vector3 camCenterPoint;
Camera c; //camera object, will be initalized in initalizeScreen()
//VertexPool will not work for moving vertices, probably
static ArrayList<GeometryGroup> staticMeshMap = new ArrayList<>();
static ArrayList<Line> linesToDraw = new ArrayList<>();
ArrayList<Triangle> screenSpaceMap = new ArrayList<>(); //used to store transformed triangles to render
//constructor
private Manager() {
myFrame = new JFrame("Game!");
myFrame.add(this);
this.addKeyListener(this);
this.addMouseMotionListener(this);
this.setFocusable(true);
myFrame.setSize(screenWidth, screenHeight);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setUndecorated(true);
myFrame.setVisible(true);
initalizeScreen();
Timer gameTimer = new Timer(timeBetweenGameTicks, e -> gameTick());
gameTimer.start();
}
//this ↓↓ is the drawing method that is called every frame
//by the rivers of babyleon
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (c==null){ //if the camera for some reason won't load, the screen goes grey until it does
g2.setColor(Color.GRAY);
g2.fillRect(0, 0, getWidth(), getHeight());
return;
}
maxTriangleDistance = 0;
minTriangleDistance = c.far;
pixelsPerUnit = getWidth()/renderPlaneWidth;
renderPlaneWidth = c.getRenderPlaneWidth();
camDirection = c.getDirectionVector();
camCenterPoint = Vector3.add(Vector3.multiply(camDirection, c.getRenderPlaneDistance()), c.getPosition());
renderPlane = new Plane(Vector3.add(Vector3.multiply(camDirection, c.getRenderPlaneDistance()), c.getPosition()), camDirection);
pointRotationQuaternion = BigUtils.createRotationQuaternion(c.getVorientation(), -c.getHorientation());
renderPlane = new Plane(Vector3.add(Vector3.multiply(camDirection, c.getRenderPlaneDistance()), c.getPosition()), camDirection);
g2.setColor(Color.GREEN);
g2.fillRect(0, 0, getWidth(), getHeight()); //fill the screen with black,
// g2.translate(getWidth() / 2, getHeight() / 2); //then center the image
//resetting the screen space buffer map thing
screenSpaceMap = new ArrayList<Triangle>();
//find screen space for each tri
for (GeometryGroup entry : staticMeshMap){
TriangleGroup value = entry.triangleGroup;
for (Triangle t : value.triangles) {
triToScreen(t);
}
}
//TODO
//DOES NOT WORK!!!
int ix = 0;
for (Triangle t : screenSpaceMap) {
Path2D.Double path = new Path2D.Double();
path.moveTo(t.v1.x, t.v1.y);
path.lineTo(t.v2.x, t.v2.y);
path.lineTo(t.v3.x, t.v3.y);
path.lineTo(t.v1.x, t.v1.y);
path.closePath();
g2.setColor(t.color);
g2.fill(path);
ix++;
}
for (Line line : linesToDraw) {
renderLine(g2, line);
}
}
public void initalizeScreen(){
// yo so this ↓↓ is the camera
c = new Camera(new Vector3(0,0,0),90);
c.lookAt(new Vector3(0,0,-1));
renderPlaneWidth = c.getRenderPlaneWidth();
geo.makeStaticPlane(-5,5,5,-5,-5,-5,Color.PINK,Color.ORANGE);
geo.makeStaticPlane(-50,50,50,-50,-50,-50,Color.RED,Color.BLUE);
Line l = new Line(new Vector3(-1000000, 0, 0),new Vector3(1000000, 0, 0),Color.blue); //X AXIS
linesToDraw.add(l);
l = new Line(new Vector3(0, -1000000, 0),new Vector3(0, 1000000, 0),Color.RED); //Y AXIS
linesToDraw.add(l);
l = new Line(new Vector3(0, 0, -1000000),new Vector3(0, 0, 1000000),Color.PINK); //z AXIS
linesToDraw.add(l);
//invisable cursor
myFrame.setCursor( myFrame.getToolkit().createCustomCursor(
new BufferedImage( 1, 1, BufferedImage.TYPE_INT_ARGB ),
new Point(),
null ) );
}
private void renderLine(Graphics2D g2, Line line) {
Vector3 start = line.getPoint1();
Vector3 end = line.getPoint2();
start = transformToScreen(start);
end = transformToScreen(end);
g2.setColor(line.c);
g2.drawLine((int)start.x, (int)start.y, (int)end.x, (int)end.y);
}
private Vector3 transformToScreen(Vector3 point) {
point = Vector3.getIntersectionPoint(Vector3.subtract(point, c.getPosition()), c.getPosition(), renderPlane);
point = Vector3.rotate(Vector3.subtract(point, camCenterPoint), pointRotationQuaternion);
int screenX = (int) (getWidth() / 2 + point.x * pixelsPerUnit);
int screenY = (int) (getHeight() / 2 - point.y * pixelsPerUnit);
return new Vector3(screenX, screenY, point.z);
}
public void gameTick() {
//other calculations and whatnot
repaint();
}
public void keyTyped(KeyEvent e) {
//Hey! I'm not implemented! Fix that!
}
private void triToScreen(Triangle triangle)
{
Vector3 triangleCenter = triangle.getCenter();
double distanceToTriangle = Vector3.subtract(triangleCenter, c.getPosition()).getMagnitude();
if (distanceToTriangle > maxTriangleDistance)
maxTriangleDistance = distanceToTriangle;
else if (distanceToTriangle < minTriangleDistance)
minTriangleDistance = distanceToTriangle;
if
(
Vector3.dotProduct(Vector3.subtract(triangleCenter, c.getPosition()), camDirection) <= 0 //is the triangle behind the camera?
|| distanceToTriangle >= c.far //is the triangle too far away?
|| distanceToTriangle <= c.near //is the triangle too close?
){
System.out.println("skipped a triangle");
System.out.println("near plane error:"+(distanceToTriangle <= c.near));
System.out.println("far plane error:"+(distanceToTriangle >= c.far));
System.out.println("behind camera error:"+(Vector3.dotProduct(Vector3.subtract(triangleCenter, c.getPosition()), camDirection) <= 0));
return;
//if the trianle meets one of the above contitions it is not eligable for rendering at this time
}
//clone the triangle's vertices:
Vector3 triangleVertex1 = triangle.v1.clone();
Vector3 triangleVertex2 = triangle.v2.clone();
Vector3 triangleVertex3 = triangle.v3.clone();
//the screen coords of the triangle, to be determined by the rest of the method.
Point p1ScreenCoords = new Point();
Point p2ScreenCoords = new Point();
Point p3ScreenCoords = new Point();
//will be set true if atleast one of the verticies is in camera's view.
boolean shouldDrawTriangle = false;
//get intersection with render plane
triangleVertex1 = Vector3.getIntersectionPoint(Vector3.subtract(triangleVertex1, c), c.getPosition(), renderPlane);
//rotate the point
triangleVertex1 = Vector3.rotate(Vector3.subtract(triangleVertex1, camCenterPoint), pointRotationQuaternion);
//check if it's in fov
if ((Math.abs(triangleVertex1.x) < renderPlaneWidth/2*1.2 && Math.abs(triangleVertex1.y) < renderPlaneWidth*((double)getHeight()/(double)getWidth())/2))
shouldDrawTriangle = true;
//scale to screen coord
p1ScreenCoords.x = (int)(getWidth()/2 + triangleVertex1.x*pixelsPerUnit);
p1ScreenCoords.y = (int)(getHeight()/2 - triangleVertex1.y*pixelsPerUnit);
//repeat for other points
triangleVertex2 = Vector3.getIntersectionPoint(Vector3.subtract(triangleVertex2, c.getPosition()), c.getPosition(), renderPlane);
triangleVertex2 = Vector3.rotate(Vector3.subtract(triangleVertex2, camCenterPoint), pointRotationQuaternion);
if ((Math.abs(triangleVertex2.x) < renderPlaneWidth/2 && Math.abs(triangleVertex2.y) < renderPlaneWidth*((double)getHeight()/getWidth())/2))
shouldDrawTriangle = true;
p2ScreenCoords.x = (int)(getWidth()/2 + triangleVertex2.x*pixelsPerUnit);
p2ScreenCoords.y = (int)(getHeight()/2 - triangleVertex2.y*pixelsPerUnit);
triangleVertex3 = Vector3.getIntersectionPoint(Vector3.subtract(triangleVertex3, c.getPosition()), c.getPosition(), renderPlane);
triangleVertex3 = Vector3.rotate(Vector3.subtract(triangleVertex3, camCenterPoint), pointRotationQuaternion);
if ((Math.abs(triangleVertex3.x) < renderPlaneWidth/2 && Math.abs(triangleVertex3.y) < renderPlaneWidth*((double)getHeight()/getWidth())/2))
shouldDrawTriangle = true;
p3ScreenCoords.x = (int)(getWidth()/2 + triangleVertex3.x*pixelsPerUnit);
p3ScreenCoords.y = (int)(getHeight()/2 - triangleVertex3.y*pixelsPerUnit);
if (shouldDrawTriangle){
Triangle screenTri = new Triangle(new Vector3(p1ScreenCoords.x, p1ScreenCoords.y, Vector3.getDiagonalDistance(triangleVertex1, c.getPosition())),
new Vector3(p2ScreenCoords.x, p2ScreenCoords.y, Vector3.getDiagonalDistance(triangleVertex2, c.getPosition())),
new Vector3(p3ScreenCoords.x, p3ScreenCoords.y, Vector3.getDiagonalDistance(triangleVertex3, c.getPosition())),
triangle.color);
screenSpaceMap.add(screenTri);
}
}
public void quietMouseMove() {
try {
// Temporarily remove the listener
myFrame.removeMouseMotionListener(this);
// Move the mouse using Robot (without triggering mouseMoved)
Robot robot = new Robot();
Point center = myFrame.getLocationOnScreen();
int x = center.x + myFrame.getWidth() / 2;
int y = center.y + myFrame.getHeight() / 2;
robot.mouseMove(x, y);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
myFrame.addMouseMotionListener(Manager.this);
}
});
} catch (AWTException e) {
e.printStackTrace();
}
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode(); // Get the virtual key code
switch (keyCode) {
case KeyEvent.VK_W:
c.translate(new Vector3(50,0,0));
break;
case KeyEvent.VK_A:
c.translate(new Vector3(0,-50,0));
break;
case KeyEvent.VK_S:
c.translate(new Vector3(-50,0,0));
break;
case KeyEvent.VK_D:
c.translate(new Vector3(0,50,0));
break;
case KeyEvent.VK_SPACE:
c.translate(new Vector3(0,0,50));
break;
case KeyEvent.VK_SHIFT:
c.translate(new Vector3(0,0,-50));
break;
case KeyEvent.VK_ESCAPE:
System.out.println("Exit game!");
System.exit(0); // Quit the application
break;
}
}
public void keyReleased(KeyEvent e) {
//Hey! I'm not implemented! Fix that!
}
public void mouseDragged(MouseEvent e) {
//Hey! I'm not implemented! Fix that!
}
public void mouseMoved(MouseEvent e) {
if (e.getX()==0&&e.getY()==0){
return;
}
double turnAmountX = e.getX() - screenWidth/2;
double turnAmountY = e.getY() - screenHeight/2;
c.updateOrientation(turnAmountX, turnAmountY, mouseSensitivity);
quietMouseMove();
}
public static void main(String[] args) {
new Manager();
}
}