CodeWalrus

Development => PC, Mac & Vintage Computers => Topic started by: c4ooo on July 22, 2016, 07:40:10 pm

Title: c4's 3D Game Engine [3d][lwjgl][java]
Post by: c4ooo on July 22, 2016, 07:40:10 pm
I first did 3D programming in java the summer of 2014, but haven't really made any 3D games. Anyways, i decided to pick it up again, don't really know what type of game to make.
(https://codewalr.us/proxy.php?request=http%3A%2F%2Fimgur.com%2FYfOb033.png&hash=9687cfd2de8c29507700fa4ed384dbf7)
Title: Re: c4ooo does 3D programming again
Post by: Juju on July 22, 2016, 07:42:02 pm
Nice floor.
Title: Re: c4ooo does 3D programming again
Post by: DJ Omnimaga on July 23, 2016, 06:52:29 am
I'm definitively curious about what you'll come up with. Something themed around calc games would be cool actually (it doesn't have to be advanced, but for example a voxel version of Reuben 1 but with battles replaced with real time action like in Zelda)
Title: Re: c4ooo does 3D programming again
Post by: c4ooo on July 24, 2016, 05:47:40 pm
lolz math sucks

        longestLength = Math.max((float) Math.sqrt(Math.pow(x3 - x4, 2) + Math.pow(y3 - y4, 2) + Math.pow(z3 - z4, 2)),
                Math.max((float) Math.sqrt(Math.pow(x2 - x4, 2) + Math.pow(y2 - y4, 2) + Math.pow(z2 - z4, 2)),
                        Math.max((float) Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2) + Math.pow(z2 - z3, 2)),
                                Math.max((float) Math.sqrt(Math.pow(x1 - x4, 2) + Math.pow(y1 - y4, 2) + Math.pow(z1 - z4, 2)),
                                        Math.max((float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2)),
                                                (float) Math.sqrt(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2) + Math.pow(z1 - z3, 2)))))));

(This is one line broken into 6 for readability, it finds the longest of all lines connecting 4 vertices of a quad.)

Edit: got downwards collision detection working:
(https://codewalr.us/proxy.php?request=http%3A%2F%2Fimgur.com%2FfPX2PGf.png&hash=a140d93001d46721f59b286110f64a42)
It works by testing if a line segment, representing the direction of your movement, intersects with the plane of a face, and then testing if the point of intersection is within that face. I have to make it now so the angle of the face you're interacting with affects your movement (eg if you walk forward a ramp,  your body goes up, if you try walking up a really tall slope, like a wall, you move back. And of course i have to add collision detection in other direction :P
Title: Re: c4ooo does 3D programming again
Post by: DJ Omnimaga on July 24, 2016, 06:42:09 pm
Hm glad you are getting some collision detection to work. :)

Supersonic Ball 3D?
Title: Re: c4ooo does 3D programming again
Post by: Snektron on July 24, 2016, 09:29:10 pm
Quote from: c4ooo on July 24, 2016, 05:47:40 pm
lolz math sucks

        longestLength = Math.max((float) Math.sqrt(Math.pow(x3 - x4, 2) + Math.pow(y3 - y4, 2) + Math.pow(z3 - z4, 2)),
                Math.max((float) Math.sqrt(Math.pow(x2 - x4, 2) + Math.pow(y2 - y4, 2) + Math.pow(z2 - z4, 2)),
                        Math.max((float) Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2) + Math.pow(z2 - z3, 2)),
                                Math.max((float) Math.sqrt(Math.pow(x1 - x4, 2) + Math.pow(y1 - y4, 2) + Math.pow(z1 - z4, 2)),
                                        Math.max((float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2)),
                                                (float) Math.sqrt(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2) + Math.pow(z1 - z3, 2)))))));

(This is one line broken into 6 for readability, it finds the longest of all lines connecting 4 vertices of a quad.)


How about you break that up for readabillity:

double distance(float x0, float y0, float z0, float x1, float y1, float z1)
{
     float x = x0 - x1;
     float y = y0 - y1;
     float z = z0 - z1;
     return Math.sqrt(x*x + y*y + z*z);
}

double max(double... values)
{
    // Java 8
   return Arrays.stream(values).max().getAsDouble();

   // Java <= 7
   double max = Double.MIN_VALUE;
   for (double d : values)
       max = Math.max(d);
   return max;
}

(...)
    longestLength = (float) max
    (
        distance(x1, y1, z1, x2, y2, z2),
        distance(x2, y2, z2, x3, y3, z3),
        distance(x3, y3, z3, x4, y4, z4),
        distance(x4, y4, z4, x1, y1, z1),
    );
