Havok Spring Joints?

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

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.

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

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.

// 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! :slight_smile:

More input about joints:
Havok set joints positions on Raycast - Questions - Babylon.js (babylonjs.com)

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.

1 Like

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

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

2 Likes

Jiggle jiggle, i like to see you wiggle wiggle

1 Like

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.

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:

    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

4 Likes