### Joints Extension

#### captaincomic

• Posts: 6108
This extension will only work with Stencyl 3.1 or newer (build >= 7261).
The pulley joint block needs Stencyl 3.2 or newer (build >= 7754).

Joints Extension
This extension should work on every platform. I have only tested it on Flash, if you any issues with it let me know.

This extension provides blocks to create and destroy joints, and to modify their parameters.
To see what you can do with it, check out this Demo and the Ragdoll Test.  You can also find them on StencylForge as "Joints Sandbox" and "Ragdoll Test 3x".

There are currently blocks for all joints (that are implemented in the haxe port of Box2D) except for gear joints and friction joints.

To learn more about Box2D joints in general, take a look at the Box2D manual (PDF).

Download this Extension. (For instructions see How to Install and Update an Extension.)

Usage Notes
When a joint block expects coordinates for an anchor point of an actor, the coordinates are relative to their origin point of the actor (by default the center of the actor). If you want to attach a joint to the center of an actor, use ( x: 0, y: 0 ).

The blocks that only expect coordinates for one anchor point, calculate the coordinates of the second anchor point based on the position of the actors.

Use the blocks only for the type of joint that they are meant for. If you use a block on the wrong type of joint, you will get an error, like "Type Coercion failed: cannot convert box2D.dynamics.joints::B2RevoluteJoint@f1eae359 to box2D.dynamics.joints.B2DistanceJoint".

To specify the axis for line joints and prismatic joints, enter the coordinates of vector pointing in the desired direction, and make sure that the length of the vector is shorter than or equal to 1. For example, to create a line joint that restricts an actor to horizontal motion, you would enter ( x: 1 y: 0 ). For a 45° diagonal, you can enter ( x: sqrt(2)/2 y: sqrt(2)/2 ), which has length 1, or for example ( x: 0.5 y: 0.5 ), which is shorter than 1.

Most values of a newly created joint are 0 (or false) by default. This means that, for example, to turn on a motor, you need to enable the motor, set the maximum force and set the motor speed. Otherwise it will do nothing.

There is also a block to return the last created joint, a joint selected from the scene, or a joint attribute. Normally you won't need to use this block, as you get the same dropdown for the joint fields in the other blocks and attributes, but sometimes it can be useful, for example, for storing joints in a list.

Mouse Joints
Mouse joints are useful to drag actors in a game with physics, since the mouse joint will drag the actor towards the mouse and thus works well with collisions (unlike setting actor coordinates directly). Usually you will want to set the target of the mouse joint to the current mouse coordinates in every frame. Make sure to set the maximum force (1000 * mass of the actor is a good starting point), otherwise the mouse joint won't have an effect.

Distance Joints
A distance joint fixes the distance between two actors, or one actor and a point on the scene.
* Stick joints created in the scene editor are distance joints internally.

Line Joints
A line joint restricts the motion of the actor to a given axis. Rotation is still allowed.
* Slide joints created in the scene editor are line joints internally.

Prismatic Joints
Like a line joint, a prismatic joint restricts the motion of the actor to a given axis, but rotation is also restricted.

Revolute Joints
A revolute joint constrains two bodies to share a common point while they are free to rotate about that point.
There are four different blocks to create a revolute joint, to connect two actors, or to attach an actor to the scene, and with one anchor point (the other one calculated automatically) or two anchor points, respectively.
* Hinge joints created in the scene editor are revolute joints internally.

Weld Joints
A weld joint glues two actors together, or glues one actor to the scene. This can be useful for creating an object made out of multiple pieces that can fall apart (when you destroy the weld joints that held the pieces together).

Pulley Joints
A pulley joints connects two actors with a rope and a pulley (or two) defined by the anchor points.

Version History
1.0: Initial version, with blocks for mouse joints and revolute joints
1.1: Updated to use joint attributes
1.2: Added blocks for distance, line, prismatic and weld joints
1.3: Fixed set/get length of distance joints not accounting for physics scale
1.4: Added blocks for pulley joints

« Last Edit: May 14, 2017, 11:14:48 am by captaincomic »

• Posts: 853
Yoohoo!!

#### HopFrog

• Posts: 45
This is awesome

#### juanj56

• Posts: 22
Wow! Thanks! this is awesome!

#### SadiQ

• Posts: 1795
Apparently I am unable to install this extension (using build 7281). Whenever I try to install it nothing happens and if I try to open the zip file I get a "Zip folder is invalid" message.  I downloaded the file several times but still the same result.
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

#### captaincomic

• Posts: 6108
Apparently the zip was corrupted. Please try again, I uploaded a new one.

#### SadiQ

• Posts: 1795
Now it works properly.
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

#### Almakos

• Posts: 632
According to the examples (Demo and the Ragdoll Test) this extension is totally awesome.
But for some reason I am having difficulties setting up my actors so that they behave similar to the ones in Demo...
If I create distance joint it works like something very stretchy...what value could I put to prevent any stretching? I want a firm distance joint similar to the demo example.
I've tried experimenting with length, frequency and damping but none of my tests were successful...
are there any special requirements for the actors?
Thank you
Fishing Fantasy Trailer

#### SadiQ

• Posts: 1795
If you set the distance for that joint inside the Always event, the distance is still not constant?
The default values for the frequency and damping ratio should provide a non oscillating joint (if I'm not mistaken).
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

#### Almakos

• Posts: 632
seems it is highly dependant on the weight of the object...so in order to make it work like in demo objects should be very light
and putting length in always block doesn't help. yes I've expected that default frequency and damping values will make rock solid connection, but turns out that if object is heavy (20 kg) and gravity is high then connection will be stretchy or buggy, hard to say for sure.
Fishing Fantasy Trailer

#### SadiQ

• Posts: 1795
Try setting the Damping ratio to 1 and frequency to 0 and see if it helps. If not you may need captaincomic's help
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

#### captaincomic

• Posts: 6108
That reminds me, I wanted to put the Demo on StencylForge.

It kind of makes sense that heavier objects and higher gravity makes the joints weaker, as you are applying stronger forces on the objects and the joints have to work against those forces. I don't know how to make a joint stronger though. I will also experiment a bit.

Edit: The "Joints Sandbox" and "Ragdoll Test 3x" are now public on StencylForge.

« Last Edit: May 17, 2014, 02:17:46 pm by captaincomic »

#### captaincomic

• Posts: 6108
I cannot reproduce a stretchy distance joint with 20 kg actors and 85 gravity.

But I found a bug in the "set length" block: it didn't convert to argument to Box2D units (= factor 10 of pixel units).  Maybe that was the problem, that the joint was much longer than you wanted it to be? Anyway, it's fixed now.

Btw. setting the length is only needed when you want to change the length. Normally you can just place the actors in the right distance to each other and then create the joint and the length will be initialized accordingly. Setting the length in "always" vs. setting it once also won't make a difference.

#### Almakos

• Posts: 632
turns out one of the actors I've had was set to normal in physics settings and I've been updating its position and rotation through some code and with that weird set up I was getting stretching if another actor was heavy enough. After I've set first actor to cannot be pushed, everything fixed immediately.
Huge thanks for putting demo scene on Forge. It was very helpful.
And thank you for this wonderful extension.
Fishing Fantasy Trailer

#### captaincomic

• Posts: 6108
Ah, ya, setting position directly doesn't work well with joints.

You're welcome