Intersecting with color
BlitzPlus Forums/BlitzPlus Beginners Area/Intersecting with color
| ||
I want to know if there is an easy way to test if an object is touching a color (black) for colision detecting. Thanks |
| ||
You could read the pixel at the given (x,y) position using the GetColor(x,y) command. Its pretty easy to use... all you would do would be to check to see if ColorRed(), ColorGreen(), and ColorBlue() are all 0 (for black). |
| ||
Hmm... I would need to do 100s, if not 1000s of color readings per frame, wouldn't I? Is this a fast working command? |
| ||
A slightly faster approach, although I'm still not sure it would be fast enough for a game like that, is to use the "readpixelfast(x,y(,buffer))" command, as follows:function checkforblack(x1,y1,x2,y2) ;checks the perimeter for any non-black pixels local tempa local tempb local rgb lockbuffer(imagebuffer(backgroundimage)) setbuffer(imagebuffer(backgroundimage)) for tempa=x1 to x2 for tempb=0 to 1 rgb=readpixelfast(tempa,tempb*y2+y1) if GetRed(rgb)<>0 or GetGreen(rgb)<>0 or GetBlue(rgb)<>0 unlockbuffer(imagebuffer(backgroundimage)):return 0 ;the function returns a zero and exits else if tempa=x2 and tempb=1 then unlockbuffer(imagebuffer(backgroundimage)):return 1 ;the function returns a one and exits endif next next end function Function GetRed(rgb) Return rgb Shr 16 And %11111111 End Function Function GetGreen(rgb) Return rgb Shr 8 And %11111111 End Function Function GetBlue(rgb) Return rgb And %11111111 End Function |
| ||
I made a color multiplier with 2 readfasts and a writefast. It does about 320000 pixels/second (that's about a 565 * 566 pixel image). If you figure that each command takes the same time, then you can read 3 times as many pixels as I can multiply: 960000! Gotta make sure you lock your buffer before you use the ReadPixelFast command though. And also, that function doesn't stop you at the end of you image, so you eventually will start reading random memory into your program if you aren't careful about the dimensions. But IMO, ReadPixelFast is probably a decent solution for you if you have a decently small player graphic (under 10000 pixels). Depends on what kind of running time you can deal with. here's the manual page for ReadPixelFast edit: The only other thing I can think of might be cumbersome: ImageRectCollide. That depends on how your color is spaced through the map. |
| ||
@Gladclef Who wrote that code? If you did, is it public domain? I am writing for a tech fair and need copyright permissions. |
| ||
@acolite246 Yeah, go ahead and use it. |
| ||
Thank you |
| ||
Wait! Now my program crashes saying blitzcc.exe has encountered an error... send error report? |
| ||
Hello? Oh and what does the : mean? And? |
| ||
Wait! Now my program crashes saying blitzcc.exe has encountered an error. Turn the debugger on, if isn't already. At a complete guess, you've possibly provided values for X2 and/or Y2 which are one too big. Remember that a 32 pixel wide image goes from 0 to 31, not from 1 to 32. Oh and what does the : mean? And? It's just a convenient way of putting more than one command on the same line. |
| ||
Wait! Now my program crashes saying blitzcc.exe has encountered an error. Fixed it... turns out that the width or the height cannot be zero and must be at least one. But that is great code! @Gabriel - thanks for the tip about the : |