HardonCollider usage

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

HardonCollider usage

Post by Kazagha »

I have been struggling to get my head around the HardonCollider over the past few days;

I have been using the following bit of code to pull the coordinates, width and height from the 'enemies' table and create the collision objects

Code: Select all

function collisionENEMY()
  for i,v in ipairs (enemies) do
	enemy1 = HC.addRectangle(v.x, v.y, v.width, v.height)
	end
end
Then using the following to update the 'text' table with collisions, and specifically if the collision is with 'enemy1' from the above.

Code: Select all

function on_collision(dt, shape_a, shape_b)
            --this will show with any collision  
            text[#text+1] = string.format("Colliding")
    if shape_b == enemy1 then
	    text[#text+1] = string.format("Enemy Detected!")
    else
	    text[#text+1] = string.format("Nothing")
    end
	if shape_a == enemy1 then
	    text[#text+1] = string.format("shape_a")
	end
end
The table is written to the screen so I can see what is happening (something I picked up when doing a tutorial). How do I modify the code so I can identify which enemy from the 'enemies' table has triggered the collision? I have attached the .love file for my game thus far, if that is of use to anyone. (main.lua and game.lua are two that have all the functions in question)

-Kazagha
Attachments
test.love
(151.69 KiB) Downloaded 225 times
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: HardonCollider usage

Post by Kazagha »

*cough* I think that it's under wraps now.

I added the collision objects to a table like this;

Code: Select all

function collisionENEMY()
	coll = {}
  for i,v in ipairs (enemies) do
	coll[i] = HC.addRectangle(v.x, v.y, v.width, v.height)
	
  end
end
Then I can reference them like this;

Code: Select all

function on_collision(dt, shape_a, shape_b)
    text[#text+1] = string.format("Colliding")
    if shape_b == coll[1] then
	    text[#text+1] = string.format("Enemy One Detected!")
    end
    if shape_b == coll[2] then
	    text[#text+1] = string.format("Enemy Two Detected!")
    end
end
It's not perfect but it's a mighty good start.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: HardonCollider usage

Post by TechnoCat »

Kazagha wrote:*cough* I think that it's under wraps now.

I added the collision objects to a table like this;

Code: Select all

function collisionENEMY()
	coll = {}
  for i,v in ipairs (enemies) do
	coll[i] = HC.addRectangle(v.x, v.y, v.width, v.height)
	
  end
end
Then I can reference them like this;

Code: Select all

function on_collision(dt, shape_a, shape_b)
    text[#text+1] = string.format("Colliding")
    if shape_b == coll[1] then
	    text[#text+1] = string.format("Enemy One Detected!")
    end
    if shape_b == coll[2] then
	    text[#text+1] = string.format("Enemy Two Detected!")
    end
end
It's not perfect but it's a mighty good start.
You're on the right track, but it still isn't good. Right now you are completely disregarding shape_a's ID. I guess that could be intentional and I am missing a ghost call, but you don't know the order the shapes will be passed into the function. So the enemy could be shape_a or shape_b.

Look at this: http://vrld.github.com/HardonCollider/t ... hello-pong
And maybe this can help: http://love2d.org/forums/viewtopic.php?f=5&t=3682
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: HardonCollider usage

Post by Kazagha »

Thanks for the reply TechnoCat. For the most part I am just happy that it's detecting collisions in a way that I can figure out that collision object 'enemy1' is the same 'enemy1' from the 'enemies' table. Still needs a lot of modification to do that, but it's working thus far.

The thing that is confusing me at the moment is an 'test' object at 516,420 detects collisions with a 'scanning line' that I created with a rectangle object and the rotate function, but if I move it further away from the scanner to 516,410 there are no collisions detected.

I have tried increasing the size of the scanner line, changed to "HC.init(5, on_collision)" down from 100, as I was of the understanding that it's the size of the scanning grid, I don't know why it won't detect the collision.

I will be sure to check out your links going forward, I was using the pong tutorial and reference section to make what I have created thus far. It will be awesome when they have more tutorials up.

-Kazagha
Attachments
test.love
(44.25 KiB) Downloaded 186 times
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: HardonCollider usage

Post by TechnoCat »

Kazagha wrote:It will be awesome when they have more tutorials up.
I wouldn't count on that.

I edited your code a bit, maybe this will make more sense? (and also make things easier?)
Attachments
test.love
(44.49 KiB) Downloaded 180 times
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: HardonCollider usage

Post by Kazagha »

TechnoCat wrote:
Kazagha wrote:It will be awesome when they have more tutorials up.
I wouldn't count on that.
You wouldn't count on there being more tutorials or the awesomeness thereof? There is a part two and three after the pong tutorial that are currently blank. It's easier to understand how something works when you can see it in practice.
TechnoCat wrote:I edited your code a bit, maybe this will make more sense? (and also make things easier?)
Yes you did; it's amazing what can be done when it's in the right hands. I would never have figure out how to create the enemies.id, or integrating the collision object into the main table, that has made things so much easier.

-Kazagha
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: HardonCollider usage

Post by Kazagha »

*scratches head*

There is some sort of discrepancy when rotating a collision shape, in the below the shape hasn't show a collision;
collision.jpg
collision.jpg (6.89 KiB) Viewed 6684 times
I hacked in the ability to manually move outline rectangle, left and right arrow keys and enter to stop moving. It detects all collisions when it's flat at the beginning of the game, but misses most collisions when at a angle.

I might have to try a different tact if that is the case.

-Kazagha
Attachments
test.love
(44.36 KiB) Downloaded 185 times
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: HardonCollider usage

Post by Kazagha »

I modified the code again changing the 'rotate' function; shape:setRotation(angle, cx,cy). Originally it rotated on it's end around the origin of the rectangle. Setting cx/cy the same as the origin.

I removed the cx/cy, which by default causes the shape to rotate around it's centre point. This now picks up all the collisions no worries.

The below shows a collision and it's only just touching;
collision.jpg
collision.jpg (10.4 KiB) Viewed 6681 times
-Kazagha
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: HardonCollider usage

Post by TechnoCat »

User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: HardonCollider usage

Post by Kazagha »

Oh man. Thanks creating the issue ticket for my problem.

-Kazagha
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests