[library] bump.lua v3.1.4 - Collision Detection

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:

[library] bump.lua v3.1.4 - Collision Detection

Post by kikito »

Hi there!

I am releasing a new major version of my collision detection library, bump!

The code and documentation can be found on github:

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

You can also find the code of the two demos. The simple one:

http://github.com/kikito/bump.lua/tree/simpledemo

Image

And the not so simple one:

http://github.com/kikito/bump.lua/tree/demo

Image

I have attached .love files for both on this post for your convenience.

Ok, so what's new?

I've tried to write a ChangeLog with all the details. As you can read there, some methods have changed their names / parameters / inner workings.

The most important change however, is the following:

This is the code needed to solve (simple) collisions in v 2.0.0:

Code: Select all

  if dx ~= 0 or dy ~= 0 then
    player.l, player.t = player.l + dx, player.t + dy

    local cols = world:move(player, player.l, player.t)

    -- slide the player on the first collision
    if cols[1] then
      local tl,tt,_,_,sl,st = cols[1]:getSlide()
      player.l, player.t = tl, tt
      world:move(player, player.l, player.t, player.w, player.h, {skip_collisions = true})
      player.l, player.t = sl, st
      cols = world:move(player, player.l, player.t)
    end

    -- touch on the rest of the collisions
    for _,col in ipairs(cols) do
      local tl, tt = cols[1]:getTouch()
      player.l, player.t = tl, tt
    end
    world:move(player, player.l, player.t, player.w, player.h, {skip_collisions = true})
  end
And this is the equivalent code in v3.0.0:

Code: Select all

  if dx ~= 0 or dy ~= 0 then
    player.x, player.y = world:move(player, player.x + dx, player.y + dy)
  end
I think it is very rad. Seriously, update today. You will have to change some things, but it's worth it.

I am new to this lib. Where do I begin?

Ok, here's a quick intro:

Code: Select all

-- The first thing you have to do is require the lib
local bump = require 'bump'

-- You then have to create at least 1 world (love.load is a good place to do this at the beginning)
local world = bump.newWorld()

-- You then add as many "items" as you want to the world. Items are represented as "rectangles" inside the world
local player = {x=100, y=100}
world:add(player, player.x, player.y, 32,32) -- x,y,w,h

-- You can do this many times (to add things like blocks etc)
local block = {x=200, y=200}
world:add(block, block.x, block.y, 64,64)

-- When you want to move the player (or any other item), inside love.update,
-- instead of changing player.x and player.y to the "goal", use world:move
local goalX, goalY = 120,100 -- this is where I want the player to go
local actualX, actualY = world:move(player, goalX, goalY)

-- bump.lua takes care of the collision detection for you - if there was a block in the path of player, actualX will be different from goalX.
-- update the player with that instead of goal:
player.x, player.y = actualX, actualY

-- Once an item is no longer needed you can remove it from the world:
world:remove(player)
That's the gist of it. There're ways to tweak how the collisions are handled etc. They are explained on the README. You can also use the simpledemo code as a place to start, and the demo code when you are ready for more advanced stuff.

Update: released 3.0.1 on github & the attached .love files. It fixes one bug on "cross" collisions.
Update: released 3.1.0, the "filter" param in world:move and world:check changes to filter(other) to filter(item, other) - this changes how the filters work, see the bump-demo or the docs for details. I am also including the platforms demo on this branch
Update: released 3.1.2, which includes world:getItems and world:countItems.
Update: released 3.1.3. It fixes a floating point related error, and adds some speed improvements when updating an item
Update: released 3.1.4. It removes one global variable (called '_')
Attachments
bump-platforms-3.1.0.love
(19.01 KiB) Downloaded 902 times
bump-simpledemo-3.1.0.love
(10.54 KiB) Downloaded 732 times
bump-demo-3.1.0.love
(114.48 KiB) Downloaded 788 times
Last edited by kikito on Wed Jun 03, 2015 9:44 am, edited 8 times in total.
When I write def I mean function.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: [library] bump.lua v3.0 - Collision Detection

