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)
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
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.
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 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.
There is some sort of discrepancy when rotating a collision shape, in the below the shape hasn't show a collision;
collision.jpg (6.89 KiB) Viewed 6919 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.
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;