How do i make an object stick to a spinning object?[SAMPLE CODE INCLUDED]

So i have a spinning object and another object that falls on that object… What i want it for that object to stick to the spinning one and spin around with it… 

Here’s some sample code!..

local physics = require("physics") physics.start() local spinner = display.newCircle( 0, 0, 50 ) spinner.x = display.contentCenterX spinner.y = display.contentCenterY spinner:setFillColor( 1, 0, 0 ) physics.addBody( spinner, "static", { bounce = 0 }) local player = display.newCircle( 0, 0, 25 ) player.x = display.contentCenterX player.y = display.contentCenterY - 200 player:setFillColor( 0, 0, 1 ) physics.addBody( player, "dynamic", { bounce = 0} ) local doTurn = function(event) spinner.rotation = spinner.rotation + 1 end Runtime:addEventListener( "enterFrame", doTurn )

Thanks!!!

Hi @SonicX278,

The best approach would be, upon collision, to attach the player to the spinner with a “weld” joint (or you could use a “distance” joint if you want some degree of flex). Note that adding a joint, however, cannot be done in the same time-step as the collision, so you’ll need to trigger it with a 10 millisecond timer or similar.

Also, you should probably spin the spinner by just setting its angular velocity… more efficient than running an “enterFrame” listener on it.

Take care,

Brent

Hey, So i tried doing this

local function checkCollision(event) if event.phase == "began" then local function dropDown() local weldJoint = physics.newJoint( "weld", player, spinner, anchor\_x, anchor\_y ) end timer.performWithDelay( 100, dropDown, 1 ) end end player:addEventListener( "collision", checkCollision )

But the player isnt stuck to the spinner… and its stops spinning it after like 100-200 pixels… any ideas why? 

Thanks!

That’s basically the right idea, but what is “anchor_x” and “anchor_y”. It looks like you just copied the example from the guide, but you didn’t specify what those variables actually are.

Also, you probably don’t need a delay as long as 100. 10 should suffice… it just needs to happen on the next time step basically.

Hello, So i got it working!

local physics = require("physics") physics.start() local spinner = display.newCircle( 0, 0, 50 ) spinner.x = display.contentCenterX spinner.y = display.contentCenterY spinner:setFillColor( 1, 0, 0 ) physics.addBody( spinner, "kinematic", { bounce = 0 }) spinner.gravityScale = 0 local player = display.newCircle( 0, 0, 25 ) player.x = display.contentCenterX player.y = display.contentCenterY - 70 player:setFillColor( 0, 0, 1 ) physics.addBody( player, "dynamic", { bounce = 0} ) spinner.angularVelocity = 50 local function touchJump(event) if event.phase == "began" then --player:applyForce( 0, -10, player.x, player.y ) --player:setLinearVelocity( 0, 10 ) end end Runtime:addEventListener( "touch", touchJump) local function checkCollision(event) if event.phase == "began" then local function dropDown() local weldJoint = physics.newJoint( "weld", player, spinner, player.x, player.y ) end timer.performWithDelay( 10, dropDown, 1 ) end end player:addEventListener( "collision", checkCollision )

But what i want is so i could touch anywhere and the player jumps and then comes back to the spinner…

but i cant get it to work with the touchJump function…

Thanks!

Well, it sort of depends on what you mean by “jump”. Do you want the player to completely detach from the spinner? Or do you want him to remain somewhat attached to the spinner, as if tethered by a flexible string, and then return to the spinner? These situations would require a different handling of joints…

Brent

Well what do you think? Lets say the circle is dis-formed like with hills and such stuff like that… and the circle is spinning and i want it to jump over it… how would that work?

Thanks!

Hey, So any ideas??

I’m not exactly sure what your game needs to do. Basically, a “weld” joint can firmly attach two bodies together, although it can optionally have some “flex”. If you want the player to detach from the spinner on jump, you should just break (remove) the joint entirely.

Brent

Ok so i almost have what i want… Heres what i have so far…

local physics = require("physics") physics.start() local spinner = display.newCircle( 0, 0, 50 ) spinner.x = display.contentCenterX spinner.y = display.contentCenterY spinner:setFillColor( 1, 0, 0 ) physics.addBody( spinner, "kinematic", { bounce = 0 }) spinner.gravityScale = 0 spinner.isSleepingAllowed = false local player = display.newCircle( 0, 0, 25 ) player.x = display.contentCenterX player.y = display.contentCenterY - 85 player:setFillColor( 0, 0, 1 ) physics.addBody( player, "dynamic", { bounce = 0} ) player.isSleepingAllowed = false spinner.angularVelocity = 50 local function touchJump(event) if event.phase == "began" then if weldJoint then display.remove(weldJoint) end player:applyForce( 0, -5, player.x, player.y ) elseif event.phase == "ended" then weldJoint = physics.newJoint( "weld", player, spinner, player.x, player.y ) end end Runtime:addEventListener( "touch", touchJump)

