Objects collision with same color

Hi guys,

I really need help.

So I have a circle(on picture) with a 6 colors and I want to make that balls can have collision with that circle ( like red ball with red part on circle) and after collision it removes and increase score by 1 or 2.

Here is my code:

require "ssk.loadSSK" -- -- Set up physics local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) -- -- Forward Declarations -- SSK local angle2Vector = ssk.math2d.angle2Vector local vector2Angle = ssk.math2d.vector2Angle local scaleVec = ssk.math2d.scale local addVec = ssk.math2d.add local subVec = ssk.math2d.sub local normVec = ssk.math2d.normalize local getNormals = ssk.math2d.normals local vecLen = ssk.math2d.length local vecLen2 = ssk.math2d.length2 -- Lua and Corona local mAbs = math.abs local mRand = math.random local getInfo = system.getInfo local getTimer = system.getTimer local strMatch = string.match local strFormat = string.format -- -- A place to store our 'balls' local balls = {} local colors = { \_R\_, \_G\_, \_Y\_, \_C\_ } local colorNames = { "RED", "GREEN", "YELLOW", "CYAN"} local spawnTimer local onCollision local spawnArrow -- -- Target local spinner = display.newImage("circle.png") spinner.x = display.contentWidth/2 spinner.y = display.contentHeight/2 spinner:scale( 0.8, 0.8) local adjustment = 0 local PI = 3.14159265358 local function onTouch(e) if(e.phase == "began") then local dx = e.x - spinner.x local dy = e.y - spinner.y adjustment = math.atan2(dy,dx) \* 180 / PI - spinner.rotation end if(e.phase == "moved") then local dx = e.x - spinner.x local dy = e.y - spinner.y spinner.rotation = (math.atan2(dy,dx) \* 180 / PI) - adjustment end end Runtime:addEventListener('touch', onTouch) -- The balls -- -- Min distance to start away is worst case (i.e. Corner of screen) local vec = subVec( centerX, centerY, left, top, true) local minLen = vecLen( vec ) onCollision = function( self, event ) if( event.phase == "began" ) then balls[self] = nil print("My approach angle: ", self.approachAngle, " color: ", colorNames[self.myColor]) display.remove( self) end return true end spawnArrow = function() local size = 20 local speed = 200 local approachAngle = math.random(0,360) local startOffset = angle2Vector( approachAngle, true ) local velocityVec = table.shallowCopy( startOffset ) startOffset = scaleVec( startOffset, minLen ) velocityVec = scaleVec( velocityVec, -speed ) local tmp = display.newCircle( startOffset.x + centerX, startOffset.y + centerY, size/2 ) physics.addBody( tmp, "dynamic", { radius = size/2 } ) tmp.isSensor = true tmp.approachAngle = approachAngle tmp.myColor = math.random(1,#colors) tmp:setFillColor( unpack(colors[tmp.myColor]) ) tmp.collision = onCollision tmp:addEventListener( "collision" ) local function hitTestObjects(tmp, spinner) local left = tmp.contentBounds.xMin \<= spinner.contentBounds.xMin and tmp.contentBounds.xMax \>= spinner.contentBounds.xMin local right = tmp.contentBounds.xMin \>= spinner.contentBounds.xMin and tmp.contentBounds.xMin \<= spinner.contentBounds.xMax local up = tmp.contentBounds.yMin \<= spinner.contentBounds.yMin and tmp.contentBounds.yMax \>= spinner.contentBounds.yMin local down = tmp.contentBounds.yMin \>= spinner.contentBounds.yMin and tmp.contentBounds.yMin \<= spinner.contentBounds.yMax return (left or right) and (up or down) end local doTest = true local function test(event) --Check if the objects are colliding and are the same color if doTest == true then if hitTestObjects(tmp, spinner) == true then if tmp.color == spinner.color then print("collided with same color object") --Remove the images display.remove(tmp) doTest = false end else spinner:translate(0, 0) end end return true end Runtime:addEventListener("enterFrame", test) tmp:setLinearVelocity( velocityVec.x, velocityVec.y ) balls[tmp] = tmp timer.performWithDelay( 3000, spawnArrow ) end spawnArrow()

I have that collision ( balls with circle ) but in corona simulator output it just says collided with same color object and on Corona simulator balls are coming across the screen with random colors from code but I want to make that when e.g. yellow ball hits red part on circle it just opens restart screen.

Sorry if you don’t understand,I am from Croatia :stuck_out_tongue:

When your balls collide with the spinner, you can calculate the angle. If it is 0 to 60 degrees, you’ve hit the green, if it’s between 60 and 120, it’s hit the light blue, etc.

Thanks for that,but can you write that for code above? Please

You can use ball.x minus spinner.x and ball.y minus spinner.y to get the vector components, and use vector2Angle to get the angle.

You could pass this angle into a function to see what colour of the spinner it’s collided with. A function like this:

local function angleToColour(angle) &nbsp;&nbsp; &nbsp;-- function assumes degrees, not radians &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local rot = spinner.rotation &nbsp;&nbsp;&nbsp; angle = angle - rot &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;-- ensure a positive angle &nbsp;&nbsp; &nbsp;while angle \< 0 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;angle = angle + 360 &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp;&nbsp; if angle \>= 0 and angle \< 60 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return "green" &nbsp;&nbsp;&nbsp; elseif angle \>= 60 and angle \< 120 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return "cyan" &nbsp;&nbsp;&nbsp; -- etc &nbsp;&nbsp;&nbsp; end end

You can get the angle and call the function inside your ball’s collision handler ( where the ball’s coordinates are accessed by self.x and self.y).

Then you can use a conditional to check whether the colour hit is the same as the ball’s colour.

Oh, and don’t forget to make your spinner a physics object, or you won’t detect the collision.

Also, if you do it this way, you don’t need the test stuff you have near the bottom (which is broken anyway).

Thanks.

I typed that code inside ball’s collision and it’s the same thing like before.

Sorry,but I am new at Corona and in lua i just know physics and that’s all.

A small sample project to illustrate:

local physics = require "physics" physics.start() physics.setGravity(0, 0) local ball = display.newCircle(10, 10, 5) physics.addBody(ball, "dynamic") ball:setLinearVelocity(50, 50) local spinner = display.newImage("circle.png") spinner.x = display.contentWidth/2 spinner.y = display.contentHeight/2 spinner.rotation = math.random(1, 359) physics.addBody(spinner, "static", {isSensor = true, radius = spinner.width \* 0.46}) ball:toFront() local hitText = display.newText("no collision yet", 180, 30) -- draw lines to illustrate true divisions local myLines = {} local angle = 60 local rot = spinner.rotation for i = 1, 6, 1 do myLines[i] = display.newLine(spinner.x, spinner.y, spinner.x + 100 \* math.cos((angle + rot)\*3.14/180), spinner.y + 100 \* math.sin((angle + rot)\*3.14/180)) myLines[i]:setStrokeColor(0) angle = angle + 60 end local function angleToColour(angle) -- function assumes degrees, not radians local rot = spinner.rotation angle = angle - rot -- ensure a positive angle while angle \< 0 do angle = angle + 360 end if angle \>= 0 and angle \< 60 then return "green" elseif angle \>= 60 and angle \< 120 then return "cyan" elseif angle \>= 120 and angle \< 180 then return "blue" elseif angle \>= 180 and angle \< 240 then return "pink" elseif angle \>= 240 and angle \< 300 then return "red" elseif angle \>= 300 and angle \< 360 then return "yellow" end end onCollision = function( self, event ) if( event.phase == "began" ) then hitText.text = angleToColour(math.atan2(self.y - spinner.y, self.x - spinner.x)\* 180 / 3.14159265) end return true end ball.collision = onCollision ball:addEventListener("collision", ball)

Thank you!

It works,but now I want to make when red ball hits green part that it goes on restart screen.

How can I type that?

You should familiarise yourself with composer:

https://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

When your balls collide with the spinner, you can calculate the angle. If it is 0 to 60 degrees, you’ve hit the green, if it’s between 60 and 120, it’s hit the light blue, etc.

Thanks for that,but can you write that for code above? Please

You can use ball.x minus spinner.x and ball.y minus spinner.y to get the vector components, and use vector2Angle to get the angle.

You could pass this angle into a function to see what colour of the spinner it’s collided with. A function like this:

local function angleToColour(angle) &nbsp;&nbsp; &nbsp;-- function assumes degrees, not radians &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local rot = spinner.rotation &nbsp;&nbsp;&nbsp; angle = angle - rot &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;-- ensure a positive angle &nbsp;&nbsp; &nbsp;while angle \< 0 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;angle = angle + 360 &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp;&nbsp; if angle \>= 0 and angle \< 60 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return "green" &nbsp;&nbsp;&nbsp; elseif angle \>= 60 and angle \< 120 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return "cyan" &nbsp;&nbsp;&nbsp; -- etc &nbsp;&nbsp;&nbsp; end end

You can get the angle and call the function inside your ball’s collision handler ( where the ball’s coordinates are accessed by self.x and self.y).

Then you can use a conditional to check whether the colour hit is the same as the ball’s colour.

Oh, and don’t forget to make your spinner a physics object, or you won’t detect the collision.

Also, if you do it this way, you don’t need the test stuff you have near the bottom (which is broken anyway).

Thanks.

I typed that code inside ball’s collision and it’s the same thing like before.

Sorry,but I am new at Corona and in lua i just know physics and that’s all.

A small sample project to illustrate:

local physics = require "physics" physics.start() physics.setGravity(0, 0) local ball = display.newCircle(10, 10, 5) physics.addBody(ball, "dynamic") ball:setLinearVelocity(50, 50) local spinner = display.newImage("circle.png") spinner.x = display.contentWidth/2 spinner.y = display.contentHeight/2 spinner.rotation = math.random(1, 359) physics.addBody(spinner, "static", {isSensor = true, radius = spinner.width \* 0.46}) ball:toFront() local hitText = display.newText("no collision yet", 180, 30) -- draw lines to illustrate true divisions local myLines = {} local angle = 60 local rot = spinner.rotation for i = 1, 6, 1 do myLines[i] = display.newLine(spinner.x, spinner.y, spinner.x + 100 \* math.cos((angle + rot)\*3.14/180), spinner.y + 100 \* math.sin((angle + rot)\*3.14/180)) myLines[i]:setStrokeColor(0) angle = angle + 60 end local function angleToColour(angle) -- function assumes degrees, not radians local rot = spinner.rotation angle = angle - rot -- ensure a positive angle while angle \< 0 do angle = angle + 360 end if angle \>= 0 and angle \< 60 then return "green" elseif angle \>= 60 and angle \< 120 then return "cyan" elseif angle \>= 120 and angle \< 180 then return "blue" elseif angle \>= 180 and angle \< 240 then return "pink" elseif angle \>= 240 and angle \< 300 then return "red" elseif angle \>= 300 and angle \< 360 then return "yellow" end end onCollision = function( self, event ) if( event.phase == "began" ) then hitText.text = angleToColour(math.atan2(self.y - spinner.y, self.x - spinner.x)\* 180 / 3.14159265) end return true end ball.collision = onCollision ball:addEventListener("collision", ball)

Thank you!

It works,but now I want to make when red ball hits green part that it goes on restart screen.

How can I type that?

You should familiarise yourself with composer:

https://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/