Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / Image points detection
- - By asmodean Date 2013-03-09 19:23
Hi, guys. I am newbie with gosu  and game development. I am making game with ruby and I want to make some collision detection. So I thought i can build optimal circle around an object into image. The image has black or transparent background and i have to find the distance between the farthest points of the object. Any suggestions? I thought i can save the image into bitmap and iterate trough the array and find these points but i can't do this with gosu.
Parent - - By lol_o2 Date 2013-03-09 20:04
For circular collisions you don't need any image, Gosu.distance method should be enough.
Parent - - By asmodean Date 2013-03-09 20:16
But I don't have the positions of the farthest points. How to find them?
Parent - - By lol_o2 Date 2013-03-09 20:25
So you want to render this circle base on image's size? There's no optimal method for this.
But to calculate diameter of the circle, you can use diameter = [image.width, image.height].max and then you need only a center point of the image, which should be position of your object.
Parent - - By asmodean Date 2013-03-09 21:24
Actually i want to find subimage's farthest points and use their distance as diameter. The image has some background and unit drawn over the background. I want to find the radius of the circle around the unit, not around the image.Is there any way to do this with some gem or i have to use some other tool to find these points?
Parent - By Spooner Date 2013-03-09 23:51
You can read individual pixel colour values in Gosu with the texplay (or ashton) gem.
Parent - By jlnr (dev) Date 2013-03-10 01:54
You can also use Gosu::Image#to_blob which will give you a binary string of width * height RGBA quads. You can ignore the RGB bytes and simply look at the alpha channel of each pixel to perform your calculation.

In any case, looping over individual pixels will be very, very slow in Ruby, so you should probably make this part of your build process and ship the game with precompiled values (if that is possible for what you want to achieve).
Parent - By asmodean Date 2013-03-10 11:33
Thank you guys, this is what I am looking for. Actually I knew about these decisions but I wasn't sure if i can use them. I was hoping to find ready function about this what I want, but now when I know that it can be done my problem is solved. :)
Parent - - By BlueScope Date 2013-03-10 12:32
A much easier way seems to be to just cut your images down to the size they are, as in dn't leave transparent pixels on sides where they don't need to be. Then, drw them from center to always get expected results, and do your distance calculations based on something like this:

# for square sprites only:
r = width / 2 * 1.4

# for sprites with different width and height values:
r = width > height ? width / 2 * 1.4 : height / 2 * 1.4

For efficiency, you should replace the '/ 2 * 1.4' with '0.7'. What this whole thing does is getting the length of the longest diagonal to be used as a radius for circular calculations.
Parent - By asmodean Date 2013-03-10 14:28
At the beginning I will do what you said, but lately I will have to change this, because the circle built in your way won't be the optimal circle... if I cut the images in that way, it will be better just to use rectangle for the collision detection. Let't say we have 5 width and 5 height, then my circle will have about 7 diameter. But if the object is sphere then the optimal diameter will be 5, no 7.
Parent - - By asmodean Date 2013-03-10 15:00
Wait a moment, your second formula is wrong. Let's say we have height 30 and width 10 so the diagonal is about 32, then the radius about 16.. But with your formula the radius is 21. This *1.4 works only for square, not for rectangle.
Parent - By BlueScope Date 2013-03-11 07:51
You're right, it would require a compensation for the shorter side for such extreme values. However, you would need almost-square images anyhow for this method, as you're going to use a radius formula afterwards.

It's of course no perfect way, or one that handles all exceptions, just 'the next best thing' before pixel-wise checking - nevermind for collisions, which have to be real time.
Up Topic Gosu / Gosu Exchange / Image points detection

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill