Detecting touch event on different parts of a multi element object

Hi guys,

Can you help me out here.

Lets say I have a multi element body created in Physics editor.

Man character is created with two legs, two arms, one body and one head.

local man = display.newImage( "man.png" ) -- LOAD ONE IMAGE OR LOAD MULTIPLE IMAGES?? man.x, man.y = display.contentWidth/2, display.contentHeight/2-200   local leftLeg = { -- some shape numbers here --} local rightLeg = { -- some shape numbers here --} local body = { -- some shape numbers here --} local leftArm = { -- some shape numbers here --} local rightArm = { -- some shape numbers here --} local head = { -- some shape numbers here --}   physics.addBody( man, "dynamic", {shape=leftLeg}, {shape=rightLeg}, {shape=body}, {shape=leftArm}, {shape=rightArm}, {shape=head} )

How can I remove man`s body parts on touch (not collision)?

I do not want to set alpha of touched body parts to zero, I want to remove touched body parts.

So if you touch left arm for example, man looses his left arm.

If removing man`s body parts on touch is not possible, can you hint how to set touched body parts alpha to zero and animate touched body parts?

I am not interested in collisions so far.

Waiting your reply.

Many, many thanks!

Ivan

Hi Ivan,

This is possible, but it requires a little trickery. Because you’re trying to detect touch on a specific body part, but a specific part is only revealed in a collision event, you should still use collisions but in a different way. Basically, create a small circular physics object and ensure that it’s a sensor type. That object could be around the size of a user’s “touch” on the screen (like the size of a fingerprint or a little smaller).

Next, create a touch handler that, when the user touches the screen, it moves the circle to that location. Also, build your collision handler, and make sure that when the circle collides with the man’s body, it reveals the number (index) of the body part, as outlined in the guides/docs. In there, use the values to remove the appropriate body part.

IMPORTANT:

I just realized that you’re trying to remove one part of a multi-element body. You can’t do that… once the body is created, it’s an established body and can’t be altered. The workaround would be to assemble the man from individual physics bodies, and if necessary, join the body parts together via joints if the man needs to move as a whole unit.

Best regards,

Brent

Hi Brent,

Many thanks for a lengthy reply.

If I assemble man from individual physics bodies, how to make a smooth movement (walking, running, jumping…)?

If I apply a linearVelocity to individual body parts it is gonna look more like Break dancing than walking  :D 

Is there a code example or tutorial regarding multi-body joint movement?

Waiting your reply.

Many thanks.

Ivan

Ivan, how are you going to move it though? If you have a complete multi element body and you want it to interact with the environment, are you going to make the interaction with the user by buttons or touch to the screen? Remember that user doesn’t have a keyboard and input is limited, if you want to make a platform game i’d recommend you using spritesheets.

If you want to use a ragdoll with a multi-element physics body (for a different game) you can use a ragdoll (there are some tutorials for ragdolls in the latest Corona Geek videos http://coronageek.com/). Remember, like Brent said, you will need to create a workaround about not being able to remove parts of the multi-element body (one option that comes to my mind is when part of the body is removed, get the ID, make a shiny effect, remove all and create the multi-element body again without that part using said ID.

Hi undecode,

I agree that moving multi-element body on the phone is not practical way to solve things.

I think I am gonna play around with sprite sheets.

One last question:

  1. Do you think that removing and then creating new body on x and y of removed body is not gonna make a position offset visible to the player?

Many thanks.
Ivan

It depends on what you want to achieve.

I would do that only if the body is not moving, as i want to keep the minimun manual interaction with a physics body that is currently active, if i want to load it again.  

You could remove the object and put it in the same position, but what is the purpose of removing in the first time?

Are you loading a different image? If so, you might want to add a shiny/explosion effect so people don’t see the transition.

Hi undecode,

I want to remove the body in order to reduce the arm of leg.

Is that acceptable to Apple or Google policies (I will not use blody or similar effects)?

Ivan

Hi Ivan,

This is possible, but it requires a little trickery. Because you’re trying to detect touch on a specific body part, but a specific part is only revealed in a collision event, you should still use collisions but in a different way. Basically, create a small circular physics object and ensure that it’s a sensor type. That object could be around the size of a user’s “touch” on the screen (like the size of a fingerprint or a little smaller).

Next, create a touch handler that, when the user touches the screen, it moves the circle to that location. Also, build your collision handler, and make sure that when the circle collides with the man’s body, it reveals the number (index) of the body part, as outlined in the guides/docs. In there, use the values to remove the appropriate body part.

IMPORTANT:

I just realized that you’re trying to remove one part of a multi-element body. You can’t do that… once the body is created, it’s an established body and can’t be altered. The workaround would be to assemble the man from individual physics bodies, and if necessary, join the body parts together via joints if the man needs to move as a whole unit.

Best regards,

Brent

Hi Brent,

Many thanks for a lengthy reply.

If I assemble man from individual physics bodies, how to make a smooth movement (walking, running, jumping…)?

If I apply a linearVelocity to individual body parts it is gonna look more like Break dancing than walking  :D 

Is there a code example or tutorial regarding multi-body joint movement?

Waiting your reply.

Many thanks.

Ivan

Ivan, how are you going to move it though? If you have a complete multi element body and you want it to interact with the environment, are you going to make the interaction with the user by buttons or touch to the screen? Remember that user doesn’t have a keyboard and input is limited, if you want to make a platform game i’d recommend you using spritesheets.

If you want to use a ragdoll with a multi-element physics body (for a different game) you can use a ragdoll (there are some tutorials for ragdolls in the latest Corona Geek videos http://coronageek.com/). Remember, like Brent said, you will need to create a workaround about not being able to remove parts of the multi-element body (one option that comes to my mind is when part of the body is removed, get the ID, make a shiny effect, remove all and create the multi-element body again without that part using said ID.

Hi undecode,

I agree that moving multi-element body on the phone is not practical way to solve things.

I think I am gonna play around with sprite sheets.

One last question:

  1. Do you think that removing and then creating new body on x and y of removed body is not gonna make a position offset visible to the player?

Many thanks.
Ivan

It depends on what you want to achieve.

I would do that only if the body is not moving, as i want to keep the minimun manual interaction with a physics body that is currently active, if i want to load it again.  

You could remove the object and put it in the same position, but what is the purpose of removing in the first time?

Are you loading a different image? If so, you might want to add a shiny/explosion effect so people don’t see the transition.

Hi undecode,

I want to remove the body in order to reduce the arm of leg.

Is that acceptable to Apple or Google policies (I will not use blody or similar effects)?

Ivan