(...)


instead of the new max() fucntion you could also just use multple Math.max's. This should be a tiny bit faster due to less casting (though you could remove those in your version too). But its a lot more readable ;)
Title: Re: c4ooo does 3D programming again
Post by: c4ooo on July 27, 2016, 12:16:10 pm
Ok so, i want some volunteers to do some boring testing :)
So here's what to do: download the attachment, extract the zip, and double click on run.bat. The program should start! Don't touch any of the controls yet except the mouse yet! Your player should be falling down. You will either land on the pink thing or fall right through it. It will be nice if you could reply saying whether you fall through or land on it, as well as a copy of the output.txt file that should be created in the folder in which you extracted the zip. Sorry, so far its windows only :(
Title: Re: c4ooo does 3D programming again
Post by: Snektron on July 27, 2016, 12:32:22 pm
I won't be able to test it since im running Linux on this laptop :(
Title: Re: c4ooo does 3D programming again
Post by: E37 on July 27, 2016, 12:39:24 pm
I'll test it this evening!
Title: Re: c4ooo does 3D programming again
Post by: Snektron on July 27, 2016, 12:45:40 pm
Okay i managed to get it working. You have to download lwjgl 2.9.3 (https://sourceforge.net/projects/java-game-lib/files/Official%20Releases/LWJGL%202.9.3/)
Then extract lwjgl.jar, lwjgl_util.jar and jinput.jar to a folder called 'lib' next to 3D.jar. Then you copy the folder natives/<your platform> to a folder called 'natives' next to 3D.jar.
Then you can run it with

java -Djava.library.path="natives" -jar 3D.jar


The first time i landed on the platform, but the second and third time i fell through the world :(
Title: Re: c4ooo does 3D programming again
Post by: c4ooo on July 27, 2016, 01:10:35 pm
Ohh, i packaged the whole thing into a fat jar so the natives and libs should be inside the farjar?!?!?! ??? But lol i take pride in the fact that you actually got it to work xD (That sentence seems weird <_< )
Well, can you paste the output.txt of several tries please :) ?
Title: Re: c4ooo does 3D programming again
Post by: DJ Omnimaga on July 27, 2016, 03:39:43 pm
Quote from: c4ooo on July 27, 2016, 12:16:10 pm
Ok so, i want some volunteers to do some boring testing :)
So here's what to do: download the attachment, extract the zip, and double click on run.bat. The program should start! Don't touch any of the controls yet except the mouse yet! Your player should be falling down. You will either land on the pink thing or fall right through it. It will be nice if you could reply saying whether you fall through or land on it, as well as a copy of the output.txt file that should be created in the folder in which you extracted the zip. Sorry, so far its windows only :(
When I try to run run.bat, I get the following error:

QuoteError: Unable to access jarfile 3dtest.jar
Press any key to continue . . .


Looks like you got the file name wrong. ANyway I renamed the jar file and now I get this error @c4ooo

QuoteError: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more
Press any key to continue . . .


Windows 10 Pro (64-bits)
Intel Core i7 2.80 GHz
8 GB RAM
Nvidia GeForce GT 710 (2 GB video memory)
Title: Re: c4ooo does 3D programming again
Post by: c4ooo on July 27, 2016, 04:36:19 pm
Yea looks like i forgot to add the whole dam library  :-\ Why were you unable to access the jar though ???

Edit: yea, looks like i put the wrong jar file in the zip, hold on i'll fix it.
Title: Re: c4ooo does 3D programming again
Post by: DJ Omnimaga on July 27, 2016, 04:58:23 pm
Ah I see now. And yeah I thought the file name was wrong at first.
Title: Re: c4ooo does 3D programming again
Post by: c4ooo on July 27, 2016, 05:07:00 pm
This one should work: http://s000.tinyupload.com/?file_id=40630677022870276995
(File was to big to uploud to CW)
Title: Re: c4ooo does 3D programming again
Post by: DJ Omnimaga on July 27, 2016, 05:49:02 pm
There, it works. Thanks :)

As for what it does, I can walk anywhere and only fall down from one corner without walls. I don't fall down if I don't walk over that corner. Is that normal?
Title: Re: c4ooo does 3D programming again
Post by: c4ooo on July 28, 2016, 08:07:23 am
Quote from: DJ Omnimaga on July 27, 2016, 05:49:02 pm
There, it works. Thanks :)

As for what it does, I can walk anywhere and only fall down from one corner without walls. I don't fall down if I don't walk over that corner. Is that normal?

Well, coalition is still a bit rusty; the platform has a circular collision hitbox :P

