Hardoncollider with tilemap

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Hardoncollider with tilemap

Post by Refpeuk »

Hey guys,

I'm working on a project that uses a tilemap, and right now my code creates a rectangle for each tile for the player to collide with. I keep them each small enough that they don't collide with each other to improve framerate, but close enough that the player still cannot pass through them. With only a 20x20 tilemap my framerate drops to 6. (3 if I leave the tiles big enough to collide with each other) Is there something else I need to do? I can upload my .love if you want, but what I am doing is fairly simple, just an extra step that resembles the draw loop to create a rectangle for each tile.

Do I need to write an algorithm to create a single collision object for the whole tilemap (or each clump of collidable tiles)? If that's what I need to do does anyone know how to do that?

Thanks
It was the best of times, it was the worst of times . . .
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Hardoncollider with tilemap

Post by vrld »

There's HC:setPassive(shape, ...).
This enables you to significantly speed up the collision detection. Typical candidates for passive shapes are those which are numerous, but don't act in themselves, e.g. the level geometry.
Sometimes it will also help to increase the cell size (in the constructor).

You should upload the .love anyway, maybe there's something else that could be done better.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Hardoncollider with tilemap

Post by TechnoCat »

I found Hardon Collider unsuitable for tiled maps when I was creating a Bomberman clone.
Using groups and settings shapes as ghosts or passive helps a lot though.
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: Hardoncollider with tilemap

Post by Refpeuk »

OK, I added it to my code so it now looks like this:

Code: Select all

for x = 1, shipmap.width do
        for y = 1, shipmap.height do
            if shipmap[x][y].type ~= 0 then
                shipmap[x][y].col = collider:addRectangle(x*img.tileset1.tilesize, y*img.tileset1.tilesize,
                    img.tileset1.tilesize, img.tileset1.tilesize)
                HC:setPassive(shipmap[x][y].col)
            end
        end
    end
Unfortunately, I get this error: Attempt to call method 'setPassive' (a nil value).
It was the best of times, it was the worst of times . . .
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Hardoncollider with tilemap

Post by tentus »

Edit: fixed for posterity.

Don't call setPassive on HC, call it on the collider. collider:setPassive(shipmap[x][y].col)
Last edited by tentus on Mon Mar 26, 2012 11:20 pm, edited 2 times in total.
Kurosuke needs beta testers
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Hardoncollider with tilemap

Post by vrld »

The code is correct (as documented), at least with newish HC versions. However, from what you've posted it's not possible to tell what is going wrong. Please post a .love.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: Hardoncollider with tilemap

Post by Refpeuk »

OK, attached a .love.

There are some things there that are only placeholder, or not used yet. You can probably easily tell which parts are mean to be fairly permanent and which are just temporary solutions. Please let me know how my coding style is, I'm still a beginner.

Thanks!
Attachments
tilemap.love
(47.19 KiB) Downloaded 101 times
It was the best of times, it was the worst of times . . .
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Hardoncollider with tilemap

Post by TechnoCat »

tentus wrote:Don't call setPassive on HC, call it on the thing that is becoming passive. (shipmap[x][y].col)
To clarify: shapes are separate from HC. Setting a shape passive wouldn't make sense. You tell HC a certain shape that will be hashed to a unique ID to be passive inside of HC.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Hardoncollider with tilemap

Post by tentus »

Sorry, I changed my mind about how I wanted to word my post and posted it without finishing my changes (I really don't know why).

As is often the case, collider and HC have gotten mixed around. If you change line 136 of world.lua to this:

Code: Select all

collider:setPassive(shipmap[x][y].col)
you won't get a bluescreen.
Kurosuke needs beta testers
User avatar
Refpeuk
Citizen
Posts: 91
Joined: Wed Dec 14, 2011 6:16 pm

Re: Hardoncollider with tilemap

Post by Refpeuk »

Thanks so much, it works!
It was the best of times, it was the worst of times . . .
Post Reply

Who is online

Users browsing this forum: No registered users and 206 guests