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?
You can read individual pixel colour values in Gosu with the texplay (or ashton) gem.
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).
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. :)
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.
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.
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.
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.