Post by Kingdaro »

Excellent work, kikito. I'm so glad this library exists; making a simple platformer game from scratch is so unreasonably difficult, it's a little silly. The new API looks sick, keep up the good work. :awesome:
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [library] bump.lua v3.0 - Collision Detection

Post by kikito »

Thanks!

Your comments regarding the v2.0 interface where what made me release this new version. I hope you like it!
When I write def I mean function.
User avatar
drunken_thor
Citizen
Posts: 75
Joined: Wed Oct 01, 2014 12:14 am
Location: Toronto, Canada
Contact:

Re: [library] bump.lua v3.0 - Collision Detection

Post by drunken_thor »

It says something about a library when I had fun playing around with the demo. I like the simple work flow you created kikito! Awesome work as always.
Light_world.lua for all your lighting needs
Talkies for all dialog needs
Github
twitter
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: [library] bump.lua v3.0 - Collision Detection

Post by Jasoco »

Oh. Oh my.

The thing is, with 2.0 I needed that "complex" code to do other checks, like checking if I'm standing on ice or a conveyor belt. With the new "simple" code there doesn't seem to be any place to put that check code. Can the old methods still be used? I'd assume so.

Edit: Eh. Edited edit.

Edit again: After reading the instructions top to bottom I changed some stuff, but now notice that "getTouch" and "getSlide" don't exist anymore. So I guess I need to rewrite this thing even more than I was expecting to.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [library] bump.lua v3.0 - Collision Detection

Post by kikito »

Jasoco wrote:The thing is, with 2.0 I needed that "complex" code to do other checks, like checking if I'm standing on ice or a conveyor belt. With the new "simple" code there doesn't seem to be any place to put that check code. Can the old methods still be used? I'd assume so.
Hi, the old way of doing things can still be used - the old `check` is now called `project` and has slightly different parameters. The relevant code is in the new world:check.

That said, you can also do this:

Code: Select all

-- use world:move instead of world:check if you are ok moving the item in the world and then adjusting
local actualX, actualY, cols, len = world:move(item, goalX, goalY) 

-- And work with the collisions "after the fact" - i.e. modifying actualX on coveyor belts:
for i=1,len do
  local other = cols[i].other
  if other.isConveyorBelt then
    actualX = actualX + other.conveyorSpeed * dt
  end
end

-- Update with the modified data, taking collisions into account
world:update(item, actualX, actualY)
item.x, item.y = actualX, actualY
Jasoco wrote:Edit again: After reading the instructions top to bottom I changed some stuff, but now notice that "getTouch" and "getSlide" don't exist anymore. So I guess I need to rewrite this thing even more than I was expecting to.
Collisions have a "slide" and "bounce" attribute now instead of method. But they get them when they are resolved instead of when they are detected - it's faster this way. You will need to trigger world:check or world:move as mentioned above in order to resolve them.

Let me know if you need help translating the code, I will give you a hand.
When I write def I mean function.
User avatar
joedono
Prole
Posts: 40
Joined: Mon Mar 04, 2013 6:58 pm

Re: [library] bump.lua v3.0 - Collision Detection

Post by joedono »

I'm not sure if I found an issue or if I'm doing something wrong. I was just playing around with the library. Below is my main.lua code. The player is a red box that can run through the green boxes. Colliding with the green boxes will reduce the green box's health until it "dies". The green box's color gets duller as its health falls. When it dies, it is removed from the bump world and the game.

The weird part is that collisions will be generated for boxes the player object doesn't appear to be colliding with. I can't figure out a pattern, but it only happens while the player is actively moving or when the player stops on a border between two or more boxes. One reliable way to see this is to hold the right and down arrows once the demo starts. You'll see that the green boxes in the upper left continue to lose health until they disappear or until the player stops generating collisions by exiting the area at the lower right.

Code: Select all

local bump = require("lib/bump");

