Hi! I'm Solumin, a general programmer who's just started with game development, Lua, and Love. I've been lurking on the forums for a while, but my issues have become a bit too...involved, shall we say...to be defeated by mere voyeurism. Currently I'm working on a project with my college's game development club. The game in question is a fairly standard platformer with some interesting mechanics based on art and color. I discovered Love while searching for game development information in relation to Lua, which I had heard was quite popular in the games industry. After playing around with it I fell in, heh, love, and have been using it for a couple months now.
However, as the subject of this post suggests, I've been having trouble with collision detection. I'm using HardonCollider for my collision detection needs but I don't really understand what I'm doing. The first tutorial on the HC site covers the basics really well, but I need help with the advanced topics, such as the uses for the different callbacks, the exact nature of the minimum translation vector (it isn't working intuitively for me), groups, spatial hash -- basically, all the stuff that will be covered in the later tutorials. If I understood these topics I would offer to write the tutorials, but I'm having a lot of trouble.
I managed to kludge and futz my way through it, using, for example, shape:collidesWith(...) to check for collision detection and then dealing with it roughly. However, this restricted the system to rectangles only -- no angles, triangles, circles, etc. That leads to really boring levels and unnecessary limitations on other facets of development. I would love to get HC working as it's supposed to.
Anyway, I can post code if you lovely fellows would care to take a gander. Any tips, tricks, and advice in regards to HardonCollider, Love, platformers, and game development in general would be much appreciated.
Thanks!
A little help with collision detection
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: A little help with collision detection
[[Double post -- yet another issue...hopefully someone can help]]
Here's a more specific problem, which I don't think needs a new thread since it's related to what I originally posted.
In my game, I create a world out of a series of platforms. The coordinates for each platform are passed to a function that creates a rectangle or polygon use hardoncollider.addPolygon(...) then pushes the new polygon into a table called 'ground' that keeps track of the platforms. Later in the program there is a function that checks if a given point is in any of the world's platforms:
However, i always get an error:
None of the platforms have any of the functions that they should, such as contains, getBBox, or unpack. They are apparently blank tables. However, I have no problem with plat:draw(), which is used in another function (I tested it in checkPoint just in case.) As far as I know, only draw works. Does anyone know what is happening? Or why the platforms don't have any of their functions?
Here's a more specific problem, which I don't think needs a new thread since it's related to what I originally posted.
In my game, I create a world out of a series of platforms. The coordinates for each platform are passed to a function that creates a rectangle or polygon use hardoncollider.addPolygon(...) then pushes the new polygon into a table called 'ground' that keeps track of the platforms. Later in the program there is a function that checks if a given point is in any of the world's platforms:
Code: Select all
function World.checkPoint(x,y)
for i, plat in ipairs(ground) do
--if the platform contains the point, then return the platforms and exit.
if plat:contains(x,y) then return plat end
end
--if the spot fits in none of the platforms, then the point (x,y) is bad - return nil
return nil
end
'bridge' looks for a nearby platform so it can create a bridge between that platform and the one the player is standing on. It is triggered by a keypress.Error: [string "world.lua"]:45: attempt to call method 'contains' (a nil value)
stack traceback:
[string "world.lua"]:45: in function 'checkPoint'
[string "main.lua"]:90: in function 'bridge'
[string "main.lua"]:79: in function 'keypressed'
[string "boot.lua"]:158: in function '?'
[string "boot.lua"]35: in function <[string "boot.lua"]06>
[C]: in function 'xpcall'
None of the platforms have any of the functions that they should, such as contains, getBBox, or unpack. They are apparently blank tables. However, I have no problem with plat:draw(), which is used in another function (I tested it in checkPoint just in case.) As far as I know, only draw works. Does anyone know what is happening? Or why the platforms don't have any of their functions?
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: A little help with collision detection
Hm. What is the code that pushes those polygons to the ground table?
Help us help you: attach a .love.
Re: A little help with collision detection
Hi Solumin!
Different callbacks
The main HC module manages the collision detection for you. Whenever it detects a collision between two shapes, a callback is invoked, according to the following:
The start callback is useful for detecting collisions with bullets, traps, or anything else, that triggers an immediate response. The persist callback is useful when you need to react continuusly, such as push two shapes out of each other if they intersect. The stop callback will be used whenever you need to know when two shapes stopped touching.
The example code on the HardonCollider main page will help to see what callback is called when.
The minimum translation vector
is the direction that the first shape in the collision callback has to be moved in order to not collide with the second anymore. In the left part of the image, the mtv is the blue arrow. If you move shape A in the direction of the mtv, the shapes stop colliding. In the right part, shape A intersects with shape B in a way that leaves many possibilities to resolve the collision (one of them is the red arrow), but there is only one shortest direction (the blue arrow) - the mtv.
Groups
Shapes that share a group won't emit callbacks on collisions. For example, this can be used so that enemys wont collide with each other, stop the player's bullets to collide with himself or to mark level geometry.
Spatial Hash
Don't worry about that. It's used in the main module to detect collisions efficiently.
Solumin wrote:The first tutorial on the HC site covers the basics really well
In order:Solumin wrote:but I need help with the advanced topics, such as the uses for the different callbacks, the exact nature of the minimum translation vector (it isn't working intuitively for me), groups, spatial hash
Different callbacks
The main HC module manages the collision detection for you. Whenever it detects a collision between two shapes, a callback is invoked, according to the following:
- If the two shapes did not collide in the last frame, run the start callback.
- If the two shapes did collide in the last frame, run the persist callback.
The start callback is useful for detecting collisions with bullets, traps, or anything else, that triggers an immediate response. The persist callback is useful when you need to react continuusly, such as push two shapes out of each other if they intersect. The stop callback will be used whenever you need to know when two shapes stopped touching.
The example code on the HardonCollider main page will help to see what callback is called when.
The minimum translation vector
is the direction that the first shape in the collision callback has to be moved in order to not collide with the second anymore. In the left part of the image, the mtv is the blue arrow. If you move shape A in the direction of the mtv, the shapes stop colliding. In the right part, shape A intersects with shape B in a way that leaves many possibilities to resolve the collision (one of them is the red arrow), but there is only one shortest direction (the blue arrow) - the mtv.
Groups
Shapes that share a group won't emit callbacks on collisions. For example, this can be used so that enemys wont collide with each other, stop the player's bullets to collide with himself or to mark level geometry.
Spatial Hash
Don't worry about that. It's used in the main module to detect collisions efficiently.
Don't! The main module will do that for you.Solumin wrote:I managed to kludge and futz my way through it, using, for example, shape:collidesWith(...) to check for collision detection and then dealing with it roughly.
Re: A little help with collision detection
Robin wrote:Hm. What is the code that pushes those polygons to the ground table?
Code: Select all
hc = require 'hardoncollider' --loaded at the top of the file
function World.addPlatform(coords)
local temp
if #coords == 4 then
temp = hc.addRectangle(unpack(coords))
else
temp = hc.addPolygon(unpack(coords))
end
table.insert(ground, temp)
hc.addToGroup('ground', temp)
end
I thought the mtv worked something like that. My problem was that when I used it, shape_one would get sucked into shape_two if shape_one was colliding with the left side or bottom of shape_two. The top and right sides were fine. In the callbacks, x and y were always (as far as I know) positive and negative respectively. (I've seen mtv's like (2, -0) before) Every collision would cause shape_one to move right and up. I just solved that by checking the position of shape_one and flipping the sign of mtv_x and mtv_y accordingly.vrld wrote:In order:
I think that working with Pygame was messing up my groups. I thought that groups were some special construct or table that you added the platforms to. Strings are a lot easier Thank you so much!
Re: A little help with collision detection
That was a bug in a previous version of HardonCollider. Do you use the newest version? If not, that would also explain why contains(x,y) fails - it's not included in your version.Solumin wrote:My problem was that when I used it, shape_one would get sucked into shape_two if shape_one was colliding with the left side or bottom of shape_two.
Re: A little help with collision detection
I forgot to actually put the new hardoncollider code where Love could read it, so Love was looking at the old code. Well. Glad that's resolved.
Ok, things are mostly working now. Is there a way to create a polygon from the shape returned by hc.addPolygon(...)? Or a way to give the shape the functions that polygons have?
EDIT: I've worked around the issues related to the above 2 questions, so please ignore them. Sorry for asking so many questions!
Ok, things are mostly working now. Is there a way to create a polygon from the shape returned by hc.addPolygon(...)? Or a way to give the shape the functions that polygons have?
EDIT: I've worked around the issues related to the above 2 questions, so please ignore them. Sorry for asking so many questions!
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 5 guests