How to pin an object to the physics world

I have slight difficulty with creating physic objects with some specific properties.I really need to pin some of my objects to the world.

For example I have cross shaped bar which needs to stay still on the screen from its center point and not be affected from gravity and but when another object hits it should turn around from its fixed point.

Simple illustration: https://rapidshare.com/files/2845166383/cross.swf

When I use kinematic objects, my physics object is not affected from gravity but I cant also fix that object’s position and just allow rotation from the center point.

I will appreciate the help if someone knew an easy way to do it and have an idea.

Thanks in advance.

[import]uid: 191500 topic_id: 34297 reply_id: 334297[/import]

The short of it is that you need an anchor to hold a dynamic object in place. Just like a turning wheel needs something in the centre to rotate around, so do your objects in the Box2D world need something to spin around.

The good advice recommends using a static object in the Box2D. Then whenever an object needs to stay fixed in position join it to the static anchor. There are complex force vs object reasons, too, (but I’m hungover and don’t want to explain that right now) but just know that it involves the “immovable object and irresistible force” problem.

Here’s a little bit of code to show what I mean…

[lua]-- global anchor for holding anything in place
anchor = display.newGroup() – anchor - attach anything to this, there is no limit
physics.addBody( anchor, “static”, {isSensor=true, radius=10} ) – don’t let anything bounce off it
anchor.isVisible = false – make it invisible

– pin an object to the anchor and let it swing freely
local plank = display.newRect( 200, 200, 200, 50 ) – some object to attach to the anchor
physics.addBody( plank ) – make it physical
plank.joint = physics.newJoint(“pivot”,anchor,plank,250,225) – create the joint to let it swing[/lua] [import]uid: 8271 topic_id: 34297 reply_id: 136347[/import]

You will want to play around with using joints:

http://docs.coronalabs.com/api/library/physics/newJoint.html
[import]uid: 147305 topic_id: 34297 reply_id: 136350[/import]

Hey Horacebury;

I tried using a static anchor point with pivot joint and it worked perfectly well.

My mistake is trying to use touch joint and I couldn’t achieve the a perfectly functioning rotation with my object even I adjust the damping ratio to 0.

Thanks for the quick reply and perfectly working advice.

[import]uid: 191500 topic_id: 34297 reply_id: 136351[/import]

Yes; A touch joint simulates the user’s finger on the screen, not a connection between two physics bodies. [import]uid: 8271 topic_id: 34297 reply_id: 136353[/import]

Just to clarify a bit, the touch joint is useful for any point that the attached body should “follow”, not just a user touch or drag. I sometimes use it when I want a body to “attach” to a theoretical point on the screen (moving or stationary, the x/y can be passed in any method), and I want the motion to behave like an elastic band pulling the body toward the point. The added benefit of the touch joint is adjustable pull force and damping. A great joint to have in your arsenal!

Brent [import]uid: 200026 topic_id: 34297 reply_id: 136407[/import]

The short of it is that you need an anchor to hold a dynamic object in place. Just like a turning wheel needs something in the centre to rotate around, so do your objects in the Box2D world need something to spin around.

The good advice recommends using a static object in the Box2D. Then whenever an object needs to stay fixed in position join it to the static anchor. There are complex force vs object reasons, too, (but I’m hungover and don’t want to explain that right now) but just know that it involves the “immovable object and irresistible force” problem.

Here’s a little bit of code to show what I mean…

[lua]-- global anchor for holding anything in place
anchor = display.newGroup() – anchor - attach anything to this, there is no limit
physics.addBody( anchor, “static”, {isSensor=true, radius=10} ) – don’t let anything bounce off it
anchor.isVisible = false – make it invisible

– pin an object to the anchor and let it swing freely
local plank = display.newRect( 200, 200, 200, 50 ) – some object to attach to the anchor
physics.addBody( plank ) – make it physical
plank.joint = physics.newJoint(“pivot”,anchor,plank,250,225) – create the joint to let it swing[/lua] [import]uid: 8271 topic_id: 34297 reply_id: 136347[/import]

You will want to play around with using joints:

http://docs.coronalabs.com/api/library/physics/newJoint.html
[import]uid: 147305 topic_id: 34297 reply_id: 136350[/import]

Hey Horacebury;

I tried using a static anchor point with pivot joint and it worked perfectly well.

My mistake is trying to use touch joint and I couldn’t achieve the a perfectly functioning rotation with my object even I adjust the damping ratio to 0.

Thanks for the quick reply and perfectly working advice.

[import]uid: 191500 topic_id: 34297 reply_id: 136351[/import]

Yes; A touch joint simulates the user’s finger on the screen, not a connection between two physics bodies. [import]uid: 8271 topic_id: 34297 reply_id: 136353[/import]

Just to clarify a bit, the touch joint is useful for any point that the attached body should “follow”, not just a user touch or drag. I sometimes use it when I want a body to “attach” to a theoretical point on the screen (moving or stationary, the x/y can be passed in any method), and I want the motion to behave like an elastic band pulling the body toward the point. The added benefit of the touch joint is adjustable pull force and damping. A great joint to have in your arsenal!

Brent [import]uid: 200026 topic_id: 34297 reply_id: 136407[/import]