2
\$\begingroup\$

and apologies in advance for this fairly random question.

I'm using Love2D 0.9.1 and Box2D to create two bodies joined by a revolute joint. A simplified representation of the two bodies is a Sphere and a rectangle:

hero.heroPhysics = {}
hero.heroPhysics.body = love.physics.newBody(theWorld, hero.x,hero.y,"dynamic")
hero.heroPhysics.shape = love.physics.newCircleShape(CONSTANT_heroRadius)

hero.extraPhysics = {}
hero.extraPhysics.body = love.physics.newBody(theWorld, hero.x,hero.y,"dynamic")
hero.extraPhysics.shape = love.physics.newRectangleShape(CONSTANT_heroRadius * 3, 8) 

hero.jointPhysics = {}
local hx,hy = hero.heroPhysics.body:getWorldCenter() 
hero.jointPhysics.joint = love.physics.newRevoluteJoint(hero.heroPhysics.body, hero.extraPhysics.body, hx, hy, false)

Everything seems to be ok, except that the anchor points of the joint are the centers of mass of the two bodies, and I'd like to manually adjust the anchor point of the rectangle to be closer to one end of the rectangle. However, I can't find a way in Love2D to set the joint anchor points? I can get the anchor points via Joint:getAnchors(), but not set them? Or maybe I'm missing something.

In pictures, I'm only able to get something resembling the bottom picture, whereas I really want something like the top picture (please excuse crude drawing):

a crude drawing

I've tried changing the rectangle body's center of mass using body:setMassData(), but that doesn't seem to make a difference (unless I try to offset it too much, at which point Box2D dies).

\$\endgroup\$
1
\$\begingroup\$

The method of controlling the anchor points on both of the bodies doesn't seem straightforward at first, but if you think about it in the following manner, it makes sense.

Consider two pieces of construction paper such as the rectangle and circle in your example. I can translate and rotate these two pieces anywhere I want on a flat surface. They are allowed to overlap. If I want to join them together at a point while allowing them to rotate relative to one another, I would position them in an appropriate starting position and then insert a split push pin at the rotation point (i.e. the pink dot on your drawings).

Split push pin:

Split pin image

This is how the anchor points are specified in box2d. Like pieces of construction paper, you should first position the bodies appropriately and then create the revolute joint. The only line in your example that needs to change is where you create the other physics body:

hero.extraPhysics.body = love.physics.newBody(theWorld, hero.x + CONSTANT_heroRadius*(3/8), hero.y,"dynamic")

This will position the center of the extraPhysics.body 3/8 of the CONSTANT_heroRadius to the right, so that when the revolute joint is created, the desired local anchor points will be set on each body.

\$\endgroup\$
  • \$\begingroup\$ Thanks so much! Your description makes things much clearer to me. \$\endgroup\$ – Danhoo Jul 8 '14 at 2:44
  • \$\begingroup\$ No problem, good luck with the game. \$\endgroup\$ – NauticalMile Jul 8 '14 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.