Logic: Image 'Within' User Specified Area

BlitzMax Forums/BlitzMax Beginners Area/Logic: Image 'Within' User Specified Area

Matt Vinyl(Posted 2011) [#1]
Hmm, not sure if that title is quite right, but it's the best way I could think to word it in the limited space. :0) I will try and put it across as simply as I can, however, perhaps bullet points would be best:

> There's a random image moving around the screen (imagine the ball in an Breakout game, but the arena is fully enclused).

> The player can click with the mouse to place 'nodes' anywhere in the playfield.

> After placing a node, clicking in another position will create a line between the two nodes.

> The player can continue this until they have made a shape which has no gaps in the edge (finished back at the node they started).

> Shape could be made from any number of nodes.

> The crux of the matter: I want to check if the randomly moving image is inside the created shape or not.

I'll admit to only playing around with psuedo-code during a few lunch-breaks at work at the moment, so have no actual code.

I'm guessing I would need to hold all the nodes in some sort of type, with X, Y, N (Node numbder) and maybe some other things.

I'm also thinking that there might be parts of code in 'floodfill' routines that might be of use here?

As always, any help appreciated such as pointers on how to proceed.


ImaginaryHuman(Posted 2011) [#2]
You wouldn't need floodfill but one way would be to render the polygon in a pixmap using the cpu with a raster scan polygon fill algorithm and then check if the pixels in the ball are overlapping an area of the polygon.


Jesse(Posted 2011) [#3]
for an irregular object it might be best to do what ImaginaryHuman suggested but if it's for a ball inside an area(for example a polygon) it's not too hard. there are, I believe, a couple of libraries in the code archives that can help determine that. they are still missing some functionality such as how far the ball is from the line though.
At the moment I am working on creating a vector library that will use lines circles and arcs but is far from complete so I won't even post some code.


Kryzon(Posted 2011) [#4]
You could do this in a mathematical way (calculating the distance from each segment towards the image, see if the image's radius is smaller than that etc.), but that might lead to some worst-case scenarios since the image might have to be represented by a hull such as a rectangle or circle.

I agree with ImaginaryHuman, but instead of using the CPU to raster, use the GPU and then you are able to use hardware Occlusion Queries, which does exactly what you want. Since it's from the GPU it'd be lightning fast.
DirectX9 also has this mechanism.


AdamRedwoods(Posted 2011) [#5]
Interesting...

http://gamedev.stackexchange.com/questions/244/cocos2d-collision-detection-against-a-random-shape/260#260


There's a very easy solution for that particular example.

I'm assuming your arbitrary shape is just a series of points.

Draw a ray from each of your objects in any direction. If the number of times it intersects a line segment on your shape is even (including zero), you are outside of the shape.

If the number of times you intersect is odd, you are inside the object.

Ray/line segment collision is a pretty easy algorithm to find/implement.




Matt Vinyl(Posted 2011) [#6]
Thanks gentleman (I assume!) some very useful advice there. Will give some ideas a trial.


Matt Vinyl(Posted 2011) [#7]
AdamRedwoods: Having looked at your link, I think that's a brilliant and simple idea that actually works! I love little solutions like this that sound almost too simple to be true! Just need to work out how to do a 'ray' now. :) I guess it's just some sort of line-dissecting routine?


Matt Vinyl(Posted 2011) [#8]
Hi all, just revisiting this issue now and I am trying to get it up and running now. I'm going to use 0,0 as the reference point.

So, say the user clicks the mouse at 100,100 and it is within a closed shape, I need to see how many times a 'line' (or ray) would cross the shape's border until it meets 0,0. And as in AdamRedwood's post above - if that number is odd, I can set an 'inside the object' flag to true.

It's the checking and counting I'm not sure how to implement. Any advice would be most welcome.