Reputation: 1870
I used the codes from the following the website.
But the problem I am facing is getting the coordinates of my object.
Let me explain. According to the code. You just need to add your graphic images in PNG format and refer it to the class here. What I am trying to achieve is a simple collision detection mechanism. I have added a maze (as PNG). And have a object (as PNG) to go through the blank path within the maze. In order to do this I need to know the blank spaces within the coordinates through which my object will move.
Can any one tell me how to get the blank spaces as (x,y) coordinates through which I can take my object?
Upvotes: 3
Views: 378
Reputation: 7868
If there's one entry and one exit to the maze, or an open area around the maze which contains the destination point for multiple entries or exits, the A search algorithm* will do the job:
Note, that this might be best done on a CPU. You need to modify the condition for a path segment to handle the object size, a simple approach just assumes a path width of 1 image pixel.
If you modify the algo in such a way, that it matches multiple destination points, it might work even smoother, without the need for an open area for exits (one entry to many exits). For multiple starting points or entries respectively, a modification may result in N runs and path comparison to find the shortest one (many entries to many exits), or a reverse search if just one exit exists (many entries to one exit), like for one entry to many exits. If paths of multiple destination points are required, just continue the search until all of them are found or the algorithm is 'stalled' and stops.
UPDATE:
Best and most flexible way might be using a physics library supporting various shape types, like spheres, arbitrary convex hulls, etc. For example, you could use the 'bullet physics' library to do collision detection only, without physics simulation (but why not?). Fragment based collision detection in 2D could be achieved using OpenGL occlusion queries, which tend to be faster for complex shapes.
To integrate an image, you'd need to transform/scale it to sort of multiple wall objects in the coordinate space where the other shape is defined in.
In short: Collision detection using vertex data of arbitrary shapes is pretty expensive, to develop and at runtime. This is where the collision shapes of the bullet library would help, as they are used to represent a simplified shape of the objects to be drawn.
Upvotes: 2