Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Different Collision Reactions to Different Parts of an Image
Started by mtuo Nov 11 2015 04:39 PM

15 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

mtuo

[GLOBAL: userInfoPane.html]
mtuo
  • Observer

  • 8 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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

Attached Files



[TOPIC: post.html]
#3

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

mtuo

[GLOBAL: userInfoPane.html]
mtuo
  • Observer

  • 8 posts
  • Corona SDK

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

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?



[TOPIC: post.html]
#5

mtuo

[GLOBAL: userInfoPane.html]
mtuo
  • Observer

  • 8 posts
  • Corona SDK

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

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



[TOPIC: post.html]
#6

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

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



[TOPIC: post.html]
#7

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

Are you using physics?

[TOPIC: post.html]
#8

mtuo

[GLOBAL: userInfoPane.html]
mtuo
  • Observer

  • 8 posts
  • Corona SDK

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.



[TOPIC: post.html]
#9

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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!

[TOPIC: post.html]
#10

mtuo

[GLOBAL: userInfoPane.html]
mtuo
  • Observer

  • 8 posts
  • Corona SDK

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

Attached Files



[TOPIC: post.html]
#11

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

Does the blue part move up and down?

--SonicX278

[TOPIC: post.html]
#12

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

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



[TOPIC: post.html]
#13

InfiSnyp

[GLOBAL: userInfoPane.html]
InfiSnyp
  • Corona Geek

  • 1,494 posts
  • Corona SDK

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!

[TOPIC: post.html]
#14

mtuo

[GLOBAL: userInfoPane.html]
mtuo
  • Observer

  • 8 posts
  • Corona SDK

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

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!



[TOPIC: post.html]
#15

mtuo

[GLOBAL: userInfoPane.html]
mtuo
  • Observer

  • 8 posts
  • Corona SDK

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!

Thank you for you help!



[TOPIC: post.html]
#16

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

Excellent. Glad to help :)

 

- Caleb




[topic_controls]
[/topic_controls]