Author: matheus23 (posted 2012-11-19 17:00:28, viewed 311 times)
| 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
|
package org.matheusdev.test;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_LINE_LOOP;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex2f;
import static org.lwjgl.opengl.GL11.glViewport;
import javax.swing.JOptionPane;
import mdesl.graphics.SpriteBatch;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.matheusdev.util.FastRand;
import org.matheusdev.util.collision.Circle;
import org.matheusdev.util.collision.Collision;
import org.matheusdev.util.collision.Poly;
import org.matheusdev.util.collision.Quad;
import org.matheusdev.util.gameloop.GameLoop;
import org.matheusdev.util.gameloop.GameLooper;
import org.matheusdev.util.vecmath.Vec2;
import org.matheusdev.util.vecmath.Vec3;
public class SATTest implements GameLooper {
public static int objects;
public static void main(String[] args) {
do {
try {
objects = Integer.parseInt(JOptionPane.showInputDialog(
"How much Polygons? (each one will have 8 vertices)\n" +
"This window will return, if you enter invalid values."));
} catch (Exception e) {}
} while (objects <= 0);
new GameLoop(new SATTest(), GameLoop.Type.LIMITED_DELTA, 60).start();
}
protected Poly[] polys;
protected boolean[] collisions;
protected Quad quad = new Quad(
new Vec2(), 160, 160);
protected SpriteBatch batch;
protected boolean colliding;
public SATTest() {
}
@Override
public long getNanoTime() {
return (Sys.getTime() * 1_000_000_000) / Sys.getTimerResolution();
}
@Override
public void init() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
try {
batch = new SpriteBatch();
} catch (LWJGLException e) {
e.printStackTrace();
}
resize(Display.getWidth(), Display.getHeight());
polys = new Poly[objects];
collisions = new boolean[objects];
FastRand rand = new FastRand();
for (int i = 0; i < polys.length; i++) {
polys[i] = new Poly(
new Vec2(-30 + rand.randFloat(-5, 5), 10 + rand.randFloat(-5, 5)),
new Vec2(-10 + rand.randFloat(-5, 5), 30 + rand.randFloat(-5, 5)),
new Vec2( 10 + rand.randFloat(-5, 5), 30 + rand.randFloat(-5, 5)),
new Vec2( 30 + rand.randFloat(-5, 5), 10 + rand.randFloat(-5, 5)),
new Vec2( 30 + rand.randFloat(-5, 5), -10 + rand.randFloat(-5, 5)),
new Vec2( 10 + rand.randFloat(-5, 5), -30 + rand.randFloat(-5, 5)),
new Vec2(-10 + rand.randFloat(-5, 5), -30 + rand.randFloat(-5, 5)),
new Vec2(-30 + rand.randFloat(-5, 5), -10 + rand.randFloat(-5, 5)));
polys[i].getMatrix().translate(new Vec2(rand.randFloat(0, 800), rand.randFloat(0, 600)))
.rotate(rand.randFloat(), Vec3.UP);
polys[i].updateFromMatrix();
}
}
public void resize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, h, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
batch.resize(w, h);
}
@Override
public boolean isCloseRequested() {
return Display.isCloseRequested();
}
protected float rotation = 0;
protected float time = 0;
@Override
public void tick(double delta) {
time += (float) delta;
int mx = Mouse.getX();
int my = (Display.getHeight() + 1) - Mouse.getY();
int dwheel = Mouse.getDWheel();
if (dwheel > 0) {
rotation += 0.1;
} else if (dwheel < 0) {
rotation -= 0.1;
}
quad.getMatrix()
.identity().translate(new Vec2(mx, my)).rotate(rotation, Vec3.UP);
quad.updateFromMatrix();
for (int i = 0; i < collisions.length; i++) {
collisions[i] = false;
}
Collision.axesTested = 0;
Collision.objectsTested = 0;
long time = (Sys.getTime() * 1_000) / Sys.getTimerResolution();
colliding = false;
for (int i = 0; i < polys.length; i++) {
if (Collision.polyVsPoly(quad, polys[i])) {
collisions[i] = true;
colliding = true;
}
}
System.out.println("Time taken: " + (((Sys.getTime() * 1_000) / Sys.getTimerResolution())-time) + ", axis tested: " + Collision.axesTested + ", objects tested: " + Collision.objectsTested);
}
private int count = 0;
@Override
public void render(double fps) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 1);
for (int i = 0; i < polys.length; i++) {
if (collisions[i]) {
glColor3f(1, 0, 0);
} else {
glColor3f(1, 1, 1);
}
drawVertices(polys[i].getTransformedVertices());
}
if (colliding) {
glColor3f(1, 0, 0);
} else {
glColor3f(1, 1, 1);
}
drawVertices(quad.getTransformedVertices());
if (count == 60) {
count = 0;
System.out.println("Rendered, fps: " + fps);
}
count++;
Display.update();
}
public void drawVertices(Vec2[] vertices) {
glBegin(GL_LINE_LOOP);
for (Vec2 vertex : vertices) {
glVertex2f(vertex.x, vertex.y);
}
glEnd();
}
public void drawCircle(Circle circ, float interval) {
float oneSegment = ((float) Math.PI / 180f) * interval;
Vec2 vec = new Vec2(circ.getRadius(), 0);
glBegin(GL_LINE_LOOP);
for (float i = 0; i < 360f; i += interval) {
glVertex2f(vec.x + circ.getCenter().x, vec.y + circ.getCenter().y);
vec.rotateRad(oneSegment);
}
glEnd();
}
@Override
public void dispose() {
Display.destroy();
}
} |
Special syntax:
- To highlight a line (yellow background), prefix it with '@@'
- To indicate that a line should be removed (red background), prefix it with '-'
- To indicate that a line should be added (green background), prefix it with '+'
- To post multiple snippets, seperate them by '~~~~'
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|