Sticking one object to the other

Hello there,

I have a question that needs an answer in two different aspects.

The story: I have a physics body (character) that user moves along x axis. There are crates (also physics bodies) falling from sky at random x points. When the main body picks up the crates (thus collides), I want the crates to stick to the character. User will also be able to throw the crates away by swiping.

So by my very limited knowledge and relentless searching, I have two schools of thought here:

  1. When the two objects collide (which I have succeeded in detecting) we can make the crate static, and somehow stick it to character, and make it dynamic again when we detect a swipe.

  2. When the two objects collide, we can create a simple joint between them, not disturbing the physics at all, which looks the more logical way.

And my question is:

How the hell do I do that?

If we go with the first way, I need to set crate’s x to characters x every frame, and stop doing that once I detect a swipe. How can I do that?

If we go with the latter, I kindly ask a noob tutorial for this as my brain literally hurts when I read it in the api reference. I am, of course, wide open to suggestions.

Thank you very much,

Kaan.
[import]uid: 99031 topic_id: 30192 reply_id: 330192[/import]

Hey Kaan-

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 9.5 )
–physics.setDrawMode ( “hybrid” )

local player = display.newCircle( 160, 240, 20 )
physics.addBody(player, “static”, {radius=20})

local crate = display.newRect( 160, 0, 40, 40 )
physics.addBody(crate, “dynamic”)

local function onCollision(event)
if event.phase == “ended” then
timer.performWithDelay(1, function()
myJoint = physics.newJoint( “weld”, player, event.other, player.x,player.y, event.other.x,event.other.y )
end, 1)
end
end
player:addEventListener(“collision”, onCollision)[/lua]

That is very basic but should get you started with joints - no movement with player, could switch to dynamic if you wanted - but it’s 1:30am and wanted to throw something up here before it got any later.

Let me know how you get on.

Peach :slight_smile: [import]uid: 52491 topic_id: 30192 reply_id: 120972[/import]

Hey Kaan-

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 9.5 )
–physics.setDrawMode ( “hybrid” )

local player = display.newCircle( 160, 240, 20 )
physics.addBody(player, “static”, {radius=20})

local crate = display.newRect( 160, 0, 40, 40 )
physics.addBody(crate, “dynamic”)

local function onCollision(event)
if event.phase == “ended” then
timer.performWithDelay(1, function()
myJoint = physics.newJoint( “weld”, player, event.other, player.x,player.y, event.other.x,event.other.y )
end, 1)
end
end
player:addEventListener(“collision”, onCollision)[/lua]

That is very basic but should get you started with joints - no movement with player, could switch to dynamic if you wanted - but it’s 1:30am and wanted to throw something up here before it got any later.

Let me know how you get on.

Peach :slight_smile: [import]uid: 52491 topic_id: 30192 reply_id: 120972[/import]