PLAYER_HEIGHT = 20;
PLAYER_WIDTH = 20;
PLAYER_SPEED = 200;

WALL_HEIGHT = 30;
WALL_WIDTH = 30;
WALL_HEALTH = 100;

wallConfig = {
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

world = bump.newWorld(20);

playerFilter = function(other)
	if(other.bType == 'wall') then
		return 'cross';
	end
	
	return nil;
end

function love.load()
	player = {
		x = 0,
		y = 0,
		w = PLAYER_WIDTH,
		h = PLAYER_HEIGHT,
		bType = 'player'
	};
	
	world:add(player, player.x, player.y, player.w, player.h);
	
	walls = {};
	
	for i, wallB in ipairs(wallConfig) do
		for j, wallC in ipairs(wallB) do
			if(wallC == 1) then
				wall = {
					x = i * WALL_WIDTH,
					y = j * WALL_HEIGHT,
					w = WALL_WIDTH,
					h = WALL_HEIGHT,
					health = WALL_HEALTH,
					alive = true,
					bType = 'wall'
				};
				
				world:add(wall, wall.x, wall.y, wall.w, wall.h);
				
				table.insert(walls, wall);
			end
		end
	end
end

function love.update(dt)
	local dX, dY = 0, 0;
	
	if(love.keyboard.isDown("left")) then
		dX = -PLAYER_SPEED * dt;
	end
	
	if(love.keyboard.isDown("right")) then
		dX = PLAYER_SPEED * dt;
	end
	
	if(love.keyboard.isDown("up")) then
		dY = -PLAYER_SPEED * dt;
	end
	
	if(love.keyboard.isDown("down")) then
		dY = PLAYER_SPEED * dt;
	end
	
	local actualX, actualY, cols, len = world:move(player, player.x + dX, player.y + dY, playerFilter);
	
	player.x = actualX;
	player.y = actualY;
	
	for i = 1, len do
		local other = cols[i].other;
		other.health = other.health - 1;
		
		if(other.health <= 20) then
			other.alive = false;
			world:remove(other);
		end
	end
	
	for index, wall in ipairs(walls) do
		if(wall.alive == false) then
			table.remove(walls, index);
		end
	end
end

function love.draw()
	for i, wall in ipairs(walls) do
		love.graphics.setColor(0, wall.health + 100, 0);
		love.graphics.rectangle("fill", wall.x, wall.y, wall.w, wall.h);
		love.graphics.setColor(200, 200, 200);
		love.graphics.rectangle("line", wall.x, wall.y, wall.w, wall.h);
	end
	
	love.graphics.setColor(150, 0, 0);
	love.graphics.rectangle("fill", player.x, player.y, player.w, player.h);
	love.graphics.setColor(255, 0, 0);
	love.graphics.rectangle("line", player.x, player.y, player.w, player.h);
end
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: [library] bump.lua v3.0 - Collision Detection

Post by Doctory »

kikito you are a god
i just have one small question
is the filter how we want to resolve the collision?
ex. if we set it to bounce, to object colliding will bounce?
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: [library] bump.lua v3.0 - Collision Detection

Post by Roland_Yonaba »

That is just fantastic.
The demo just blew my mind. Fantastic.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: [library] bump.lua v3.0 - Collision Detection

Post by kikito »

@joedono:

I've tried your code and did not find any obvious problem. I confirm I can reproduce the issue - I am investigating this a bit more, trying to see exactly what happens. Thanks a lot for taking the time to send me the code. It might an error I introduced when I renamed l & t to x & y.

@Doctory

The answer is yes, but it's subtle. If you only use world:move or world:check, then yes, the filter "resolves" the collisions. Note that "bouncing" is just a type of collision, and it might be different from the one you expect in a physics-enable thing. What it does is "preserve the distance travelled", but "reflecting the direction". Physics-based collisions take things like mass and elasticity into account.

@Roland

Thanks :)
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: No registered users and 45 guests