### Creating suspension with joints

#### ETHproductions

• Posts: 430
I'm working on a game similar to Hill Climb Racing, with two wheels loosely attached to the body of a vehicle. At the moment, I'm using the Joints extension to attach the wheels to the body. It would be pretty easy to use some revolute to make the tires stick to one place relative to the body, but I'd like there to be a bit of looseness to it, like suspension.

My current solution (which is not working) involves using some invisible "anchor" actors to attach the wheels to the center of the underside of the car using revolute joints. The revolute joints have their limit angles set to the proper amounts, which apparently need to be in radians, as opposed to degrees. However, the limits seem to be ignored if any force besides gravity is applied to the tires (for example, the ground). Observe (all limits set to 12 degrees, motors disabled):

<a href="https://www.dropbox.com/s/vb4s3ps1ur7y6yg/HillClimbTest.swf?raw=1" target="_blank" class="new_win">https://www.dropbox.com/s/vb4s3ps1ur7y6yg/HillClimbTest.swf?raw=1</a>

(Don't worry about the number in the top left; it's just the distance from the anchor to the front tire.)

I've experimented with a bunch of different variables, including:
- Limits set to the desired values (tires swing around very easily)
- Limits set to 12 (tires still swing around, but not as much)
- Gravity changed from 20 to 2 (tires swing around, slowly at first, then getting more and more crazy)
- Gravity set to 0 (tires swing slowly to 12 degrees)
- Enabling motors with motor speed and max torque of 500 (I can't tell the difference)

I've done a bit of research, and what I've found is that the line joint is the suggested joint to use for a tire (apparently, it was created for just this purpose). This was what I was originally intending to do; however, since there is no block to attach two actors with a line joint, I thought it was impossible.

So, my main question: Is it possible to attach two actors with a line joint? If not, I'd be happy to show my code for someone to look over. Thanks in advance!
Fontstruct - Stencyl - Website (in progress)

Proud Member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

• Posts: 2720
Try to set to 0.01 the mass of all car body parts, except for the wheels.
I'm spanish, excuse me for my bad English.
I'm not a private teacher. Please, post your questions in the public forum.

• Posts: 1795
Emanuele Feronato used prismatic joints to create a car's shocks in this tutorial: http://www.emanueleferonato.com/2011/08/22/step-by-step-creation-of-a-box2d-cartruck-with-motors-and-shocks/

Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

#### ETHproductions

• Posts: 430
@LIBERADO: Thanks for the idea! I already had the anchor actors set that way, but when I tried with the car body, it became even looser than before.

@SadiQ: Ah, so it is possible to create a line or prismatic joint between two actors! I'll look into how I'd use that code in Stencyl. Thanks for the link!
Fontstruct - Stencyl - Website (in progress)

Proud Member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

#### ETHproductions

• Posts: 430
Oh my word, I actually got it working.... on the first try!

To make it work, I edited the code for the Joints extension, copy-pasting the existing createLineJoint function and changing one line. Then I added in a corresponding extension block, and voila! It worked perfectly. Well, not perfectly; the suspension was still a little loose. But by tweaking the motors and the mass of the parts, I got it to work (almost) exactly how I wanted it to.

The one problem I have is that the joint's axis seems to swing around a little bit, but at this level, it doesn't much matter. At some point, after a bit more testing, I may share a version of the extension with the new block. I also think the extension deserves a lot more documentation than it has.

Here's the working prototype:

<a href="https://www.dropbox.com/s/8cqn3ktz4guyp94/HillClimbTest2.swf?raw=1" target="_blank" class="new_win">https://www.dropbox.com/s/8cqn3ktz4guyp94/HillClimbTest2.swf?raw=1</a>
Fontstruct - Stencyl - Website (in progress)

Proud Member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

• Posts: 2720
Wow! Congratulations, you have done a great job. The result is impressive!

I may share a version of the extension with the new block.
Yes, please! That would be very useful.
I'm spanish, excuse me for my bad English.
I'm not a private teacher. Please, post your questions in the public forum.

#### merrak

• Posts: 2735
That's an impressive result! I'd be interested in seeing the extension if you share it.

#### okyanusefer

• Posts: 40
Oh my word, I actually got it working.... on the first try!

To make it work, I edited the code for the Joints extension, copy-pasting the existing createLineJoint function and changing one line. Then I added in a corresponding extension block, and voila! It worked perfectly. Well, not perfectly; the suspension was still a little loose. But by tweaking the motors and the mass of the parts, I got it to work (almost) exactly how I wanted it to.

The one problem I have is that the joint's axis seems to swing around a little bit, but at this level, it doesn't much matter. At some point, after a bit more testing, I may share a version of the extension with the new block. I also think the extension deserves a lot more documentation than it has.

Here's the working prototype:

<a href="https://www.dropbox.com/s/8cqn3ktz4guyp94/HillClimbTest2.swf?raw=1" target="_blank" class="new_win">https://www.dropbox.com/s/8cqn3ktz4guyp94/HillClimbTest2.swf?raw=1</a>

Hi, Did you share it anywhere? I am looking for a tutorial similar to this one. I couldn't find on the forum. Thanks.

#### ETHproductions

• Posts: 430
Hi @okyanusefer,

Sorry for the late reply. I don't think I still have the original copy of this project, though I do have a modified one which is attached below. The physics are closer to those of Hill Climb Racing, which was what I was originally going for.

You'll need to install my modification of the Joints extension, also attached below, before opening the game. It adds one block for creating a line joint between two actors.

Hope this helps! If you need, I can do a little more investigation into getting it to run like the original demo I posted.
Fontstruct - Stencyl - Website (in progress)

Proud Member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

#### Bombini

• Posts: 1400
I love those type of games!