Hey all, the title pretty much explains exactly what I’m trying to do. This is what I have so far:
function spawnRagdoll(event)
if event.phase == "ended" then
ragdollGroup = display.newGroup()
ragdollBody = display.newImage("Art/ragdollBody.png")
ragdollArm1 = display.newImage("Art/ragdollArm.png")
ragdollArm2 = display.newImage("Art/ragdollArm.png")
physics.addBody(ragdollBody, {friction = 0.3})
physics.addBody(ragdollArm1, {friction = 0.3})
physics.addBody(ragdollArm2, {friction = 0.3})
physics.newJoint("pivot", ragdollBody, ragdollArm1, 28, 12)
physics.newJoint("pivot", ragdollBody, ragdollArm2, 0, 12)
ragdollGroup:insert(ragdollBody)
ragdollGroup:insert(ragdollArm1)
ragdollGroup:insert(ragdollArm2)
ragdollGroup.y = 430
local x = event.x
local y = event.y
local xForce = x - ragdollBody.x
local yForce = y - ragdollBody.y
ragdollBody:applyLinearImpulse(xForce, yForce, ragdollBody.x, ragdollBody.y)
end
end
Runtime:addEventListener("touch", spawnRagdoll)
This works, sort of…It fires in the general direction of the touch position, but only if I touch right at the top edge of the screen. Also, I’m only using the simulator right now. Tomorrow, I’ll probably get around to testing this on a real device. Thanks for any help, guys! [import]uid: 13174 topic_id: 5366 reply_id: 305366[/import]
you’re making the force power relative to the distance of your touch event from your ragdoll
[lua]local xForce = x - ragdollBody.x
local yForce = y - ragdollBody.y[/lua]
so obviously it’s going to fire further (by applying more force) the further away your touch event is.
if you want a consistent force you want to work out your angle then use trig to apply the correct value in each direction
something like this maybe (untested)
[lua]local pi = math.pi
local cos = math.cos
local sin = math.sin
local x = event.x
local y = event.y
local deltaX = x - ragdollBody.x
local deltaY = y - ragdollBody.y
local angle = math.deg( math.atan( deltaY / deltaX ) )
local force = 0.7 – change this to whatever value you need
local xForce = ( sin( angle * pi / 180 ) * force )
local yForce = ( cos( angle * pi / 180 ) * force )
Well, actually, I am trying to apply more force the farther away the touch is. But maybe I’m implementing it wrong? Also, it works fine if I don’t set the ragdollGroup’s coords right before I launch it. But, obviously that leaves it at 0, 0, and that’s not what I’m looking for. Thanks for your help! [import]uid: 13174 topic_id: 5366 reply_id: 17933[/import]