# RMagick and Collision Mask

**URL:** <https://www.ruby-forum.com/t/rmagick-and-collision-mask/134323>\
**Category:** Ruby\
**Created:** [April 9, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/rmagick-and-collision-mask/134323 "2008-04-09T20:41:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![di3go](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@di3go](https://www.ruby-forum.com/u/di3go)\
**Post date:** [April 9, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/rmagick-and-collision-mask/134323/1 "2008-04-09T20:41:41Z")

</div>

Im trying to make a perfect pixel collision in a game and im gonna use a  
collision mask to do this, the physics its with chipmunk and i need a  
polygon to do it, my idea is, read the image pixel per pixel and store  
in a matrix the x and y of the black pixels, later with this matrix i  
can create a poligon just geting the first and last element from the  
column in each line of the matrix.

The problem is the RMagick, i know how to read the image pixel per pixel  
but dont know how to compare the color to get only the black pixels

Someone can help me?

thx 🙂

---

<div class="post-metadata">

**Author:** ![di3go](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9a28c/32.png) [@di3go](https://www.ruby-forum.com/u/di3go)\
**Post date:** [April 9, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/rmagick-and-collision-mask/134323/2 "2008-04-09T20:50:33Z")

</div>

Diego B. wrote:

> Im trying to make a perfect pixel collision in a game and im gonna use a  
> collision mask to do this, the physics its with chipmunk and i need a  
> polygon to do it, my idea is, read the image pixel per pixel and store  
> in a matrix the x and y of the black pixels, later with this matrix i  
> can create a poligon just geting the first and last element from the  
> column in each line of the matrix.
> 
> The problem is the RMagick, i know how to read the image pixel per pixel  
> but dont know how to compare the color to get only the black pixels
> 
> Someone can help me?
> 
> thx 🙂

black = Magick::Pixel.from\_color(“black”)  
p == black # true if the pixel p is black.

If you want to include “almost black” pixels then use  
Magick::Pixel#fcmp:  
[http://www.imagemagick.org/RMagick/doc/struct.html#Pixel](http://www.imagemagick.org/RMagick/doc/struct.html#Pixel).

If you have any other questions about RMagick you can always contact me  
at removed\_email\_address@domain.invalid, or post your question on the Help Forum for  
the RMagick project on RubyForge:  
[http://rubyforge.org/forum/?group\_id=12](http://rubyforge.org/forum/?group_id=12).
