Different Collision Reactions to Different Parts of an Image

How can I make my character react differently to different pieces of another image colliding with it. Lets say the character is simply a ball in the center of the screen. And from the left of the screen, a flood of color approaches that is much larger than the ball. This flood is split into two colors, red on top and blue on the bottom. You can swipe the ball up or down to line it up with the red or the blue. How can I make the collisions with the different colors fire off different functions? Basically how can i make the collision of two objects have different outcomes depending on where they collide? Not sure if that is too confusing or if I explained it poorly, my apologies in advance.

Hey, So do you want something like this? ( Check the attachment )

So the black dots are moving at the ball and if it touches the red part it turn red and if it touches the blue part it turns blue?.

–SonicX278

You could make it with two different objects, a red one and a blue one, and do your collision with that. You could also create two physics fluid simulations and use them to collide.

Though this is doable with a single object, I think the best way you’ll achieve this is with two different objects. That’ll make it much easier for you.

  • Caleb

That basically a concept. In what I am doing, basically if you hit one color you win and the other you lose. So how would i fire off the separate functions like that?

So how would i go about doing it with two objects? I tried that and the problem I am having is they don’t appear as one, they get caught and collide with each other and glitch around the screen instead of moving smoothly side by side and appearing as one object

Try the ‘isSensor’ property: it stops objects from bouncing off of each other but keeps them checking collisions.

https://docs.coronalabs.com/api/type/Body/isSensor.html

  • Caleb

Are you using physics?

Yes, and have done addBody and all that stuff. Im just trying to get them to react differently to different parts of the characters.

Can you draw out a sample of what you want on paint? Or something along that time? Like I did a couple posts back.

Thanks!

alright so basically if they green cube hits the blue, they score a point, if they hit the red, they lose

Does the blue part move up and down?

–SonicX278

All you’ll need to do is add a body to the red object and a body to the blue object. Set the isSensor parameter on both objects (or maybe just the blue object; all you need is for at least one to have isSensor), then listen for collisions.

To assure that the blue object registers collisions before the red object, you can make the blue object’s physics body just a tiny bit bigger in the X-axis than the red object’s physics body.

A complete working code example:

display.setStatusBar(display.HiddenStatusBar) math.randomseed(os.time()) require("physics") physics.start() physics.setGravity(0, 0) physics.setDrawMode("hybrid") local redWidth = display.contentCenterX \* 0.5 local redHeight = display.contentHeight local blueWidth = display.contentCenterX \* 0.05 local blueHeight = display.contentHeight \* 0.2 local blueMargin = 10 local playerRadius = 20 local red = display.newRect(display.screenOriginX + redWidth \* 0.5, display.contentCenterY, redWidth, redHeight) red:setFillColor(1, 0, 0) red.id = "red" local blue = display.newRect(redWidth - blueWidth \* 0.5, math.random(blueHeight \* 0.5, display.contentHeight - blueHeight \* 0.5), blueWidth, blueHeight) blue:setFillColor(0.5, 0.5, 1) blue.id = "blue" physics.addBody(red, "static", { isSensor = true }) physics.addBody(blue, "static", { isSensor = true, box = { halfWidth = blue.width \* 0.5 + blueMargin \* 0.5, halfHeight = blue.height \* 0.5 } }) local player = display.newCircle(display.contentWidth, display.contentCenterY, playerRadius) player.isBullet = true physics.addBody(player, { radius = player.width \* 0.5 }) function player:collision(event) if event.phase == "began" and not player.collidedAlready then if event.other.id == "red" then print("YOU DIED!") elseif event.other.id == "blue" then print("YOU LIVED!") end player.collidedAlready = true player:setLinearVelocity(0, 0) end end player:setLinearVelocity(-1000, 0) player:addEventListener("collision")
  • Caleb

Thanks Caleb! I’m currently at work so kinda was trying to help with my mobile phone so I can’t check the sample but I’m sure it spot on!

Thanks and good luck!

Wow this is great, thank you. I have been stuck on this for weeks and this will finally let me finish what I am working on. I truly appreciate it!

Thank you for you help!

Excellent. Glad to help :slight_smile:

  • Caleb

Hey, So do you want something like this? ( Check the attachment )

So the black dots are moving at the ball and if it touches the red part it turn red and if it touches the blue part it turns blue?.

–SonicX278

You could make it with two different objects, a red one and a blue one, and do your collision with that. You could also create two physics fluid simulations and use them to collide.

Though this is doable with a single object, I think the best way you’ll achieve this is with two different objects. That’ll make it much easier for you.

  • Caleb

That basically a concept. In what I am doing, basically if you hit one color you win and the other you lose. So how would i fire off the separate functions like that?

So how would i go about doing it with two objects? I tried that and the problem I am having is they don’t appear as one, they get caught and collide with each other and glitch around the screen instead of moving smoothly side by side and appearing as one object