XL2
Established Member
Hi all!
One of the hardest thing in making homebrew 3D games is the collision detection. So I did a quick FPS-style demo (with source code, done in a few hours only so don't expect Half-Life on the Saturn) with working collision detection (still buggy) and a tool to generate the bounding boxes and slopes (buggy as well, but works nicely for axis-aligned polygons). Not directly related to my game, Sonic Z-Treme, but I will continue to update the tool I created for the bounding boxes and hopefully it will help someone with his project. I'm using Jo Engine for this, but with some minor modifications it could work with SGL. I didn't use Rockin'B's tool / code, but liked the idea of using the MDL/H files directly to output the collision data, so I took inspiration from his work in a way.
There are currently several limitations and the tool isn't complete (I'm in the middle of adding features such as writing to binary), but I guess maybe that could be interresting for some of you here since I included the source code. It works better for a game like Sonic X-Treme/Z-Treme, corridor shooters like Doom/Quake or games like Resident Evil. It wouldn't work too well for a game like Mario 64 since it won't play nice with things like hills and rocks with multiple faces. It works better with axis-aligned quads (or slope on one axis). You could also just use a complex heigth mask for hills and rocks, but right now for memory constraints I use small arrays (more on that later). So in other words, it could be modified for more complex geometry, but my hands are full right now so I don't think I will do it.
Hopefully it will help someone start his/her own 3D game project on the Saturn.
Video of the short demo (I say again, I quickly coded it, so it's nothing impressive!) :
For the tool, first I want to apologize, the code is full of commented code that I didn't fully remove since I might use some in the future, and it's not user friendly. It shouldn't be that hard to use, but it requires a few things.
First, make sure you link it up with the SGL library (SGL.h) (Edit : I removed the need to make the tool point to SGL.h, but didn't test it properly so if you have issues make sure you tell me) as I'm using that to read the polygons. Then include your level's .h file (just convert it with the Jo Engine map editor, without the textures) (EDIT : The tool now accepts .OBJ files directly. I haven't done much testing, but the maps in the FPS demo are using the resulting files and seems to be OK).
If you use a level's h file, call the JO display command (it won't display anything, I just use that to initialize the polygon list - it avoids having to modify the .h file and remove one step for you).
If you are using OBJ file, just put the correct filename in the main function.
The tool needs to be compiled. It outputs a file, I just named it TESTPOLY2BB.txt, with examples of the struct I use. Look at the FPS demo, in MESHES/LVL_BB.h and Collision.c to see how it works. I use Code::Blocks to compile, but it should work with anything.
The tool works by reading the polygons, creating bounding boxes and then merging the boxes with neighbours to reduce the amount the Saturn needs to go throught (it could be reduced further than now, on my to do list). It also reads the normals to determine if it's a slope, then calculates the slope (still buggy, I will have to improve it). It also adds "depth" to the polygon to avoid going throught it if you go too fast.
I will have to improve my slope generation code as it's hit and miss but I didn't put much time in it yet. It also doesn't generate heigth masks yet, but I've included examples in the FPS demo on how to use masks, so it shouldn't be too hard.
For the FPS demo and code, just put it in your Jo Engine Src folder next to the demos, from there you should be able to open the code and modify it and compile it. And once more : it's quickly coded, so some things are probably wrong/stupid, if I update it I will share with you guys.
Again, sorry for the not-so-user-friendly-hackjob of a code!
EDIT : Updated the collision tool (I will rename it soon enough) to convert .OBJ files to binary file format. The FPS demo now accepts the binary files (.ZTM) to load the maps. Press L/R + START to switch maps. Note that the slopes in map 2 are wrong and the collision tool is still having issues with them (I haven't tried to fix this yet). I also disabled the UseNearClip as it doesn't play nicely with FPS unlike the Sonic game. I will try to find a fix for that. I also added different textures in map 2, including cheap texture animation (temporary solution, it works, but it's not great, I will add something inside the map tool to generate it or change the function to be more flexible).
EDIT (2017-08-10) : Did a quick update to make everything smoother. I fixed the look up/down (now it's smooth and looks OK) and also fixed the camera movement when you are "under" a tile (such as when you climb stairs on Map 2) to make it smoother. Also added gouraud shading on the whole levels.
EDIT (2017-08-14) : Many bugs fixed. Obj to binary now works without known issues. I also added basic view frustum culling in the FPS demo, auto-crouch (buggy, you can try it in the "air duct" on map 2, near the top), a NBG1 HUD (ugly and done in 5 minutes - if you want back the NBG2 and RBG0, just uncomment the lines in Display.c). I tried it on real hardware and had to reduce the draw distance to get decent framerate (around 60, but lots of slowdowns on map 2). Removing the "flame" background and RBG0 helps to hide the short draw distance since everything just fades to black. There seem to be an issue since putting the game at 30 FPS has the same results as 60 FPS and no advanges. Also, if you try map 3, it crashes on real hardware and in Yabause, but works in SSF. I still haven't implemented proper BSP for the collision code, so that might be part of the issue, but I know the current memory allocation while loading is bad as there are many gaps between the meshes.
EDIT (2017-08-16) : Upgraded to Jo Engine 9.0 (make sure you upgrade also). I also fixed the memory allocation, so you should be able to load much bigger files now (try map 3 to see).
EDIT (2017-08-25) : You can now write textures directly in the binary file. The map program will read TGA textures (uncompressed only) and write them. Also added auto-subdivision of the map for culling, you just need to specify how many "cuts" you want (there is a bug that will sometimes "lose" polygons, so just try different values for "cuts" (x, y, z axis) if one setting doesn't work well). I also changed the FPS demo's collision code to reduce CPU usage.
One of the hardest thing in making homebrew 3D games is the collision detection. So I did a quick FPS-style demo (with source code, done in a few hours only so don't expect Half-Life on the Saturn) with working collision detection (still buggy) and a tool to generate the bounding boxes and slopes (buggy as well, but works nicely for axis-aligned polygons). Not directly related to my game, Sonic Z-Treme, but I will continue to update the tool I created for the bounding boxes and hopefully it will help someone with his project. I'm using Jo Engine for this, but with some minor modifications it could work with SGL. I didn't use Rockin'B's tool / code, but liked the idea of using the MDL/H files directly to output the collision data, so I took inspiration from his work in a way.
There are currently several limitations and the tool isn't complete (I'm in the middle of adding features such as writing to binary), but I guess maybe that could be interresting for some of you here since I included the source code. It works better for a game like Sonic X-Treme/Z-Treme, corridor shooters like Doom/Quake or games like Resident Evil. It wouldn't work too well for a game like Mario 64 since it won't play nice with things like hills and rocks with multiple faces. It works better with axis-aligned quads (or slope on one axis). You could also just use a complex heigth mask for hills and rocks, but right now for memory constraints I use small arrays (more on that later). So in other words, it could be modified for more complex geometry, but my hands are full right now so I don't think I will do it.
Hopefully it will help someone start his/her own 3D game project on the Saturn.
Video of the short demo (I say again, I quickly coded it, so it's nothing impressive!) :
For the tool, first I want to apologize, the code is full of commented code that I didn't fully remove since I might use some in the future, and it's not user friendly. It shouldn't be that hard to use, but it requires a few things.
If you use a level's h file, call the JO display command (it won't display anything, I just use that to initialize the polygon list - it avoids having to modify the .h file and remove one step for you).
If you are using OBJ file, just put the correct filename in the main function.
The tool needs to be compiled. It outputs a file, I just named it TESTPOLY2BB.txt, with examples of the struct I use. Look at the FPS demo, in MESHES/LVL_BB.h and Collision.c to see how it works. I use Code::Blocks to compile, but it should work with anything.
The tool works by reading the polygons, creating bounding boxes and then merging the boxes with neighbours to reduce the amount the Saturn needs to go throught (it could be reduced further than now, on my to do list). It also reads the normals to determine if it's a slope, then calculates the slope (still buggy, I will have to improve it). It also adds "depth" to the polygon to avoid going throught it if you go too fast.
I will have to improve my slope generation code as it's hit and miss but I didn't put much time in it yet. It also doesn't generate heigth masks yet, but I've included examples in the FPS demo on how to use masks, so it shouldn't be too hard.
For the FPS demo and code, just put it in your Jo Engine Src folder next to the demos, from there you should be able to open the code and modify it and compile it. And once more : it's quickly coded, so some things are probably wrong/stupid, if I update it I will share with you guys.
Again, sorry for the not-so-user-friendly-hackjob of a code!
EDIT : Updated the collision tool (I will rename it soon enough) to convert .OBJ files to binary file format. The FPS demo now accepts the binary files (.ZTM) to load the maps. Press L/R + START to switch maps. Note that the slopes in map 2 are wrong and the collision tool is still having issues with them (I haven't tried to fix this yet). I also disabled the UseNearClip as it doesn't play nicely with FPS unlike the Sonic game. I will try to find a fix for that. I also added different textures in map 2, including cheap texture animation (temporary solution, it works, but it's not great, I will add something inside the map tool to generate it or change the function to be more flexible).
EDIT (2017-08-10) : Did a quick update to make everything smoother. I fixed the look up/down (now it's smooth and looks OK) and also fixed the camera movement when you are "under" a tile (such as when you climb stairs on Map 2) to make it smoother. Also added gouraud shading on the whole levels.
EDIT (2017-08-14) : Many bugs fixed. Obj to binary now works without known issues. I also added basic view frustum culling in the FPS demo, auto-crouch (buggy, you can try it in the "air duct" on map 2, near the top), a NBG1 HUD (ugly and done in 5 minutes - if you want back the NBG2 and RBG0, just uncomment the lines in Display.c). I tried it on real hardware and had to reduce the draw distance to get decent framerate (around 60, but lots of slowdowns on map 2). Removing the "flame" background and RBG0 helps to hide the short draw distance since everything just fades to black. There seem to be an issue since putting the game at 30 FPS has the same results as 60 FPS and no advanges. Also, if you try map 3, it crashes on real hardware and in Yabause, but works in SSF. I still haven't implemented proper BSP for the collision code, so that might be part of the issue, but I know the current memory allocation while loading is bad as there are many gaps between the meshes.
EDIT (2017-08-16) : Upgraded to Jo Engine 9.0 (make sure you upgrade also). I also fixed the memory allocation, so you should be able to load much bigger files now (try map 3 to see).
EDIT (2017-08-25) : You can now write textures directly in the binary file. The map program will read TGA textures (uncompressed only) and write them. Also added auto-subdivision of the map for culling, you just need to specify how many "cuts" you want (there is a bug that will sometimes "lose" polygons, so just try different values for "cuts" (x, y, z axis) if one setting doesn't work well). I also changed the FPS demo's collision code to reduce CPU usage.
Attachments
Last edited: