## Hardon Collider not detecting collision

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
munchor
Prole
Posts: 39
Joined: Sun Jun 10, 2012 4:28 pm

### Hardon Collider not detecting collision

Hey there guys,

I don't know what I'm doing wrong, but Hardon Collider is not detecting collision. Well, in other words, I'm not using it correctly, even though I just read this and this.

Code: Select all

lick = require "lick"
lick.reset = true

HC = require "HardonCollider"

TILE_SIZE = 25
MAP_WIDTH = 26
MAP_HEIGHT = 24
N_MAPS = 1

maps = {}
for i = 1, N_MAPS do
end

-- Set up the collider
collider = HC(100, on_collide)
player_rect = collider:addRectangle(400, 400, TILE_SIZE, TILE_SIZE)
solid_tiles = get_solid_tiles(maps[1])
end

function love.update(dt)
player_rect:moveTo(love.mouse.getPosition())
collider:update(dt)
end

function love.draw()
love.graphics.push()
love.graphics.translate(250, 0)
maps[1]:draw()
love.graphics.pop()

--love.graphics.rectangle("fill", player.x, player.y, TILE_SIZE, TILE_SIZE)
player_rect:draw("fill")
end

function on_collide(dt, shape_a, shape_b, mtv_x, mtv_y)
print("Hey")
end

function get_solid_tiles(map)
local collidable_tiles = {}

local layer = map.tl["ground"]

for tileX = 1, map.width do
for tileY = 1, map.height do
local tile

if layer.tileData[tileY] then
tile = map.tiles[layer.tileData[tileY][tileX]]
end

if tile and tile.properties.solid then
local ctile = collider:addRectangle((tileX - 1) * 16, (tileY - 1) * 16, 16, 16)
ctile.type = "tile"
collider:setPassive(ctile)
table.insert(collidable_tiles, ctile)
end

end
end

return collidable_tiles
end

So, supposedly, whenever the player hits a block, it should print out "Hey".

Immediately, I figured out the issue had to be the solid tiles. So, I printed out #solid_tiles, and got 0.

My tileset has the property "solid" and the layer is called "ground", so everything should be working fine.

Any idea of what I missed? Thank you. (Also, the map is being drawn correctly, and I am making it so that the player hits the gray tiles).

coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

### Re: Hardon Collider not detecting collision

Looking to that map I don't have sure what kind of game you have and how you move your character (because I don't know what happens inside player_rect:moveTo) but If the movement it's grid-based or aligned (tile/cell to tile/cell) you probably won't need even use a library for detect collision.

munchor
Prole
Posts: 39
Joined: Sun Jun 10, 2012 4:28 pm

### Re: Hardon Collider not detecting collision

Well, there is, indeed a grid, but the player can move freely and diagonally, instead of tile per tile.

Additionally, I don't know how I'd go about not using a library for collision. I've done it many times, but usually I have a bidimensional array of the map to check if a certain pixel is a block or not.

Actually, come to think about it, I do! It's map.tiles[]. How can I use this by the way? Thank you.

coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

### Re: Hardon Collider not detecting collision

munchor wrote:Well, there is, indeed a grid, but the player can move freely and diagonally, instead of tile per tile.

Additionally, I don't know how I'd go about not using a library for collision. I've done it many times, but usually I have a bidimensional array of the map to check if a certain pixel is a block or not.

