Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1] 2 3 4
1  Discussions / General Discussions / Re: [FIXED] Get position of end of angle? on: 2014-04-20 06:13:37
I know the topic title says that you've already fixed your issue, but if you want my take on this issue:
http://www.java-gaming.org/topics/shooting-from-the-angle-of-player/31896/msg/297330/view.html#msg297330
2  Game Development / Newbie & Debugging Questions / [lwjgl] game runs slow after a few seconds on macbook pro on: 2014-04-20 05:54:02
Hi all!
I just got a macbook pro (2014, 15 inch retina display) for college. It has an nvidia GPU (NVIDIA GeForce GT 750M), which... isn't bad for a laptop gpu.

When working on my game, I run into a problem. It is a 3d fps (quite system intensive, uses shaders for shadow mapping, bloom, fxaa. it has md3 model support, 3d polygonal collisions, ext). When I first run the application, I get about 120 fps. After about 10 seconds, the game drops to 90 fps, then 10 seconds later, 60 fps. Sometimes it stays at 60 fps, and other times it drops again to about 20 fps. What is odd, is that it seems locked at these values (never straying more than a number or two above/below the fps). I don't use the Display.sync() method for the game, and the internal tick rate always is at 300 cycles per second (the game is multi-threaded).

I looked at the activity monitor, and found that the memory use is constant throughout the entire duration of running the application, and the cpu usage is relatively constant.

What could cause the game to start slowing down like this?
3  Game Development / Newbie & Debugging Questions / Re: VBO Model Framework on: 2014-03-23 18:15:55
you probably should not use collada, it is a bloated inconsistent file format. Try something easier like MD5.
Or if you don't want to worry about vertex skinning, MD2 or MD3 work well
4  Discussions / General Discussions / Re: Accuracy vs flow, 2d top-down diagonal movement. on: 2014-03-22 21:41:47
I use vectors for movement; This makes it easy because you can normalize a vector which eliminates the whole problem of faster diagonal movement.

If you have a max speed, say 6...
1  
2  
3  
if (velocity.getMagnitude() > maxSpeed) {
   velocity.normalize().multiply(maxSpeed);
}
5  Discussions / General Discussions / Just re-discovered an old website I made for a game on: 2014-03-22 20:31:23
Back in 2011 I made a game called Mineblock... (one of my first java games)
I made a website for it: https://web.archive.org/web/20111108040305/http://mineblock.uphero.com/index.php
It was taken down, and I forgot the domain name Tongue

I forgot how many people signed up for it :O
6  Game Development / Newbie & Debugging Questions / Re: Converting a 3x3 rotation matrix to euler angles to 4x4 matrix on: 2014-03-21 22:42:01
Thanks Wink
Worked perfectly Smiley
7  Game Development / Newbie & Debugging Questions / Re: Converting a 3x3 rotation matrix to euler angles to 4x4 matrix on: 2014-03-21 22:00:03
One thing that I haven't been able to get to work right...
I wanted to change the "whole" direction of the md3 model.

My model class now uses a Matrix4f to express it's current rotation/position. Because the quake player model is made up of three separate models welded together by "tags", I need to add a new rotation to each model starting from the root one. So to do this, I stored the matrix4 value from the previous model, applied it to the current one, an then added that current models rotation. While this works, I'm not able to set a global direction for the model, as I overwrite the rotation.

So I guess my problem boils down to this...
I have a matrix4f, and I want to add it to a matrix3f.
I tried converting the matrix3f to a matrix4f, and then doing Matrix4f.add(m1, m2, src) but it doesn't seem to work...
8  Game Development / Newbie & Debugging Questions / Re: lwjgl vbo fatal error ?? on: 2014-03-21 16:43:39
Can I see your up-to-date version of the class AND your implementation of it?
9  Game Development / Newbie & Debugging Questions / Re: lwjgl vbo fatal error ?? on: 2014-03-21 14:32:39
Just a heads up (I learned this the hard way)
1  
glVertexPointer
is deprecated, IE, it doesn't work in "core" more (OpenGL3+).

Normally this wouldn't be an issue--If you're on a PC and are on the default compatibility mode, you can use both deprecated and non-deprecated methods without worrying. If you're on a mac however... and if you use compatibility mode (on my default), you can't use ANY OpenGL3+ methods. If you turn off compatibility mode, and turn on core mode, you can't use anything but OpenGL3+.

This method has been replaced with:
1  
GL20.glVertexAttribPointer()


If you want an example on it:
http://www.lwjgl.org/wiki/index.php?title=The_Quad_with_Projection,_View_and_Model_matrices

