applyTorque() on tap

Hello Im trying to apply a rotation to my display object when tapped by this code isnt working. Any suggestions on how to make it work.

local function bubbleTap(event) if event.phase == 'ended' then print(event.target.name) event.target:applyTorque(50) end end local animal = display.newImageRect("assets/images/animals/"..animalName..".png",65,65) physics.addBody(animal, 'kinematic', {radius=43, bounce = 0.0}) animal.x = bubble.x animal.y = \_H animal:setLinearVelocity(0,-80) animal.name = animalName animal.type = "animal" animal:toBack() animal:addEventListener('touch', bubbleTap)

I’m pretty sure the body needs to be “dynamic”.

“kinematic” ignores forces and Torque is a force.  i.e. While you can set a linearVelocity for a kinematic body, you cannot move it via collision, setForce(), or applyTorque().

Also, you may want to use the body mass to get a consistent application for force:

event.target:applyTorque(50 \* event.target.mass)

This way, if you change the size of the object later in development, the resultant change for a torque of 50 will stay the same.

-Ed

PS - Good post by the way.  Clear, concise, precise and you formatted your code legibly in a code block. Thanks!

Thanks @roaminggamer the code works if i do this:

if event.target.type == "animal" then print(event.target.name.." animal was tapped") audio.play(event.target.sound) event.target.bodyType = "dynamic" event.target:applyTorque(50\*event.target.mass) event.target:setLinearVelocity(0,100) end

but now this happens:

http://youtu.be/pbIpYRA2Ch4?hd=1

Everytime i tap the two objects, its like the first objects is blocking the second. Is there anyway to have these two objects act as one until tapped, then when tapped they separate. So if the bubble is tapped, the animals tap event also fires.

Thanks 

Answer

Just return false from your touch listeners and the touch event will continue to propagate.

Second Note (not about question)

Also, when watching the video, I wasn’t sure if those objects were supposed to collide with each other, but IF NOT…

Make them sensors.

-- After addBody call obj.isSensor = true

or use this filter when adding the body to make the objects collide with no other objects:

physics.addBody( obj, "dynamic", { filter = { groupIndex = - 1 } } )

Ahh thanks. And yes thank you, The objects colliding was my next concern. Thanks for the help!!

Hi @robertsthomasd,

The “collision filtering” that @roaminggamer mentions is perfectly valid, but I’ve always found the first method (categoryBits/maskBits) mentioned in the following guide to be more flexible, more customizable, and more future-proof if your game parameters change along the way:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#collision-filtering

Take care,

Brent

I’m pretty sure the body needs to be “dynamic”.

“kinematic” ignores forces and Torque is a force.  i.e. While you can set a linearVelocity for a kinematic body, you cannot move it via collision, setForce(), or applyTorque().

Also, you may want to use the body mass to get a consistent application for force:

event.target:applyTorque(50 \* event.target.mass)

This way, if you change the size of the object later in development, the resultant change for a torque of 50 will stay the same.

-Ed

PS - Good post by the way.  Clear, concise, precise and you formatted your code legibly in a code block. Thanks!

Thanks @roaminggamer the code works if i do this:

if event.target.type == "animal" then print(event.target.name.." animal was tapped") audio.play(event.target.sound) event.target.bodyType = "dynamic" event.target:applyTorque(50\*event.target.mass) event.target:setLinearVelocity(0,100) end

but now this happens:

http://youtu.be/pbIpYRA2Ch4?hd=1

Everytime i tap the two objects, its like the first objects is blocking the second. Is there anyway to have these two objects act as one until tapped, then when tapped they separate. So if the bubble is tapped, the animals tap event also fires.

Thanks 

Answer

Just return false from your touch listeners and the touch event will continue to propagate.

Second Note (not about question)

Also, when watching the video, I wasn’t sure if those objects were supposed to collide with each other, but IF NOT…

Make them sensors.

-- After addBody call obj.isSensor = true

or use this filter when adding the body to make the objects collide with no other objects:

physics.addBody( obj, "dynamic", { filter = { groupIndex = - 1 } } )

Ahh thanks. And yes thank you, The objects colliding was my next concern. Thanks for the help!!

Hi @robertsthomasd,

The “collision filtering” that @roaminggamer mentions is perfectly valid, but I’ve always found the first method (categoryBits/maskBits) mentioned in the following guide to be more flexible, more customizable, and more future-proof if your game parameters change along the way:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#collision-filtering

Take care,

Brent