[library] bump 2.0 – Collision Detection
Posted: Fri May 30, 2014 11:43 pm
Hi.
This is an older version of bump. Click here to go to the new version
Some of you have been asking how to do collision detection in LÖVE. This might interest you.
But first, do you like eye candy? Me too. Here:
What you see above is the demo of bump 2.0. It is attached at the end of this post. (Go try it, I managed to add some nice music).
bump is a collision detection library. It has changed so much since bump 1.0 that I decided to open a new thread instead of continuing with the old one.
bump only works with axis-aligned bounding boxes (rectangles). If your game needs polygons instead, I recommend giving a look to HardonCollider. If you need complex, realistic physics, then use [wiki]love.physics[/wiki].
In order to use bump 2.0, you first create a world:
Then you populate the world with "items". Items are "your game objects", like the player, the scenario tiles, enemies or missiles. You add them to the world with world:add. For each item that you add, you must provide the coordinates of the {left,top} coordinates as well as the width and height of each item. Usually, items are tables.
If you want to tell the world that an item has moved, you can do so with world:move(item, new_left, new_top).
world:move always moves the item to the new coordinates, ignoring all collisions. If you want to detect collisions, you must call world:check(item, new_left, new_top) before moving. It returns a list of collisions, and its length. There are various ways you can operate with that list of collisions. Here's an example that will move the player only until it "touches" the enemy:
You can remove items from the world with world:remove(item).
That's the basics. The README on the github page has much more information:
https://github.com/kikito/bump.lua
You can see the source code of the demo in the "demo" branch:
https://github.com/kikito/bump.lua/tree/demo
Also, there is a simplified example called "simple demo". Here's the branch:
https://github.com/kikito/bump.lua/tree/simpledemo
I have included a .love for it, too.
Enjoy!
This is an older version of bump. Click here to go to the new version
Some of you have been asking how to do collision detection in LÖVE. This might interest you.
But first, do you like eye candy? Me too. Here:
What you see above is the demo of bump 2.0. It is attached at the end of this post. (Go try it, I managed to add some nice music).
bump is a collision detection library. It has changed so much since bump 1.0 that I decided to open a new thread instead of continuing with the old one.
bump only works with axis-aligned bounding boxes (rectangles). If your game needs polygons instead, I recommend giving a look to HardonCollider. If you need complex, realistic physics, then use [wiki]love.physics[/wiki].
In order to use bump 2.0, you first create a world:
Code: Select all
local bump = require 'bump'
local world = bump.newWorld()
Code: Select all
local player, enemy = ... -- create player and enemy
world:add(player, 100,200, 32, 32) -- Add the player in 100,200, as a square of 32x32
world:add(enemy, 0,0, 64,64) --
Code: Select all
world:move(player, 100,300) -- tell the world that the player has moved to 100,300
Code: Select all
local cols, len = world:check(player, 0,0)
if len == 0 then
world:move(player, 0, 0)
else
local tl,tt = cols[1]:getTouch()
print("Collided with " .. tostring(cols[1].other))
world:move(player, tl,tt)
end
That's the basics. The README on the github page has much more information:
https://github.com/kikito/bump.lua
You can see the source code of the demo in the "demo" branch:
https://github.com/kikito/bump.lua/tree/demo
Also, there is a simplified example called "simple demo". Here's the branch:
https://github.com/kikito/bump.lua/tree/simpledemo
I have included a .love for it, too.
Enjoy!