Using Joints for a basket attached to an arm that swings

Hi everyone,

    I’m having a problem working with multiple physics objects connected with joints.  My goal is to create a basket that is attached to the end of a long arm.  The arm will rotate counter clockwise with the basket maintaining an upright orientation as it stays attached by a friction joint or a pivot. Keep in mind that I’m scaling down both images in Corona.

    Here is the code that I have so far.

    local basketBack = display.newImageRect( “images/general/swingBasket/basketBack.png”, 360, 306 )

    basketBack.xScale = 1/6 

    basketBack.yScale = 1/6

    basketBack.x = xP

    basketBack.y = yP

    --basketBack.isFixedRotation = true

    

    physics.addBody(basketBack, “dynamic”,

{ density = 1, friction = 0, bounce = 0, isSensor=false, shape = backShape[1]},

{ density = 1, friction = 1, bounce = 0, isSensor=false, shape = backShape[2]}, 

{ density = 1, friction = 1, bounce = 0, isSensor=false, shape = backShape[3]})

    local basketFront = display.newImageRect( “images/general/swingBasket/basket.png”,     360, 209 )

    basketFront.xScale = 1/6

    basketFront.yScale = 1/6

    basketFront.x = xP

    basketFront.y = yP + 8 

    physics.addBody(basketFront, “dynamic”,{ density = 3, friction = 0, bounce = 0, isSensor=true, shape = sensorShape})

    local basketArm = display.newImageRect( “images/general/swingBasket/basketArmSmall.png”, 978, 47 )

    basketArm:setReferencePoint(display.CenterRightReferencePoint)

    basketArm.xScale = 1/6

    basketArm.yScale = 1/5.75

    basketArm.x = xP + 141.8 --66.8

    basketArm.y = yP - 18.2

    physics.addBody(basketArm, “static”,{ density = 1, friction = 0, bounce = 0, isSensor=false, shape = armShape[1]},

                                            { density = 3, friction = 0, bounce = 0, isSensor=false, shape = armShape[2]})

    local frontBackJoint = physics.newJoint( “weld”, basketBack, basketFront, xP, yP )

    local backJoint = physics.newJoint( “friction”, basketBack, basketArm, xP, yP - 18.2 )

    backJoint.maxForce = 2

    backJoint.maxTorque = .1

I also have this in my main file

transition.to(swingBasket.arm, {time = 2000, rotation = -180})  

its referring to the arm just to be as clear as possible.

The reason I’m welding the front and the back is so that something can be put in between the two so it looks like the basket is actually holding an object.

I’m trying to rotate the arm from the right reference point but after I scale the object is seems that the joint develops and error and doesn’t recognize the scaling.  Also when I rotate from the centerRightReferencePoint the frictionjoint just remains in the initial position and the basket doesn’t move with the arm.

If anyone can help clear up I would be very grateful.

Rob

Hi Rob,

To begin solving this, I should note a few things about physics in Box2D in general. #1 is, scaling is not supported… you can scale the display object, but it will not scale the physics body(s) associated with it. So, if you attempt to scale and then apply joints, you will received unpredictable results. #2 is, physics work around the center reference point of an object, not a corner point. You should treat all bodies in this regard, and move/position them according to the central point. #3, you should probably be rotating the “arm” object using physics methods, not transitions. You can transition a physical object, but doing so can tend to “fight against” physical behavior.

Anyway, please work through the scenario with these things in mind, and post again with your progress report.

Best regards,

Brent Sorrentino

Hi Rob,

To begin solving this, I should note a few things about physics in Box2D in general. #1 is, scaling is not supported… you can scale the display object, but it will not scale the physics body(s) associated with it. So, if you attempt to scale and then apply joints, you will received unpredictable results. #2 is, physics work around the center reference point of an object, not a corner point. You should treat all bodies in this regard, and move/position them according to the central point. #3, you should probably be rotating the “arm” object using physics methods, not transitions. You can transition a physical object, but doing so can tend to “fight against” physical behavior.

Anyway, please work through the scenario with these things in mind, and post again with your progress report.

Best regards,

Brent Sorrentino