Different Collision Reactions to Different Parts of an Image

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