Can you send me a copy of your log.txt (or output.txt, forgot what i called it :P ). It contains some numeric data. BTW, on any of your tries, have you fallen straight through the platform without being able to walk on it?

Major Edit:

Ok, so i practically solved collision. Every face is either a wall or floor tile, and each has a slightly different method of collision. For wall faces i just see if my X vector collides, move X if it doesn't, then same thing for Z. For floor collision, i get a list of faces on which i am standing on (usually just one) and then get the get the height for a particular X,Z on that floor face. So if i set a face as a floor face, i will always be able to walk up it, vs if i set it as a wall i will never be able to walk up it.
(https://codewalr.us/proxy.php?request=http%3A%2F%2Fimgur.com%2FDaj9w78.png&hash=f7e278a8c5538e91ca3739982ccfa0a2)
Also, all the quads still have a "circular" collision hitbox <_<
Title: Re: c4's 3D Game Engine
Post by: DJ Omnimaga on July 28, 2016, 12:13:04 pm
Are slopes supported?
Title: Re: c4's 3D Game Engine [3d][lwjgl][java]
Post by: c4ooo on July 29, 2016, 01:17:31 pm
Yes of course. IDK if i explained it well, but walls basically let/hinder your movement left/right/forward/backwards, but ignore Y. Floors force your Y to be the correct Y for your current X/Z. (So yes :) )
Title: Re: c4's 3D Game Engine
Post by: DJ Omnimaga on July 29, 2016, 04:43:03 pm
Wait, by ignoring Y, wouldn't slopes cause you to move towards the lower end of the slope but without actually going downwards?
Title: Re: c4's 3D Game Engine
Post by: ben_g on July 29, 2016, 05:15:56 pm
Quote from: DJ Omnimaga on July 29, 2016, 04:43:03 pm
Wait, by ignoring Y, wouldn't slopes cause you to move towards the lower end of the slope but without actually going downwards?

Y is the up axis in LWJGL, so what he means is that he calculates the height of the floor at the player's location and just sets the player's height to that value. This means that slopes work, but the (horizontal) speed of the player will always be the same regardless of the slopĂȘ's steepness (so a player would walk up a slope that goes 10m up and 1m forwards in the same time as a flat floor of 1m).
Title: Re: c4's 3D Game Engine
Post by: DJ Omnimaga on July 29, 2016, 08:09:01 pm
Oh ok, I thought he was refering to the Y location overall
Title: Re: c4's 3D Game Engine [3d][lwjgl][java]
Post by: c4ooo on August 01, 2016, 04:41:50 pm
Ime wondering what to do with the engine. I fear that unless i go for a minimalistic style the graphics/world will be sort of s***. 
Title: Re: c4's 3D Game Engine
Post by: DJ Omnimaga on August 01, 2016, 05:03:21 pm
Plain Jump would be cool. It would be better that you start small since you are new to 3D. I think low graphics are fine to start up. Later you could maybe do some sort of 3D Supersonic Ball clone?

