# Havok Spring Joints?

**URL:** <https://forum.babylonjs.com/t/havok-spring-joints/40997>\
**Category:** Questions\
**Tags:** physics, havok\
**Created:** [May 20, 2023, 3:13pm UTC](https://forum.babylonjs.com/t/havok-spring-joints/40997 "2023-05-20T15:13:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamessimo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jamessimo/32/26847_2.png) [@jamessimo](https://forum.babylonjs.com/u/jamessimo)\
**Post date:** [May 20, 2023, 3:13pm UTC](https://forum.babylonjs.com/t/havok-spring-joints/40997/1 "2023-05-20T15:13:02Z")

</div>

I wanted to ask if with Physics v2 there is anyway to have something like a spring joint like this [https://playground.babylonjs.com/#0GNRK9#12](https://playground.babylonjs.com/#0GNRK9#12)

I noticed that spring joints used to be a cannonJS only feature and nothing related to spring joints have been brought to the physics v2.

I hope there is a way I can simulate something like a spring or elastic joint.

---

<div class="post-metadata">

**Author:** ![CodingCrusader](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/codingcrusader/32/29857_2.png) [@CodingCrusader](https://forum.babylonjs.com/u/CodingCrusader)\
**Post date:** [May 21, 2023, 7:13am UTC](https://forum.babylonjs.com/t/havok-spring-joints/40997/2 "2023-05-21T07:13:45Z")

</div>

I have no explicit example for you, but asking chatGPT reveals this:

As of my knowledge cutoff in September 2021, Havok does not have a specific joint type called “Spring Joints.” However, Havok provides a comprehensive set of joint types that allow developers to create various physical connections between objects in a simulated environment. These joint types include revolute, prismatic, spherical, and distance joints, among others.

While Havok does not have a dedicated “Spring Joint,” developers can often achieve similar behavior by combining existing joint types with additional constraints or customization. By appropriately configuring parameters such as limits, stiffness, and damping, developers can create spring-like effects using the available joint types in Havok.

It’s important to note that Havok’s capabilities and feature set may have evolved since my last update. I recommend referring to the official Havok documentation or contacting the Havok development team for the most up-to-date information on the available joint types and their functionalities

---

<div class="post-metadata">

**Author:** ![CodingCrusader](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/codingcrusader/32/29857_2.png) [@CodingCrusader](https://forum.babylonjs.com/u/CodingCrusader)\
**Post date:** [May 21, 2023, 7:17am UTC](https://forum.babylonjs.com/t/havok-spring-joints/40997/3 "2023-05-21T07:17:44Z")

</div>

**How would a code look like for a simulation of a spring joint?**

Here’s an example of how a simple simulation of a spring joint could be implemented using a physics engine like Havok. This code assumes you have a basic understanding of using Havok or a similar physics engine in your programming language of choice. Keep in mind that this is a simplified example and may require adjustments based on your specific implementation and requirements.

```auto
// Assume you have two rigid bodies, bodyA and bodyB, that you want to connect with a spring joint

// Create bodies and initialize their positions, velocities, and other properties
RigidBody bodyA, bodyB;
bodyA.SetPosition(Vector3(0.0f, 0.0f, 0.0f));
bodyB.SetPosition(Vector3(1.0f, 0.0f, 0.0f));

// Define spring properties
float springConstant = 100.0f; // Stiffness of the spring
float dampingFactor = 0.5f; // Damping coefficient

// Update loop (simulation step)
while (simulationRunning) {
    // Calculate the spring force
    Vector3 springForce = (bodyB.GetPosition() - bodyA.GetPosition()) * springConstant;

    // Calculate the relative velocity between the bodies
    Vector3 relativeVelocity = bodyB.GetVelocity() - bodyA.GetVelocity();

    // Apply damping to the relative velocity
    Vector3 dampingForce = relativeVelocity * dampingFactor;

    // Calculate the total force applied on bodyA and bodyB
    Vector3 totalForceOnA = springForce - dampingForce;
    Vector3 totalForceOnB = -springForce - dampingForce;

    // Apply the forces to the bodies
    bodyA.ApplyForce(totalForceOnA);
    bodyB.ApplyForce(totalForceOnB);

    // Step the physics simulation (e.g., using Havok's update function)
    physicsWorld.Update(timeStep);
}

```

In this example, the spring force is calculated as the displacement between the two bodies (bodyB’s position minus bodyA’s position) multiplied by the spring constant. The relative velocity between the bodies is obtained by subtracting bodyA’s velocity from bodyB’s velocity. Damping force is then calculated by multiplying the relative velocity by a damping factor. The total forces are applied to the bodies using the `ApplyForce` function provided by Havok or your chosen physics engine.

Remember that this is just a basic example, and you may need to consider additional factors such as mass, time integration, collision handling, and more to create a more accurate and robust simulation.

* * *

I hope this gives you enough input to create a spring joint in bb! 🙂

More input about joints:  
[Havok set joints positions on Raycast - Questions - Babylon.js (babylonjs.com)](https://forum.babylonjs.com/t/havok-set-joints-positions-on-raycast/40888/4)

---

<div class="post-metadata">

**Author:** ![jamessimo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jamessimo/32/26847_2.png) [@jamessimo](https://forum.babylonjs.com/u/jamessimo)\
**Post date:** [May 22, 2023, 3:48am UTC](https://forum.babylonjs.com/t/havok-spring-joints/40997/4 "2023-05-22T03:48:29Z")

</div>

So ChatGPT may need to read more lol.

Unity has Havok Spring Joints. But to be fair there also called “Soft Joints” sometimes and I have seen some people call them Elastic joints.

If anyone else knows a way to do this would be really appreciative. If not I will try doing it myself via applying Forceses.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [May 22, 2023, 12:31pm UTC](https://forum.babylonjs.com/t/havok-spring-joints/40997/5 "2023-05-22T12:31:19Z")

</div>

@Cedric spring joints would be a nice feature to have a helper, but in the meantime it’s doable by manual forces applications

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [May 22, 2023, 12:53pm UTC](https://forum.babylonjs.com/t/havok-spring-joints/40997/6 "2023-05-22T12:53:55Z")

</div>

Yes, indeed. I’m wondering if it’s just a matter or ‘tweaking’ constraints and having a wrapper like the other ones.  
I’m opening an issue to keep track : [[Physics] Spring constraint · Issue #13884 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/issues/13884)

---

<div class="post-metadata">

**Author:** ![jeremy-coleman](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeremy-coleman/32/15493_2.png) [@jeremy-coleman](https://forum.babylonjs.com/u/jeremy-coleman)\
**Post date:** [May 23, 2023, 10:05pm UTC](https://forum.babylonjs.com/t/havok-spring-joints/40997/7 "2023-05-23T22:05:49Z")

</div>

Jiggle jiggle, i like to see you wiggle wiggle

---

<div class="post-metadata">

**Author:** ![jamessimo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jamessimo/32/26847_2.png) [@jamessimo](https://forum.babylonjs.com/u/jamessimo)\
**Post date:** [May 24, 2023, 5:57am UTC](https://forum.babylonjs.com/t/havok-spring-joints/40997/8 "2023-05-24T05:57:01Z")

</div>

Whiles not a spring joint. I was surprised that distance joints didn’t offer a “min, max” as well as a restitution. That would act like a spring. If you need it to be a fixed distance then you could just not add max and restitution parameters.

---

<div class="post-metadata">

**Author:** ![Earl-of-Snadwich](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@Earl-of-Snadwich](https://forum.babylonjs.com/u/Earl-of-Snadwich)\
**Post date:** [November 26, 2024, 9:59am UTC](https://forum.babylonjs.com/t/havok-spring-joints/40997/9 "2024-11-26T09:59:30Z")

</div>

You can also enable spring joint behavior by setting the joint stiffness and damping. I think this can only be done with the 6-DOF style of constraint a la:

```auto
    dynamicJoint = new BABYLON.Physics6DoFConstraint(
            {
                pivotA: new BABYLON.Vector3(0, 0, -4),
                axisA: new BABYLON.Vector3(1, 0, 0),
                pivotB: new BABYLON.Vector3(0, 0, 0),
                axisB: new BABYLON.Vector3(1, 0, 0),
            },
            [
                {
                    axis: BABYLON.PhysicsConstraintAxis.LINEAR_DISTANCE,
                    minLimit: 0,
                    maxLimit: 0,
                    stiffness: 500000,
                    damping: 10000
                }
            ],
            scene
        );
    }

    dynamicObjectPhysicsBody.addConstraint(staticObjectPhysicsBody, dynamicJoint);
    pivotStaticObject.position = dynamicJoint.options.pivotB;

```

Im not sure what the scale is for stiffness/damping, but I often have to set them to large numbers to see the effect I want.

Here is an example on a forked playground: [https://playground.babylonjs.com/#RMF5CJ#23](https://playground.babylonjs.com/#RMF5CJ#23)