Sorry if I'm being "that guy" that is trying to get you to do a different method of things, rather than solving your problem directly... But getting rid of deprecated methods really helps in the long run!



--
[EDIT]
If you want help with your current problem...
(going off of what lion king started)

If you use a size of "GL11.GL_FLOAT", then that means there is 4 bytes per float. Since you are only using vertices which have 3 components, the stride then is 3 * 4.

Since the vertices are the first thing you're "setting up" in your VAO, the position should be 0.

So:
1  
glVertexPointer(3, GL_FLOAT, 12, 0L);




--
[EDIT 2]
glancing through your code, I feel like your drawArrays should be:
1  
 glDrawArrays(GL_QUADS, 0, vertices.length);




--
[EDIT 3]
I wouldn't use quads in a custom mesh class... It limits you greatly in what you can have meshes look like... I am not sure if my above code will work w/ quads, I know it'll work with triangles...
If you do end up using triangles, you second put method could be written this way:
1  
2  
3  
4  
5  
6  
7  
8  
public void put(float x, float y, float z, float width, float height){
   put(x, y, z);
   put(x+width, y, z);
   put(x+width, y+height, z);
   put(x+width, y+height, z);
   put(x, y+height, z);
   put(x, y, z);
}
10  Game Development / Newbie & Debugging Questions / Re: Converting a 3x3 rotation matrix to euler angles to 4x4 matrix on: 2014-03-20 19:22:35
I guess I'll just bite the bullet and get rid of my euler angle dependancy.
Seems to work fine now Wink
11  Game Development / Newbie & Debugging Questions / Re: Converting a 3x3 rotation matrix to euler angles to 4x4 matrix on: 2014-03-20 04:41:51
My model class uses euler angles, and it's used beyond just MD3 models; so I can't just convert the 3x3 matrix directly to a 4x4 matrix.
12  Game Development / Newbie & Debugging Questions / Converting a 3x3 rotation matrix to euler angles to 4x4 matrix on: 2014-03-20 02:39:25
I've been trying to get a MD3 loader to work for awhile now. With help from goussej I got the loader to load all the geometry, surfaces, skins, and tags. I now, however, found a new issue that I am not quite sure how to tackle.

the MD3 model format stores it's rotation matrix as a 3x3. The generic model format I use in my game uses euler angles for rotations (before sending it to the shader), expressed like this:
1  
2  
3  
Matrix4f.rotate((float)Math.toRadians(modelAngle.z), new Vector3f(0, 0, 1), modelMatrix, modelMatrix);
Matrix4f.rotate((float)Math.toRadians(modelAngle.y), new Vector3f(0, 1, 0), modelMatrix, modelMatrix);
Matrix4f.rotate((float)Math.toRadians(modelAngle.x), new Vector3f(1, 0, 0), modelMatrix, modelMatrix);


So, to try to break down the 3x3 matrix to euler angles, I did this:
1  
2  
3  
rotX = (float) Math.toDegrees(Math.atan2(tag._axis.m21, tag._axis.m22));
rotY = (float) Math.toDegrees(-Math.asin(tag._axis.m20));
rotZ = (float) Math.toDegrees(Math.atan2(tag._axis.m10, tag._axis.m00));


This doesn't seem to work quite right, and matrices are not my strong suit.
I will point out that the Z axis is used for height in my engine (instead of the Y axis).

Any ideas? :x
13  Game Development / Newbie & Debugging Questions / Re: Making a lwjgl application executable by "Double clicking" on: 2014-03-15 15:27:26
Does this happen with all jar files, or only your game?
Only the games I've made. I've rewritten my game like 4 times since I started it; each rewrite has the same bug. Other jars open up just fine.

Quote from: goussej
Please remind me which version of OS X you use. Look at the security settings of Gatekeeper.
OS X 10.9.2 (13C64)

I have also tried it on my dads macbook which yields the same effect.
14  Game Development / Newbie & Debugging Questions / Re: Making a lwjgl application executable by "Double clicking" on: 2014-03-15 04:41:36
If you're on windows, use this as your batch script for debugging:

1  
2  
@echo off
java -jar test.jar > log.txt 2>&1


(I have to go to bed, so I'm not going to figure it out for linux, but it redirects stdout and stderr to log.txt)

If you don't get anything in log.txt (will be created), then something's failing silently, and that's never good.

I'm on a mac for a few weeks. I removed the "@echo off" part, as it wouldn't work in a shell script. The java line still worked, and it created a log file. However, the contents of the log file seemed perfectly fine; the game turned on as it should, and ran normally.

The problem only seems to occur when I double click the jar :/
15  Game Development / Newbie & Debugging Questions / Re: Making a lwjgl application executable by "Double clicking" on: 2014-03-15 04:23:12
When running from terminal, the game turns on just fine :/
16  Game Development / Newbie & Debugging Questions / Making a lwjgl application executable by "Double clicking" on: 2014-03-15 03:36:15
Hi there!

I've always seemed to struggle with this problem, over the past few months of my learning lwjgl. When I export my application as a jar file, I am never able to make it executable by double clicking on it. I have tried Jar Splice, but even with that I find that I still need to use a batch/sh file to make the jar still run.

When I run my current jar through jar splice, and double click it, the application turns on, the window appears, but right after it loads all it's resources (models/textures), it seems that it turns off.

The batch/sh script I use:
1  
java -jar "test.jar"


I tried enabling the console in my Java's preferences, but it doesn't seem to turn on.

Any ideas?
17  Game Development / Newbie & Debugging Questions / Re: Which is the better way to render ? on: 2014-03-14 22:32:50
Just learn openGL. No point to learn useless technology that don't convert to actual knowledge.
These kind of posts aren't helpful at all... if the OP wants to use other means to an end, let him. To say there's no point, is to just deprive knowledge which may in fact become useful at some point.

Me personally, when I used to use Java2D, I would always choose the pixelArray route; I like to have control of everything Tongue
What first turned me onto the pixel array method was the speed increase. I used a simple Bitmap class I wrote which supported an additional array for alpha values, and so I essentially did all the graphical stuff myself.
18  Game Development / Newbie & Debugging Questions / Inquiry about attribute locations in glsl on: 2014-03-12 03:36:25
I have the following code in my application:
1  
2  
3  
positionLocation = glGetAttribLocation(pid, "position");
normalLocation = glGetAttribLocation(pid, "normal");
texCoordsLocation = glGetAttribLocation(pid, "texCoords");


positionLocation = 2
normalLocation = 0
texCoordsLocation = 1

Can someone explain the logic to how these locations are assigned their values? I would think they would be assigned chronologically... but I guess not.
19  Game Development / Newbie & Debugging Questions / Re: Compile and Debug Java on: 2014-03-11 19:27:18
I started with BlueJ, used it for a year, then switched to Eclipse, and never looked back Wink

In my opinion, I think that when you're first starting out with java, it is much easier/nicer to use BlueJ. Why? Because it forces you to learn the language. If your intent is to learn Java to say, take the AP Comp-Sci exam at your school, use BlueJ. But once you've learned most things about the language, then switch to whatever IDE you're most comfortable with Smiley

Things like netbeans/eclipse are really useful for speedy development. But using them can make you pick up some nasty habits (without proper knowledge of Java) :x
20  Game Development / Newbie & Debugging Questions / Re: [LWJGL] interleaved VBO (textured quad) (help with Indices) on: 2014-03-11 02:35:12
 I feel dumb... that should have been an easy catch xD

Thanks! Smiley

[edit]
The only reason I would ever think of a float buffer, is because they have a larger maximum value than an int (afaik). BUT, I forgot Java doesn't use the typical 4byte integer... so it's maximum value is fine Smiley
21  Game Development / Newbie & Debugging Questions / [LWJGL] interleaved VBO (textured quad) (help with Indices) on: 2014-03-11 02:26:23
So I decided to try to update my method of using VBO's, as apparently I was using a deprecated method which doesn't work well on the core version of openGL (on this computer).

Upon looking at this example: http://www.lwjgl.org/wiki/index.php?title=The_Quad_textured which uses the updated method, I noticed something I hadn't used before. Indices. It's pretty well explained in that example, but what I haven't been able to understand, is why it seems to require a byteBuffer. When I change it to say an IntBuffer, or a FloatBuffer, the quad no longer renders.

Why is this?
22  Discussions / Miscellaneous Topics / What's your rig? on: 2014-03-10 04:43:36
What are your guys PC specs?

-Intel i7 3.6 Ghz (quad core)
-NVidia GTX 680
-16 Gb RAM
-2560x1440 Monitor (60 Hz, 4ms delay)
-Pretty much logitech everything (keyboard, mouse, webcam, speakers, headset)
23  Discussions / General Discussions / Re: Best lwjgl game graphically? on: 2014-03-10 04:35:13
lwjgl Smiley
24  Discussions / General Discussions / Best lwjgl game graphically? on: 2014-03-10 04:32:47
I'm curious of this...

What do you guys think is the best (visually/graphically) lwjgl game released?
25  Game Development / Newbie & Debugging Questions / Re: [LWJGL] forcing openGL renderer to use the Core profile on: 2014-03-08 03:22:36
Well that introduced a million more problems than I expected Tongue

I guess I will just have to wait until I am back on my own computer.
26  Game Development / Newbie & Debugging Questions / [LWJGL] forcing openGL renderer to use the Core profile on: 2014-03-07 20:46:06
I am stuck with using my moms MacBook Air (2010) for a few weeks.

When I print out GL_VERSION as a string (from java), it tells me openGL is on version 2.1, but I need at least OpenGL 3 to work on my game.

When I downloaded OpenGL Extensions Viewer

It tells me that I am on OpenGl version 3.3.

I did notice, however, that I am on version 3.3 when I am viewing the Core openGL profile. When I read the Compatibility profile, I am on 2.1.

Is there anything I can do to force OpenGL to use the core profile?

--

I checked NVidia's website, but the drivers for this GPU only work on windows machines.

GPU: NVIDIA GeForce 320M 256 MB
27  Games Center / WIP games, tools & toy projects / Re: CarCry (3D Kinda-Racing Game) on: 2014-02-24 21:55:47
Why is this 27 Mb :x
--
The game isn't bad; It's a simple little arcade styled game which are always good Smiley

If you had the resources, I would definitely suggest spending some more time on the graphics (textures / car models), but hey graphics don't make the game Wink

Good job.
28  Discussions / Business and Project Discussions / Re: [$300-$1000] [LWJGL] Need experienced GLSL programmer on: 2014-02-11 21:05:33
Just want to say thanks to theagentd for doing this Wink

He's a shader god O:
29  Game Development / Game Mechanics / Re: Raytracing - Tutorials? on: 2014-02-11 20:44:08
I wrote a nifty little collision checking class for my java projects.

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  
377  
378  
379  
380  
381  
382  
383  
384  
385  
386  
387  
388  
389  
390  
391  
392  
393  
394  
395  
396  
397  
398  
399  
400  
401  
402  
403  
404  
405  
406  
407  
408  
409  
410  
411  
412  
413  
414  
415  
416  
417  
418  
419  
420  
421  
422  
423  
424  
425  
426  
427  
428  
429  
430  
431  
432  
433  
434  
435  
436  
437  
438  
439  
440  
441  
442  
443  
444  
445  
446  
447  
448  
449  
450  
451  
452  
453  
454  
455  
456  
457  
458  
459  
460  
461  
462  
463  
464  
465  
466  
467  
468  
469  
470  
471  
472  
473  
474  
475  
476  
477  
478  
479  
480  
481  
482  
483  
484  
485  
486  
487  
488  
489  
490  
491  
492  
493  
494  
495  
496  
497  
498  
499  
500  
501  
502  
503  
504  
505  
506  
507  
508  
509  
510  
511  
512  
513  
514  
515  
516  
517  
518  
519  
520  
521  
522  
523  
524  
525  
526  
527  
528  
529  
530  
531  
532  
533  
534  
535  
536  
537  
538  
539  
540  
541  
542  
543  
544  
545  
546  
547  
public class CollisionHelper {
   public static final float SMALL_NUM = 1.0E-008F;
   public static final int LARGE_NUM = 10000000;
   private static float[] lastTriangleData = { 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F };
   
   //p3dc_split();
  //Written by Andrew Hamilton (orange451)
  public static SplitModel model_split(D3DModel model1, int xStart, int yStart, int width, int height, int amount_cellsX, int amount_cellsY, int overlapX, int overlapY) {
      return new SplitModel(model1, xStart, yStart, width, height, amount_cellsX, amount_cellsY, overlapX, overlapY);
   }

   //p3dc_check()
  //Written by Andrew Hamilton (orange451)
  public static boolean collision_check(D3DModel model1, float x1, float y1, float z1, D3DModel model2, float x2, float y2, float z2) {
      int amtVertices = model1.getAmountVertices();
      for (int i = 0; i < amtVertices; i += 3) {
         float[] v1 = model1.getVertexAtId(i);
         float[] v2 = model1.getVertexAtId(i+1);
         float[] v3 = model1.getVertexAtId(i+2);
         v1[0] += x1;
         v1[1] += y1;
         v1[2] += z1;

         v2[0] += x1;
         v2[1] += y1;
         v2[2] += z1;

         v3[0] += x1;
         v3[1] += y1;
         v3[2] += z1;

         int amtVertices2 = model2.getAmountVertices();
         for (int ii = 0; ii < amtVertices2; ii += 3) {
            float[] v1_2 = model2.getVertexAtId(ii);
            float[] v2_2 = model2.getVertexAtId(ii+1);
            float[] v3_2 = model2.getVertexAtId(ii+2);

            v1_2[0] += x2;
            v1_2[1] += y2;
            v1_2[2] += z2;

            v2_2[0] += x2;
            v2_2[1] += y2;
            v2_2[2] += z2;

            v3_2[0] += x2;
            v3_2[1] += y2;
            v3_2[2] += z2;

            if (checkTriangleCollision(v1, v2, v3, v1_2, v2_2, v3_2)) {
               return true;
            }
         }
      }
      return false;
   }

   /*
    Retrun all triangles hit by a ray,
    By Andrew Hamilton (orange451)
   
    xRot, yRoy, zRot   = rotation of model being checked
    x1, y1, z1         = position of model being checked
    x2, y2, z2         = vertex of the ray
    nx, ny, nz         = direction of the ray
   */

   public static ArrayList<HitTriangle> collision_check_all(D3DModel model1, float xRot, float yRot, float zRot, float x1, float y1, float z1, float x2, float y2, float z2, float nx, float ny, float nz) {
      ArrayList<HitTriangle> ret = new ArrayList<HitTriangle>();
      float[] vec = { x2, y2, z2 };
      float[] dir = { nx, ny, nz };

      int amtVertices = model1.getAmountVertices();
      for (int i = 0; i < amtVertices; i += 3) {
         float[] v1 = model1.getVertexAtId(i);
         float[] v2 = model1.getVertexAtId(i + 1);
         float[] v3 = model1.getVertexAtId(i + 2);

         v1 = applyRotations(v1, xRot, yRot, zRot);
         v2 = applyRotations(v2, xRot, yRot, zRot);
         v3 = applyRotations(v3, xRot, yRot, zRot);

         v1[0] += x1;
         v1[1] += y1;
         v1[2] += z1;

         v2[0] += x1;
         v2[1] += y1;
         v2[2] += z1;

         v3[0] += x1;
         v3[1] += y1;
         v3[2] += z1;

         float dist = intersectRayTriangle(v1, v2, v3, vec, dir);
         if ((dist > 0.0D) && (dist < 10000000.0D)) {
            float[] normal = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]);
            lastTriangleData = new float[] { v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2], normal[0], normal[1], normal[2], i / 3 };
            ret.add(new HitTriangle(dist, lastTriangleData));
         }
      }

      Collections.sort(ret, new Comparator<HitTriangle>() {
         @Override
         public int compare(HitTriangle arg0, HitTriangle arg1) {
            return (int)(arg0.distanceHitFrom - arg1.distanceHitFrom);
         }
      });
      return ret;
   }

   //p3dc_check()
  //Written by Andrew Hamilton (orange451)
  public static float collision_ray(D3DModel model1, float xRot, float yRot, float zRot, float x1, float y1, float z1, float x2, float y2, float z2, float nx, float ny, float nz) {
      float[] vec = {x2, y2, z2};
      float[] dir = {nx, ny, nz};
      float retdist = LARGE_NUM;
      int amtVertices = model1.getAmountVertices();
      for (int i = 0; i < amtVertices; i += 3) {
         float[] v1 = model1.getVertexAtId(i);
         float[] v2 = model1.getVertexAtId(i+1);
         float[] v3 = model1.getVertexAtId(i+2);

         v1 = applyRotations(v1, xRot, yRot, zRot);
         v2 = applyRotations(v2, xRot, yRot, zRot);
         v3 = applyRotations(v3, xRot, yRot, zRot);
         
         v1[0] += x1;
         v1[1] += y1;
         v1[2] += z1;

         v2[0] += x1;
         v2[1] += y1;
         v2[2] += z1;

         v3[0] += x1;
         v3[1] += y1;
         v3[2] += z1;
         
         float dist = CollisionHelper.intersectRayTriangle(v1, v2, v3, vec, dir);
         if (dist < retdist && dist >= 0) {
            retdist = dist;
            float[] normal = CollisionHelper.getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]);
            lastTriangleData = new float[] { v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2], normal[0], normal[1], normal[2], i/3 };
         }
      }
      return retdist;
   }
   
   public ArrayList<HitTriangle> trimTriangleList(ArrayList<HitTriangle> triangles) {
      for (int i = triangles.size() - 1; i >= 0; i--) {
         if (i >= triangles.size())
            continue;
         HitTriangle tri = triangles.get(i);
         float num = tri.triangleData[12];
         for (int ii = triangles.size() - 2; ii >= 0; ii--) {
            if (triangles.get(ii).triangleData[12] == num) {
               triangles.remove(ii);
            }
         }
      }
      return triangles;
   }
   
   private static float degtorad(float degrees) {
      return degrees*3.14159f/180f;
   }
   private static float radtodeg(float rad) {
      return rad*180f/3.14159f;
   }
   
   public static float lengthdir_x(float length, float angle){
      return (float) (Math.cos(degtorad(angle))*length);
   }
   public static float lengthdir_y(float length, float angle){
      return (float) (-Math.sin(degtorad(angle))*length);
   }
   public static float point_distance(float x1,float y1,float x2,float y2){
      return (float) Math.sqrt(((x1-x2)*(x1-x2)) + ((y1-y2)*(y1-y2)));
   }
   public static float point_distance_3d(float x1,float y1, float z1,float x2,float y2, float z2){
      return (float) Math.sqrt(((x1-x2)*(x1-x2)) + ((y1-y2)*(y1-y2)) + ((z1-z2)*(z1-z2)));
   }
   public static float point_direction(float x1,float y1,float x2,float y2){
      return radtodeg((float) (Math.atan2(( y1 - y2),-(x1 - x2))));
   }

   /*
   Apply a rotation to a vertex,
   By Andrew Hamilton (orange451)
   */

   private static float[] applyRotations(float[] vertex, float xa, float ya, float za) {
      float x1 = vertex[0];
      float y1 = vertex[1];
      float z1 = vertex[2];
      if (xa!=0){
          float pd = point_distance_3d(x1, y1, z1, x1, 0, 0);
          float pa = point_direction(0, 0, z1, y1);
          z1=0+lengthdir_x(pd,pa-xa);
          y1=0+lengthdir_y(pd,pa-xa);
      }
      if (ya!=0){
         float pd = point_distance_3d(x1, y1, z1, 0, y1, 0);
         float pa = point_direction(0, 0, z1, x1);
          z1=0+lengthdir_x(pd,pa+ya);
          x1=0+lengthdir_y(pd,pa+ya);
      }
      if (za!=0){
         float pd = point_distance(x1,y1,0,0);
         float pa = point_direction(0, 0, x1,y1);
          x1=0+lengthdir_x(pd,pa+za);
          y1=0+lengthdir_y(pd,pa+za);
      }
     
      return new float[] {x1, y1, z1};
   }

   /*
   Triangle/triangle intersection test routine,
   by Tomas Moller, 1997.
   Ported to Java by by Andrew Hamilton (orange451)
   */

   protected static boolean checkTriangleCollision(float[] V0, float[] V1, float[] V2, float[] U0, float[] U1, float[] U2) {
      float[] E1, E2, N1 = {0, 0, 0};
      float d1, du0, du1, du2, du0du1, du0du2 = 0;

      E1 = SUB(V1, V0);
      E2 = SUB(V2, V0);
      N1 = CROSS(E1, E2);
      d1 = -DOT(N1, V0);
      du0 = DOT(N1, U0) + d1;
      du1 = DOT(N1, U1) + d1;
      du2 = DOT(N1, U2) + d1;
      du0du1 = du0 * du1;
      du0du2 = du0 * du2;

      if(du0du1>0.0f && du0du2>0.0f)
         return false;
     
      float[] N2 = {0,0,0};
      E1 = SUB(U1,U0);
      E2 = SUB(U2,U0);
      N2 = CROSS(E1,E2);
      float d2=-DOT(N2,U0);
      float dv0=DOT(N2,V0)+d2;
      float dv1=DOT(N2,V1)+d2;
      float dv2=DOT(N2,V2)+d2;
      float dv0dv1=dv0*dv1;
      float dv0dv2=dv0*dv2;

      if(dv0dv1>0.0f && dv0dv2>0.0f)
         return false;
     
      float[] D = CROSS(N1,N2); // {a, b, c}
     float max=Math.abs(D[0]);
      float bb=Math.abs(D[1]);
      float cc=Math.abs(D[2]);
      short index=0;
     
      if(bb>max) {
         max=bb;
         index=1;
      }else if(cc>max) {
         max=cc;
         index=2;
      }

      float vp0=V0[index], vp1=V1[index], vp2=V2[index], up0=U0[index], up1=U1[index], up2=U2[index], a = 0, b = 0, c = 0, x0 = 0, x1 = 0, d = 0, e = 0, f = 0, y0 = 0, y1 = 0;
     
      if(dv0dv1 > 0.0f){
         a=vp2;
         b=(vp0-vp2)*dv2;
         c=(vp1-vp2)*dv2;
         x0=dv2-dv0;
         x1=dv2-dv1;
      }else if(dv0dv2 > 0.0f){                                          
         a=vp1;
         b=(vp0-vp1)*dv1;
         c=(vp2-vp1)*dv1;
         x0=dv1-dv0;
         x1=dv1-dv2;
      }else if(dv1 * dv2 > 0.0f || dv0 != 0.0f){                            
         a=vp0;
         b=(vp1-vp0)*dv0;
         c=(vp2-vp0)*dv0;
         x0=dv0-dv1;
         x1=dv0-dv2;
      }else if(dv1 != 0.0f){                                          
         a=vp1;
         b=(vp0-vp1)*dv1;
         c=(vp2-vp1)*dv1;
         x0=dv1-dv0;
         x1=dv1-dv2;
      }else if(dv2 != 0.0f){                                          
         a=vp2;
         b=(vp0-vp2)*dv2;
         c=(vp1-vp2)*dv2;
         x0=dv2-dv0;
         x1=dv2-dv1;
      }else{
         return false;
      }
     
      if(du0du1 > 0.0f){
         d=up2;
         e=(up0-up2)*du2;
         f=(up1-up2)*du2;
         y0=du2-du0;
         y1=du2-du1;
      }else if(du0du2 > 0.0f){                                          
         d=up1;
         e=(up0-up1)*du1;
         f=(up2-up1)*du1;
         y0=du1-du0;
         y1=du1-du2;
      }else if(du1 * du2 > 0.0f || du0 != 0.0f){                            
         d=up0;
         e=(up1-up0)*du0;
         f=(up2-up0)*du0;
         y0=du0-du1;
         y1=du0-du2;
      }else if(du1 != 0.0f){                                          
         d=up1;
         e=(up0-up1)*du1;
         f=(up2-up1)*du1;
         y0=du1-du0;
         y1=du1-du2;
      }else if(du2 != 0.0f){                                          
         d=up2;
         e=(up0-up2)*du2;
         f=(up1-up2)*du2;
         y0=du2-du0;
         y1=du2-du1;
      }else{
         return false;
      }
     
      float[] isect1 = {0,0};
      float[] isect2 = {0,0};
     
      float xx=x0*x1,
      yy=y0*y1,
      xxyy=xx*yy,
      tmp=a*xxyy;
      isect1[0]=tmp+b*x1*yy;
      isect1[1]=tmp+c*x0*yy;
      tmp=d*xxyy;
      isect2[0]=tmp+e*xx*y1;
      isect2[1]=tmp+f*xx*y0;

       if(isect1[0] > isect1[1]){
           float c1=isect1[0];
           isect1[0]=isect1[1];
           isect1[1]=c1;
       }
           
       if(isect2[0] > isect2[1]){
           float c1=isect2[0];
           isect2[0]=isect2[1];
           isect2[1]=c1;
       }
       
      if(isect1[1] < isect2[0] || isect2[1] < isect1[0])
         return false;
     
      return true;
   }

   /*
   Ray/triangle intersection test routine,
   By Thomas Moller,
   Ported to Java by Andrew Hamilton (orange451)
   */

   private static float intersectRayTriangle(float vert0[], float vert1[], float vert2[], float[] orig, float dir[]){
      /*
       Arguments:
           0 vertex 1 (float array, length 3)
           1 vertex 2 (float array, length 3)
           2 vertex 3 (float array, length 3)
           3 ray vertex
           4 ray normal

       Returns:
           Returns the distance to the *closest* triangle that was hit. Returns 10000000 if no triangle was hit.
       */

      float[] edge1, edge2, tvec, pvec, qvec = {0, 0, 0};
      float det,u,v,invDet;
      float EPSILON = 0.000001f;
      edge1 = SUB(vert1, vert0);
      edge2 = SUB(vert2, vert0);
      pvec = CROSS(dir, edge2);
      det = DOT(edge1, pvec);
      if (det > -EPSILON && det < EPSILON)
         return LARGE_NUM;
      invDet = (float) (1d/det);
      tvec = SUB(orig, vert0);
      u = DOT(tvec, pvec) * invDet;
      if (u < 0.0f || u > 1.0f)
         return LARGE_NUM;
      qvec = CROSS(tvec, edge1);
      v = DOT(dir, qvec) * invDet;
      if (v < 0.0f || (u + v) > 1.0f)
         return LARGE_NUM;
      float dist = DOT(edge2, qvec) * invDet;
      if (dist < 0)
         return LARGE_NUM;
     
      return dist;
   }


   /*
   private static float intersectRayTriangle(float vert0[], float vert1[], float vert2[], float[] orig, float dir[]){
      float[] edge1, edge2, tvec, pvec, qvec = {0, 0, 0};
      float det,u,v;

      edge1 = SUB(vert1, vert0);
      edge2 = SUB(vert2, vert0);
      pvec = CROSS(dir, edge2);
      det = DOT(edge1, pvec);
      tvec = SUB(orig, vert0);
      u = DOT(tvec, pvec);

      if (det > 0.0001){
         if (u < 0.0 || u > det)
            return LARGE_NUM;
         qvec = CROSS(tvec, edge1);
         v = DOT(dir, qvec);
         if (v < 0.0 || u + v > det)
               return LARGE_NUM;
      }else if(det < -0.0001){
         if (u > 0.0 || u < det)
               return LARGE_NUM;
         qvec = CROSS(tvec, edge1);
         v = DOT(dir, qvec);
         if (v > 0.0 || u + v < det)
            return LARGE_NUM;
      }else{
         return LARGE_NUM;
      }
      float dist = DOT(edge2, qvec) * (1.0 / det);
      return dist;
   }
    */

   
   //Add two vectors together
  @SuppressWarnings("unused")
   private static float[] ADD(float[] v1, float[] v2) {
      float[] ret = new float[3];
      ret[0] = v1[0]+v2[0];
      ret[1] = v1[1]+v2[1];
      ret[2] = v1[2]+v2[2];
      return ret;
   }

   //Subtract two vectors
  private static float[] SUB(float[] v1, float[] v2) {
      float[] ret = new float[3];
      ret[0] = v1[0]-v2[0];
      ret[1] = v1[1]-v2[1];
      ret[2] = v1[2]-v2[2];
      return ret;
   }

   //Get dot product of two vectors
  private static float DOT(float[] v1, float[] v2) {
      return (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]);
   }

   
   private static float[] CROSS(float[] v1, float[] v2) {
      float[] ret = new float[3];
      ret[0] = v1[1]*v2[2]-v1[2]*v2[1];
      ret[1] = v1[2]*v2[0]-v1[0]*v2[2];
      ret[2] = v1[0]*v2[1]-v1[1]*v2[0];
      return ret;
   }
   
   public static float[] getLastTriangleData() {
      return CollisionHelper.lastTriangleData;
   }

   /*
    Calculate normal of a triangle,
    By Andrew Hamilton (orange451).
    */

   public static float[] getTriangleNormal(float argument0, float argument1, float argument2, float argument3, float argument4, float argument5, float argument6, float argument7, float argument8) {
      /*
       Arguments:
           0 x point0
           1 y
           2 z
           3 x point1
           4 y
           5 z
           6 x point2
           7 y
           8 z

       Returns:
           vector3f of the triangles' normal
       */


      float ax,ay,az,bx,by,bz,m,rx,ry,rz;

      //point0 -> point1
     ax = argument3-argument0;
      ay = argument4-argument1;
      az = argument5-argument2;

      //point0 -> point2
     bx = argument6-argument0;
      by = argument7-argument1;
      bz = argument8-argument2;

      //cross product
     rx = ay*bz-by*az;
      ry = az*bx-bz*ax;
      rz = ax*by-bx*ay;

      //magnitude
     m = (float) Math.sqrt(rx*rx+ry*ry+rz*rz);

      //normalize
     rx /= m;
      ry /= m;
      rz /= m;

      return new float[] {rx, ry, rz};
   }

   /*
    Check for collision between two vertices,
    By Andrew Hamilton (orange451).
    */

   public static int collision_line_3d(float x1, float y1, float z1, float x2, float y2, float z2, D3DModel d3dModel) {
      Vector temp = Vector.getDirection(new Location(null, x1, y1, z1), new Location(null, x2, y2, z2));
      float mag = temp.getMagnitude();
      float nx = temp.getX() / mag;
      float ny = temp.getY() / mag;
      float nz = temp.getZ() / mag;
      float raydis = collision_ray(d3dModel, 0, 0, 0, 0, 0, 0, x1, y1, z1, nx, ny, nz);
      if (raydis <= mag) {
          return 1;
      }
      return 0;
   }
}


It wont work without my other classes, but all the math you'll need is all there Wink
30  Game Development / Newbie & Debugging Questions / Re: Gouraud shade a model from an array of vertices? on: 2014-02-08 01:50:14
Thank you both Wink
That makes perfect sense.
Pages: [1] 2 3 4
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (75 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!