bump.lua - minimal collision detection lib

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

bump.lua - minimal collision detection lib

Post by kikito »

Hello there!

I've spent more time that what I wanted doing this, but I'm finally releasing this - another collision detection lib! It's called bump.lua

Note: This post is about an old version of bum. Post about bump.lua 2.0

The latest version can always be downloaded from github:

https://github.com/kikito/bump.lua

What does it do? Well, it does collision detection.

Comparison with HardonCollider

(TL;DR - if you are making a game with only bounding boxes, and you don't have incredibly fast objects, use bump. Otherwise, use HC)

So it is very similar to HardonCollider. But it does several things differently.
  • Well, first and foremost, bump only handles axis-aligned boxes. No polygons, no circles, not even rotated boxes. If you wanted to use bump.lua with those, you would have to implement the collision part for those objects yourself (or just use HardonCollider).
  • There is only one instance of bump at any given time. This means that you can't have several "worlds" occurring simultaneously, like you can in HC.
  • bump doesn't take velocity into account when making calculations. This means that if you have very fast objects, they might "go through" others. In bump.lua, you have to pad your object's velocity.
Those are the negative parts. The good:
  • The interface is simpler. For example, since there are only boundingboxes, you just have a method for adding stuff (bump.add) and one for removing stuff (bump.remove).
  • You don't need to use "bump objects" - you can use your own objects. The catch is that you have to tell bump how to get a bounding box for any object that you want to insert. By default bump will just try to call object:getBBox() to get the bounding box of the object (4 numbers - left, top, witdh, height). But you can change how the bounding box is calculated with the bump.getBBox callback.
  • You don't need to tell bump when an object has changed positions - it tracks object movement automatically.
  • The two main functions are called bump.collision(item1,item2,dx,dy) and bump.endCollision(item1, item2). Since there is only one instance of bump running at any given time, you can define them the same way you define LÖVE callbacks, without having to "pass them on to a constructor" (see the demo for an example).
  • Groups/Categories/etc are handled via a callback, called bump.shouldCollide(item1, item2). If you make it return false, item1 and item2 will not generate collisions.
  • You can use bump as a spatial hash - bump.each(f, l,t,w,h) will apply the function f to all the items in the box defined by l,t,w,h.
  • The "update" function in bump is called bump.collide(). Called without parameters, it will calculate all collisions. But if you pass it a region with l,t,w,h, it will calculate collisions in that region only.
  • There are some heuristics in how the order of the collisions executed. This should help when doing a platformer game, for example.
Demo

I'm attaching a platformer demo. It features a player character (move him with the arrows), a random blocky level, and 20 "gold coins" to pick up. The game is pretty much impossible if you just "jump around" to find the 20 gold coins. So I've included a "fly mode", which can be toggled on an off with the right shift key. Or you can try re-generating the level with the return key.

There is also a debug mode which will show you the underlying spatial grid, as well as the FPS/memory used (activate it with the TAB key).
bump-demo.png
bump-demo.png (13.12 KiB) Viewed 19989 times
My next step with this library is using it on battle-cry. Please try it out and let me know if you have any questions or comments.

EDIT: Attaching demo with v1.2. I'm leaving v1.0 attached for performance comparison
Attachments
bump-demo.love
v1.2 demo
(13.88 KiB) Downloaded 1257 times
bump-demo.love
v1.0 demo
(13.74 KiB) Downloaded 626 times
Last edited by kikito on Tue Jul 15, 2014 9:02 pm, edited 3 times in total.
When I write def I mean function.
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: bump.lua - minimal collision detection lib

Post by Zeliarden »

Yo! Cant move the player.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: bump.lua - minimal collision detection lib

Post by kikito »

What do you mean? Are you using the arrow keys?
When I write def I mean function.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: bump.lua - minimal collision detection lib

Post by TechnoCat »

The demo works great for me.
What is so great about these collision detection libraries is that they remove days of dev time from the beginning of projects.
User avatar
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

Re: bump.lua - minimal collision detection lib

Post by ishkabible »

Ya, I'm having major issues with the controls. as soon as it hit somewhere I couldn't move. I went into fly mode and then I could only go up; I couldn't fall
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: bump.lua - minimal collision detection lib

Post by Nixola »

I've got no problems at all with the demo, except for the FPS dropping down to 20 when I activate debug mode, but I'm on a netbook so it's ok
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: bump.lua - minimal collision detection lib

Post by dreadkillz »

Nice lib! The demo works fine for me (Windows 7 64)
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: bump.lua - minimal collision detection lib

Post by kikito »

ishkabible wrote:Ya, I'm having major issues with the controls. as soon as it hit somewhere I couldn't move. I went into fly mode and then I could only go up; I couldn't fall
That is weird. It means that there's something wrong with the left-right controls. I'll check it out.
When I write def I mean function.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: bump.lua - minimal collision detection lib

Post by dreadkillz »

Just a question from trying to learn from your code. I'm a little confused by your colon syntax. I thought it would be used to pass a table as an argument to the function, but you are using it to pass a string I think.

Here's the snippet from your love.draw:

Code: Select all

  love.graphics.print(("coins: %d"):format(player.coins), 10, 10)
So for the above code, you are passing ("coins: %d") to format? :crazy:
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: bump.lua - minimal collision detection lib

Post by Nixola »

Yes, it's passing a string to "string.format"; every string has a metatable with the field __index similar to this:

Code: Select all

function(t, i) return string[i] end
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests