Hello,
currently I'm trying to figure out how to check collision for an object without physics. The only problem I have at the moment is that I want an object to stop moving as soon as it hits any object, no matter which one. With the code I have at the moment I have to check for every single object if it collides with it. Is there a way to check if the object collides with any other object?
Thank you in advance.
Comments
https://github.com/kikito/bump.lua
Likes: rolfpancake, antix, misterhup
https://github.com/bakpakin/Splash.lua
But Bump is very powerful!
RenderLevel - Draws a tiled map. This class is based on the Desert example. Call render(map) to render a tiled map. It just draws tilemaps and in the code it has placeholder code for processing object and image layers as well. Subsequent calls to render() will attempt to dispose of any previously created resources.
CollisionMaker - Creates collision rectangles from a tiled map and adds them to a bump collision world. The main function you will call is createRectangles(map, name). map is a loaded tiled map and name is the name of the layer you want to make rectangles from. The function its-self It makes two passes over the layer, with the 1st pass creating vertical rectangles, and the 2nd pass creating horizontal rectangles. Each rectangle is created with a isWall bool variable to make it easy to differentiate them from other collision objects in bump. NOTE that the function treats any non zero tile as solid. Finally it adds the created rectangles to the bump collision world. Subsequent calls to createRectangles() will remove any previously added collision rectangles from the bump world. You can also just call reset() if you just want to clear the generated data.
Here is an example of how it works..
EDIT: See further down the thread for the updated example.
Likes: rolfpancake, ar2rsawseen, muro, pie, john26, jdbc
Another approach is to divide the game area into a grid and assign each object to one cell in the grid. Eg it might be quite coarse like 3x3 and A is in cell (1,2) and B is in cell (3,3). In this case you can be sure A and B are not colliding as they are in different cells (and not neighbouring cells). The disadvantage of this is you need to constantly update the cell index number for each object but it means many collision tests can be avoided.
The point is you can write your own collision detection code right in Lua. It is not necessarily better to use Box2D or other libraries. If you are doing something simple, complex libs could slow things down while 2-3 lines of Lua code is often faster.
This tutorial provides great info on collision detection.
http://www.metanetsoftware.com/technique/tutorialA.html
Likes: rolfpancake, antix
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
I still wholeheartedly recommend bump for grid based games. I twrote my own collision engine and whilst it is pretty good, it doesn't come close to bump by a long way. If I had discovered bump before writing my own collision engine, I would have saved 3 weeks heheh. Good article link
If you want to go down the SAT path then here are some other links.
https://bitbucket.org/RudenkoPaint/sat-lua - I haven't looked at this closely but it's another library from the looks of it.
http://www.phailed.me/2011/02/polygonal-collision-detection/ - This one has a simple explanation of SAT and has LUA source code too.
There is also a rather cool library called HardonColider which uses SAT. Its hard to get working with Gideros (don't even go there) but it has some great code and there's a lot to learn from reading through it. https://vrld.github.io/HardonCollider/.
You can find a few others collision libraries at https://love2d.org/wiki/Category:Libraries. In general it's a great place to go (check out their forums too) to find code and stuff. Love2D is LUA based and "most" of the things you will find there are fairly easy to get working with Gideros (except HardonCollider ROFLS)
And here's a neat little blog about circle collision/resolution with LUA code too.. http://blogs.love2d.org/content/circle-collisions
And finally this series of articles is pretty good at showing how to make a Mario style platform collision engine... https://www.raywenderlich.com/15230/how-to-make-a-platform-game-like-super-mario-brothers-part-1
Likes: rolfpancake
If you run the attached project you can click and drag the ball around the screen, which will turn red when it collides with a shape. The ball will also be pushed out of any shape that it collides with and a little white arrow will show the direction of the collision.
EDIT: See further down the thread for the updated Hardon Collider example.
Likes: rolfpancake, muro, dreiko65, pie
Thanks a ton for your links and the attached project!
(A central place for tutorial links would be great anyway)
Likes: john26
Likes: antix
@misterhup has another discussion about collisions and I posted another example project there. http://giderosmobile.com/forum/discussion/6357/bump#Item_1
I'll re-post it here so all my examples are in the one thread....
Okay here is a little example project that can detect collisions between rotated rectangles. The collision stuff is from this page.. https://bitbucket.org/RudenkoPaint/sat-lua. The version included here has been modified slightly to better work with Gideros. It's a bit messy but it works.
When you run the example project you can click and drag one of the boxes around. If it collides with the other, both turn red.
NOTE: The example is fairly basic and does not perform any collision resolution but with a bit of work it could easily do so. The "response" table in the main loop contains pretty much everything you need to resolve the collisions.
Anyway, it should be enough to get you up and running
EDIT: See further down the thread for the updated example..
Likes: totebo, misterhup, rolfpancake, dreiko65
Likes: antix
at the end of the day, if you aren't rotating rectangles, then use bump! It's just fantastic!
I updated my example while I was away for the wekeend (as well as my examples in the other thread (Hardon Collider, and Bump Collision Maker). I've added some small documentation to the top of the code and also in the classes I have provided. Most of the classes have been revamped and I'm pretty happy how they ended up. I'm surprised at how much I got done without any internet to distract me heheh.
Here is the latest incarnation of the SAT example..
Likes: totebo, misterhup, rolfpancake
Likes: pie, jdbc
Likes: misterhup, pie
Out of SAT and Hardon, which is more efficient when it comes to rectangle collisions?
Hardon Collider actually uses GJK to detect rectangle collisions. It also utilizes a spatial hash like bump to increase it's performance. In my own tests I found Hardon Collider kind of slow but I was using 640 polygons for my walls in that experiment.
the SAT example has no spatial hash so if you get to larger numbers of polygons the performance will degrade unless you implement some spatial hash or other algorithm to reduce collision checks.
For an isometric game, bump should be all you require. I would think all collisions get handled by bump and then world objects are translated when drawn to appear to be in the correct perspective.
It's an interesting question (about isometric collision) and one I could easily get side-tracked by [-X
@jdbc shares some code for translation in his thread http://giderosmobile.com/forum/discussion/6054/isometric-endless-racing#Item_1 Check it out
Likes: jdbc
I think it deserves to be moved to the Code Snippet section of the forum (if not included in a resource list inside the docs) to be easily found later.
Likes: totebo, rolfpancake
Likes: antix
I now need to do something slightly different; skewing the perspective. With your Legendary Antix Hat on, how would I even start doing something like this? See illustration.
The actual collisions can still take place in the bump world and you can do some mathsy translation to determine where the objects should be drawn onscreen I think.
So I took a break and made an example. There are some actors which move about the screen and bounce off the walls and each other. They are drawn in a skewed perspective.
Likes: MoKaLux