Otherwise I don't have much ideas. Maybe a :walrii: maze game with the walruses as 2D sprites like DOOM and you have to find them?
Title: Re: c4's 3D Game Engine [3d][lwjgl][java]
Post by: c4ooo on August 02, 2016, 02:55:15 pm
I've implemented .obj model loading :)
(https://codewalr.us/proxy.php?request=http%3A%2F%2Fimgur.com%2FbBlRQwM.png&hash=4ab1fcbec76f7be006d3e63da2011cf2)
Collision detection is still s*** so (for different reasons) so i'll be working on that :)
Title: Re: c4's 3D Game Engine
Post by: DJ Omnimaga on August 02, 2016, 05:56:41 pm
Are those space ships? Glad to see models loading working. :)
Title: Re: c4's 3D Game Engine [3d][lwjgl][java]
Post by: c4ooo on August 03, 2016, 09:17:32 am
Lol no its a giant monkey head  ;)
(https://codewalr.us/proxy.php?request=http%3A%2F%2Fimgur.com%2FyjYMKPH.png&hash=758a0dcc8ed7bb60188b66512d6a413c)
Title: Re: c4's 3D Game Engine
Post by: ben_g on August 03, 2016, 10:12:33 am
Quote from: c4ooo on August 03, 2016, 09:17:32 am
Lol no its a giant monkey head  ;)
(https://codewalr.us/proxy.php?request=http%3A%2F%2Fimgur.com%2FyjYMKPH.png&hash=758a0dcc8ed7bb60188b66512d6a413c)

It's a standard mesh in Blender, intended for testing stuff like materials on a relatively complex surface, for those wondering.
Title: Re: c4's 3D Game Engine
Post by: DJ Omnimaga on August 03, 2016, 02:48:25 pm
Oh I see. By the way have you made any more progress on the map engine? I also wonder if textures would be easy to add at this point?
Title: Re: c4's 3D Game Engine [3d][lwjgl][java]
Post by: c4ooo on August 03, 2016, 04:41:21 pm
Not really, i implemented jumping though. Collision detection is still sort of a mess. (You commonly fall through floors if FPS drops). I'm trying to implement textures right now, but am having problems with paths in blender and some other openGL stuff :(
Title: Re: c4's 3D Game Engine
Post by: DJ Omnimaga on August 03, 2016, 04:48:13 pm
Hm you really need to make collision detection non-dependent on frame rate and performance. Maybe add frame skipping while still calculating positions but just not rendering the frame? Maybe someone else like @ben_g , @TheMachine02 , @tr1p1ea or @catastropher might be able to help, especially that they all worked on calculator-based 3D polygon engines.
Title: Re: c4's 3D Game Engine [3d][lwjgl][java]
Post by: c4ooo on August 03, 2016, 06:12:43 pm
Well the collision issue is caused becouse everything in the game moves faster as FPS drops. So if your usual speed is 1 m/s at 60fps, you will be going 6 m/s at 10fps. That way objects in the game always move at the same speed regardless of FPS, but this causes inacuracies.
Also, got textures working :)
(https://codewalr.us/proxy.php?request=http%3A%2F%2Fimgur.com%2F79H9vh2.png&hash=0bac20bf18965e372167b686d7344183)
Title: Re: c4's 3D Game Engine
Post by: DJ Omnimaga on August 03, 2016, 06:38:34 pm
What I would suggest is maybe add speed throttling l.

Also I lost. :P
Title: Re: c4's 3D Game Engine [3d][lwjgl][java]
Post by: Snektron on August 04, 2016, 11:47:30 am
You should make the update loop independent of the game loop, so the update loop runs at a constant speed and the render loop can vary. You could also use the time change since the last frame. For example if the last frame was 16 ms ago (a typical time in 60 fps games), and you have an object  that goes 1 m/s then you would add 0.016m.
Title: Re: c4's 3D Game Engine [3d][lwjgl][java]
Post by: c4ooo on August 04, 2016, 06:00:52 pm
Yea that's exactly what ime doing. "Speed is 1 m/s at 60fps, you will be going 6 m/s at 10fps" ;)
Title: Re: c4's 3D Game Engine
Post by: DJ Omnimaga on August 04, 2016, 08:25:21 pm
Sometimes it's better to not go overboard with the frame rate. You don't need more than 60 fps normally. :P
Title: Re: c4's 3D Game Engine
Post by: c4ooo on August 05, 2016, 05:37:31 am
The problem is happening because frame rates are *too low*. The higher the frame rate the more accurate the collision, as object move less per frame if frame rates are higher.
Title: Re: c4's 3D Game Engine [3d][lwjgl][java]
Post by: c4ooo on August 06, 2016, 11:34:51 am
Heres a picture of a bed model i made: (https://codewalr.us/proxy.php?request=http%3A%2F%2Fimgur.com%2FytlHbx5.png&hash=e4aef53680cc6d07d226ed1c34ffb0a0)
Ime still so f***ing unsure if i would be able to turn this into a good looking game :/
Title: Re: c4's 3D Game Engine
Post by: ben_g on August 07, 2016, 01:39:12 pm
Quote from: c4ooo on August 05, 2016, 05:37:31 am
The problem is happening because frame rates are *too low*. The higher the frame rate the more accurate the collision, as object move less per frame if frame rates are higher.

If 60fps is not enough, then you can 'simulate' higher framerates by handling the collision in multiple steps: If an object moves from point A to B, then first handle the collision as if the object moved to the location in the middle between A and B, and then handle it for location B if no collision occurred. You can increase the amount of steps as necessary, and you can even make it depend on the framerate.

If you don't reach 60fps, then you could try to optimize the rendering, since LWJGL should be more than capable of rendering a scene like that at 60fps even on very slow computers. If you don't have a lot of experience with shaders, then also try to find out more about how they work, since they work a bit differently than software that runs on the CPU. For example, branching (if's and loops) can cause a big performance drop in shaders, and it's often better to do extra calculations in order to avoid branching.
Title: Re: c4's 3D Game Engine
Post by: DJ Omnimaga on August 07, 2016, 04:26:26 pm
I like the bed. I think shading would be enough for it.