Actually, come to think about it, I do! It's map.tiles[]. How can I use this by the way? Thank you.
Ah ok, don't know the shape necessities of your game but it's better then use a proper lib detection. For very simple "free" movement bounding box (https://love2d.org/wiki/BoundingBox.lua) would be a alternative. And if was grid aligned would be very simple detection. I will leave this for the expertise of kadoba and/or vrld then.

munchor
Prole
Posts: 39
Joined: Sun Jun 10, 2012 4:28 pm

### Re: Hardon Collider not detecting collision

Well, how can I access a certain tile? Like, tile X = 20, Y = 3. How can I use map to access it, and know it if that tile has property solid or not?

In other words, is there a function like "map.get_tile(20, 3)"?

Thank you.

coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

### Re: Hardon Collider not detecting collision

munchor wrote:Well, how can I access a certain tile? Like, tile X = 20, Y = 3. How can I use map to access it, and know it if that tile has property solid or not?

In other words, is there a function like "map.get_tile(20, 3)"?

Thank you.
You should read the instructions in Advanced Tile Loader files and in the thread (viewtopic.php?f=5&t=2567&start=110) or even check the examples in the lib to check how to access to tiled properties. I never used that lib but know that Kadoba did a good complete job on it. Check for example "desert" and check how is get solid info. I think what you want is something similar to this

Code: Select all

tile = map.tiles[layer.tileData[3][20] -- a bit like you already do
if tile.properties.obstacle then ... end (or the inverse depends what it suits you more.)

Code: Select all

tile and tile.properties.solid 
so probably what you want is not tile.properties.obstacle but solid. Remember that first you must call tile = map.tiles[layer.tileData[3][20] or the tile Y/X you want. Test both. Not sure but seems ways of get what you want. If you still have problems post something in ADV.Tile Loader that kadoba will gladly help for sure.

munchor
Prole
Posts: 39
Joined: Sun Jun 10, 2012 4:28 pm

### Re: Hardon Collider not detecting collision

Thank you! However, my problem is here:

Code: Select all

local layer = map.tl["ground"]
I have a layer called "ground" on tiled, yes, but when I print out #layer, it gives 0.

Does that mean it is not detecting a layer called "ground"?

Code: Select all

  local layer = map.tl["ground"]
local tile = map.tiles[layer.tileData[1][1]]

And LÖVE said: "ERROR: main.lua:69: attempt to index a nil value"

Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

### Re: Hardon Collider not detecting collision

The way to access tiles changed in AdvTiledLoader 0.10.0. The article looks like it's using an earlier version.

Here's the old way to access tiles:

Code: Select all

  local layer = map.tl["ground"]
local tile = map.tiles[layer.tileData[1][1]]

And the new way:

Code: Select all

local tile = map.tl["ground"].tileData(1,1)
You can also iterate over all tiles of a certain layer using iterate()

Code: Select all

for x, y, tile in map.tl["ground"].tileData:iterate() do
-- do something with the tiles
end
-Edit-

This is what your get_solid_tiles function should look like:

Code: Select all

function get_solid_tiles(map)
local collidable_tiles = {}

local layer = map.tl["ground"]

for tileX, tileY, tile in layer:iterate() do

if tile and tile.properties.solid then
local ctile = collider:addRectangle((tileX - 1) * 16, (tileY - 1) * 16, 16, 16)
ctile.type = "tile"
collider:setPassive(ctile)
table.insert(collidable_tiles, ctile)
end

end

return collidable_tiles
end


munchor
Prole
Posts: 39
Joined: Sun Jun 10, 2012 4:28 pm

### Re: Hardon Collider not detecting collision

That's really cool, very easy to use!

However, it seems like AdvTiledLoader is not detecting my layer "ground". I have a layer called "ground" with tiles on it on Tiled. I save the file, everything looks fine.

However, map.tl["ground"] is nil for some reason I can't understand.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="26" height="24" tilewidth="25" tileheight="25">
<tileset firstgid="1" name="Tiles" tilewidth="25" tileheight="25">
<properties>
<property name="solid" value="1"/>
</properties>
<image source="tileset.png" width="50" height="50"/>
</tileset>
<layer name="ground" width="26" height="24">
<properties>
<property name="ground" value="1"/>
</properties>
<data encoding="base64" compression="zlib">
</data>
</layer>
</map>

That's my .tmx file :S

Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

### Re: Hardon Collider not detecting collision

Hmm, it worked for me. Can you upload a .love?
maptest.love