-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALaserTest.java
More file actions
376 lines (305 loc) · 9.3 KB
/
ALaserTest.java
File metadata and controls
376 lines (305 loc) · 9.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package com.mfp.cakegdx;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.math.Vector2;
/**
*
* @author Archie Liu
* @mail longkas@gmail.com
*
*/
public class ALaserTest implements ApplicationListener, InputProcessor {
Mesh mesh;
Texture texture;
float pi = 3.14159f;
List<Float> vertices;
float shipX;
float shipY;
short pieces = 100;
float time;
short minPiece = 20;
float arcStep = 0.5f; //arc steps determined by y-axis distance
double arc;
float radius = 0.5f;
float laserV = 0.8f; //move speed
float newLaserSpeed = 0.05f; // gen new speed
float laserNumPerUnit = 50; // laser num per unit
float lastGenTime;
private float cf;
short maxLaserNum = 500;
boolean toWide = true;
Laser lastGenLaser;
List<Laser> lasers;
List<Short> indices;
float middleX;
float middleY;
float minArc = pi / 6;
private void update() {
float delta = (float) Math.min(0.06, Gdx.graphics.getDeltaTime());
Iterator<Laser> ite = lasers.iterator();
while (ite.hasNext()) {
Laser l = ite.next();
if (l.leftLowPos.y > 1) {
ite.remove();
continue;
}
l.move(delta);
}
time += delta;
lastGenTime += delta;
// System.out.println("lastGenTime " + lastGenTime);
if (lastGenTime > newLaserSpeed) {
newLaser(delta);
lastGenTime = 0;
}
this.genMeshData();
}
public void create() {
Gdx.input.setInputProcessor(this);
cf = Color.toFloatBits(255, 255, 255, 0);
lasers = new ArrayList<Laser>();
mesh = new Mesh(true, 4 * (maxLaserNum), 6 * (maxLaserNum), new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.ColorPacked, 4,
"a_color"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords"));
// texture = new Texture(Gdx.files.internal("data/20894_64.png"), true);
texture = new Texture(Gdx.files.internal("data/MEGAlaser.png"), true);
texture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
}
public static void main(String[] args) {
new LwjglApplication(new ALaserTest(), "mesh test", 600, 500, false);
}
public void render () {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
update();
if (!this.lasers.isEmpty()) {
Gdx.gl.glEnable(GL10.GL_TEXTURE_2D);
texture.bind();
mesh.render(GL10.GL_TRIANGLES, 0, 6 * lasers.size());
}
// System.out.println("renderring " + lasers.size());
}
class Laser {
Vector2 leftHighPos ;
Vector2 rightHighPos;
Vector2 leftLowPos;
Vector2 rightLowPos;
Vector2 leftLowUV;
Vector2 rightLowUV;
Vector2 rightHighUV;
Vector2 leftHighUV ;
public void move(float delta) {
float moveLength = (delta * laserV);
leftHighPos.add(0, moveLength);
rightHighPos.add(0, moveLength);
leftLowPos.add(0, moveLength);
rightLowPos.add(0, moveLength);
}
public void addToVertice(List<Float> vertices) {
//left low put
add(vertices, leftLowPos.x,leftLowPos.y,0f,cf, leftLowUV.x, leftLowUV.y);
// System.out.println( leftLowPos.x + " "+leftLowPos.y);
//right low put
add(vertices, rightLowPos.x,rightLowPos.y,0f,cf, rightLowUV.x,rightLowUV.y);
//right high put
add(vertices, rightHighPos.x,rightHighPos.y,0f,cf, rightHighUV.x,rightHighUV.y);
//left high put
add(vertices, leftHighPos.x,leftHighPos.y,0f,cf, leftHighUV.x,leftHighUV.y);
// System.out.println( (1f - 1f * (i * 1.0f / pieces)) + " e "+(1f - 1f * ( (i + 1) * 1.0f / pieces)));
}
public void genUV(Vector2 total, Vector2 lastUVLeft, Vector2 LastUVRight) { // left (0,1) to(0,0), right(1,1) to (1,0)
this.leftHighUV = lastUVLeft;
this.rightHighUV = LastUVRight;
this.leftLowUV = new Vector2(0 , lastUVLeft.y - (leftHighPos.dst(this.leftLowPos) / total.x));
this.rightLowUV = new Vector2(1, LastUVRight.y - (this.rightHighPos.dst(this.rightLowPos)) / total.y);
lastUVLeft = leftLowUV;
LastUVRight = rightLowUV;
}
public void addLen(Vector2 totalLen) {
totalLen.add(this.leftHighPos.dst(this.leftLowPos), this.rightHighPos.dst(this.rightLowPos));
}
}
private void genMeshData() {
vertices = new ArrayList<Float>();
int idx = 0;
Vector2 total = new Vector2(0,0); // one laser has
for (Laser l : lasers) { //gen total len to calculate UV
l.addLen(total);
}
Vector2 lastUVLeft = new Vector2(0, 0);
Vector2 LastUVRight= new Vector2(1, 0);
for (Laser l : lasers) {
l.genUV(total,lastUVLeft, LastUVRight);
l.addToVertice(vertices);
idx++;
}
indices = new ArrayList<Short>();
int laserCount = this.lasers.size();
for (short j = 0;j < laserCount ;j++) { //6 indice each laser
short i = (short) (j * 4);
this.add(indices, i, (short)(i+1),(short)(i+ 2), (short)(i+2),(short)(i+ 3),(short)(i));
}
float[] verticesFs = new float[vertices.size()]; //24 floats each laser
int vCount = 0;
for (Float f : vertices) {
verticesFs[vCount] = f;
vCount++;
}
short[] indicesSs = new short[indices.size()];
int iCount = 0;
for (Short s: indices) {
indicesSs[iCount] = s;
iCount++;
}
mesh.setVertices(verticesFs);
mesh.setIndices(indicesSs);
}
public void newLaser(float delta) {
this.checkLastGenLaser();
//calculate gen num
Vector2 middle = new Vector2((this.lastGenLaser.leftLowPos.x + lastGenLaser.rightLowPos.x) /2,
(this.lastGenLaser.leftLowPos.y + lastGenLaser.rightLowPos.y) /2);
float distance = middle.dst(this.shipX, this.shipY);
short genNum = (short) (this.laserNumPerUnit * distance);
float yDst = Math.abs(middle.y - shipY);
for (int genCount = 0; genCount < genNum; genCount++) {
if (this.lasers.size() >= this.maxLaserNum) {
return;
}
// System.out.println("gen new laser");
Laser laser = new Laser();
lasers.add(laser);
laser.leftHighPos = this.calculatePos(true, true, genCount, genNum, middle);
laser.rightHighPos = this.calculatePos(false, true, genCount, genNum, middle);
laser.leftLowPos = this.calculatePos(true, false, genCount, genNum, middle);
laser.rightLowPos = this.calculatePos(false, false, genCount, genNum, middle);
if (arc < minArc || arc > (pi - minArc)) {
arc = minArc;
}
arc += yDst * arcStep;
arc %= pi;
if (lasers.size() > maxLaserNum - 20) {
System.out.println(laser.leftHighPos + " " + laser.rightHighPos + " " + laser.leftLowPos + " " + laser.rightLowPos );
}
lastGenLaser = laser;
}
}
private void checkLastGenLaser() {
if (this.lastGenLaser != null && lastGenLaser.leftLowPos != null) {
return;
}
lastGenLaser = new Laser();
lastGenLaser.leftLowPos = new Vector2(0, 0.1f);
lastGenLaser.rightLowPos = new Vector2(0, 0.1f);
}
private Vector2 calculatePos(boolean left, boolean high, int genCount, int genTotalNum, Vector2 middle) {
this.checkLastGenLaser();
if (high && left) {
return new Vector2(lastGenLaser.leftLowPos); //reuse
}
if (high && !left) {
return new Vector2(lastGenLaser.rightLowPos); //reuse
}
float curY = shipY + (genTotalNum - genCount) / genTotalNum * (middle.y - shipY);
float curX = shipX + (genTotalNum - genCount) / genTotalNum * (middle.x - shipX);
if (left) {
return new Vector2( (float) (curX - Math.sin(arc) * radius /2), curY);
}
if (!left) {
return new Vector2( (float) (curX + Math.sin(arc) * radius /2), curY);
}
return null;
}
private void add(List<Short> indices, Short... shorts) {
for (Short f : shorts) {
indices.add(f);
}
}
private void add(List<Float> vertices, Float... floats) {
for (Float f : floats) {
vertices.add(f);
}
}
public boolean needsGL20 () {
return false;
}
@Override
public void dispose()
{
// TODO Auto-generated method stub
}
@Override
public void pause()
{
// TODO Auto-generated method stub
}
@Override
public void resize(int arg0, int arg1)
{
// TODO Auto-generated method stub
}
@Override
public void resume()
{
// TODO Auto-generated method stub
}
@Override
public boolean keyDown(int arg0)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyTyped(char arg0)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyUp(int arg0)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean mouseMoved(int arg0, int arg1)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean scrolled(int arg0)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDown(int arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDragged(int arg0, int arg1, int arg2)
{
this.shipX = (arg0 * 1f / Gdx.graphics.getWidth()) - 0.5f;
return false;
}
@Override
public boolean touchUp(int arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
return false;
}
}