1
\$\begingroup\$

How can I create rectangles in Farseer that behave like the two red rectangles in this video:

https://www.youtube.com/watch?v=w0p2y4dRylQ

I created an angle joint, but it behaves weird. Should I use an angle joint or is it better to use another joint?

In addition, I don't know how to adjust the angle of the joint so that the rectangle only moves in a predefined angle. How can I limit the movement angle of a joint?

I tried it like this, but the rectangle moves not as it should:

       RectangleA1 = BodyFactory.CreateRectangle(world, 3.50f, 0.50f, 1.0f);
       RectangleA1.BodyType = BodyType.Dynamic;
       RectangleA1.Position = new Vector2(10.4f, 5.40f);
       RectangleA1.Rotation = 0f;
       RectangleA1.CollisionCategories = Category.Cat5;

       RectangleA2 = BodyFactory.CreateRectangle(world, 0.10f, 1.00f, 1.0f);
       RectangleA2.BodyType = BodyType.Static;
       RectangleA2.Position = new Vector2(8.6f, 6.2f);
       RectangleA2.Rotation = 0f;
       RectangleA2.CollisionCategories = Category.Cat5;

       AngleJoint1 = JointFactory.CreateAngleJoint(world, RectangleA1, RectangleA2);

       spriteBatch.Draw(RectangleA1Sprite, ConvertUnits.ToDisplayUnits(RectangleA1.Position), null, Color.White, RectangleA1.Rotation, new Vector2(RectangleA1Sprite.Width / 2.0f, RectangleA1Sprite.Height / 2.0f), 1f, SpriteEffects.None, 0f);

       spriteBatch.Draw(RectangleA2Sprite, ConvertUnits.ToDisplayUnits(RectangleA2.Position), null, Color.White, RectangleA2.Rotation, new Vector2(RectangleA2Sprite.Width / 2.0f, RectangleA2Sprite.Height / 2.0f), 1f, SpriteEffects.None, 0f);
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Please read the documentation. If you're not building Farseer from its source code - please do that so you can easily go in and see these things. (Plus, you can glean even more information from the source code, even where it is not documented.)


For now, let's take a look at the documentation. First, AngleJoint (from the top of AngleJoint.cs in the Farseer source code):

Maintains a fixed angle between two bodies

That's clearly not what you want. There is no physical "connection" between the two joined bodies with an angle joint. They can move freely in space - as long as they maintain the same angle relative to each other.

What you want is the RevoluteJoint. The documentation says:

A revolute joint [const]rains [two] bodies to share a common point while they are free to rotate about the point. The relative rotation about the shared point is the joint angle. You can limit the relative rotation with a joint limit that specifies a lower and upper angle. You can use a motor to drive the relative rotation about the shared point. A maximum motor torque is provided so that infinite forces are not generated.

(Emphasis mine)

This is exactly what you want. Create a revolute joint as per your last question. Then set the values of LowerLimit and UpperLimit to the angle limits you desire in radians. Then set LimitEnabled = true.


It's worth noting that the video you linked is from 2008. This means it is probably running Farseer 2, rather than Farseer 3. Farseer 3 was a rewrite of Farseer 2. In fact, Farseer 3 is basically a port of Box2D. In the switch to Box2D, a lot of the nomenclature changed.

\$\endgroup\$
1
  • \$\begingroup\$ Thanx. It works. \$\endgroup\$ Commented Jun 16, 2014 at 15:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .