Comparing colorSample output to other variables doesn't work?

Hi guys,

I’m trying to make a physics object detect the color of the dynamic bar that it is bouncing on. I’m using the colorSampler and I’m rounding those numbers to numbers with 1 decimal number. After that I try comparing them to other numbers (hard coded). Now, for some reason this doesn’t work. And the weirdest part is that if they are not the same I do something like print(collisionR … “not equal to” … hardCodedR). This print statement is executed, but the output of collisionR and hardCodedR are the exact same! This is my code:

 function functions.roundToSecondDecimal(t) return mRound(t\*10)\*0.10 end function functions.getColor(event) print( "Sampling pixel at position (" .. event.x .. "," .. event.y .. ")" ) print( "R = " .. event.r ) print( "G = " .. event.g ) print( "B = " .. event.b ) circleColor = {event.r, event.g, event.b} end function functions.onCollision(event) display.colorSample(playerBall.x, playerBall.y + 20, functions.getColor) local r = functions.roundToSecondDecimal(circleColor[1]) local g = functions.roundToSecondDecimal(circleColor[2]) local b = functions.roundToSecondDecimal(circleColor[3]) if playerBallColor[1] == r and playerBallColor[2] == g and playerBallColor[3] == b then composer.gotoScene("menu") else print(playerBallColor[1] .. "is not equal to" .. r) print(playerBallColor[2] .. "is not equal to" .. g) print(playerBallColor[3] .. "is not equal to" .. b) end end playerBall:addEventListener("collision", functions.onCollision) 

So… The table playerBallColor includes three children with 1 being R, 2 being G and 3 being B.

This piece of code prints:

May 23 07:46:19.774 0.6is not equal to0.6

May 23 07:46:19.775 0.3is not equal to0.3

May 23 07:46:19.775 0.7is not equal to0.7

which is very strange in my eyes. Is there a way to fix this?

You probably should be rounding both sides of the equation using the same formula. Comparing floating point numbers for equality is tricky.

Rob

You probably should be rounding both sides of the equation using the same formula. Comparing floating point numbers for equality is tricky.

Rob