But what i want is so the player moves all the way to the spinner… But right now when i make the joint it just stops and spins around with the spinner…

Thanks!

So you’d like the player to move to the center of the spinner, instead of just attaching to the “outside” of it? In that case, a different joint type would probably be better… “distance” most likely.

Brent

Hi @SonicX278,

The best approach would be, upon collision, to attach the player to the spinner with a “weld” joint (or you could use a “distance” joint if you want some degree of flex). Note that adding a joint, however, cannot be done in the same time-step as the collision, so you’ll need to trigger it with a 10 millisecond timer or similar.

Also, you should probably spin the spinner by just setting its angular velocity… more efficient than running an “enterFrame” listener on it.

Take care,

Brent

Hey, So i tried doing this

local function checkCollision(event) if event.phase == "began" then local function dropDown() local weldJoint = physics.newJoint( "weld", player, spinner, anchor\_x, anchor\_y ) end timer.performWithDelay( 100, dropDown, 1 ) end end player:addEventListener( "collision", checkCollision )

But the player isnt stuck to the spinner… and its stops spinning it after like 100-200 pixels… any ideas why? 

Thanks!

That’s basically the right idea, but what is “anchor_x” and “anchor_y”. It looks like you just copied the example from the guide, but you didn’t specify what those variables actually are.

Also, you probably don’t need a delay as long as 100. 10 should suffice… it just needs to happen on the next time step basically.

Hello, So i got it working!

local physics = require("physics") physics.start() local spinner = display.newCircle( 0, 0, 50 ) spinner.x = display.contentCenterX spinner.y = display.contentCenterY spinner:setFillColor( 1, 0, 0 ) physics.addBody( spinner, "kinematic", { bounce = 0 }) spinner.gravityScale = 0 local player = display.newCircle( 0, 0, 25 ) player.x = display.contentCenterX player.y = display.contentCenterY - 70 player:setFillColor( 0, 0, 1 ) physics.addBody( player, "dynamic", { bounce = 0} ) spinner.angularVelocity = 50 local function touchJump(event) if event.phase == "began" then --player:applyForce( 0, -10, player.x, player.y ) --player:setLinearVelocity( 0, 10 ) end end Runtime:addEventListener( "touch", touchJump) local function checkCollision(event) if event.phase == "began" then local function dropDown() local weldJoint = physics.newJoint( "weld", player, spinner, player.x, player.y ) end timer.performWithDelay( 10, dropDown, 1 ) end end player:addEventListener( "collision", checkCollision )

But what i want is so i could touch anywhere and the player jumps and then comes back to the spinner…

but i cant get it to work with the touchJump function…

Thanks!

Well, it sort of depends on what you mean by “jump”. Do you want the player to completely detach from the spinner? Or do you want him to remain somewhat attached to the spinner, as if tethered by a flexible string, and then return to the spinner? These situations would require a different handling of joints…

Brent

Well what do you think? Lets say the circle is dis-formed like with hills and such stuff like that… and the circle is spinning and i want it to jump over it… how would that work?

Thanks!

Hey, So any ideas??

I’m not exactly sure what your game needs to do. Basically, a “weld” joint can firmly attach two bodies together, although it can optionally have some “flex”. If you want the player to detach from the spinner on jump, you should just break (remove) the joint entirely.

Brent

Ok so i almost have what i want… Heres what i have so far…

local physics = require("physics") physics.start() local spinner = display.newCircle( 0, 0, 50 ) spinner.x = display.contentCenterX spinner.y = display.contentCenterY spinner:setFillColor( 1, 0, 0 ) physics.addBody( spinner, "kinematic", { bounce = 0 }) spinner.gravityScale = 0 spinner.isSleepingAllowed = false local player = display.newCircle( 0, 0, 25 ) player.x = display.contentCenterX player.y = display.contentCenterY - 85 player:setFillColor( 0, 0, 1 ) physics.addBody( player, "dynamic", { bounce = 0} ) player.isSleepingAllowed = false spinner.angularVelocity = 50 local function touchJump(event) if event.phase == "began" then if weldJoint then display.remove(weldJoint) end player:applyForce( 0, -5, player.x, player.y ) elseif event.phase == "ended" then weldJoint = physics.newJoint( "weld", player, spinner, player.x, player.y ) end end Runtime:addEventListener( "touch", touchJump)

But what i want is so the player moves all the way to the spinner… But right now when i make the joint it just stops and spins around with the spinner…

Thanks!