Jump to content

How to create and remove joints by script?


photo

Recommended Posts

6 hours ago, Sevdat said:

Good evening,

How can I create and remove joints by code? If I have 2 primitives how do I create a joint and then stop them from being a joint? I read the documentation, but I couldn't understand how to code it: https://developer.unigine.com/en/docs/future/api/library/physics/class.joint?rlang=cs&autotranslate=en#createJoint_int_Joint

Hello!

Have you had the opportunity to play with the "Joints" samples provided with the SDK? You can access them through the SDK Browser by following this path: "Samples -> UnigineScript -> Joints". These samples are made in UnigineScript but examining them could help you understand how it can be done through the code.

Thanks!

Link to comment
Posted (edited)
4 hours ago, bmyagkov said:

Hello!

Have you had the opportunity to play with the "Joints" samples provided with the SDK? You can access them through the SDK Browser by following this path: "Samples -> UnigineScript -> Joints". These samples are made in UnigineScript but examining them could help you understand how it can be done through the code.

Thanks!

Good day bmyagkov,

First time looked into it now. The codes are C++, but that's not the issue.:

image.png.a3081b87f3eab361def1c11ea56adbff.png

I still can't find how the joint connections can be added and removed later.

Edited by Sevdat
Link to comment

Good day,

I think I figured it out:

	ObjectMeshDynamic clone;
    ObjectMeshDynamic clone2;
    Joint connectedJoint;

    public ObjectMeshDynamic createSphere(float radius,vec3 position,int i){
        ObjectMeshDynamic sphere = Primitives.CreateSphere(radius, 9, 32);
        sphere.TriggerInteractionEnabled = true;
        sphere.SetIntersection(true, 0);
        sphere.SetIntersectionMask(1, 0);
        sphere.SetCollision(true,0);
        sphere.SetCollisionMask(1, 0);
        sphere.WorldPosition = position;
        sphere.Name = $"{i}";
        BodyRigid bodySphere = new BodyRigid(sphere);
        new ShapeSphere(bodySphere, 2);
        bodySphere.ShapeBased = false;
        bodySphere.Mass = 1f;
        return sphere;
    }
    public JointBall createJoint(
        ObjectMeshDynamic object1, ObjectMeshDynamic object2, vec3 vec
        ){
            return new JointBall(object1.Body, object2.Body, vec);
    }
    public void deleteJoint(Joint joint){
        joint.Broken = true;
        joint.Enabled = false;
    }

    private void Init(){
        Visualizer.Enabled = true;
        Unigine.Console.Onscreen = true;
        Physics.ShowShapes = Physics.SHOW_TYPE.SOLID;
        Physics.ShowJoints = true;
        Physics.ShowCollisionSurfaces = true;
        vec3 pos = new vec3(5,10,50);
        clone = createSphere(1f,pos,1);
        clone2 = createSphere(1f,new vec3(15,10,40),1);
        connectedJoint = createJoint(clone, clone2, pos);
    }
    float time;
    private void Update(){
        time += Engine.IFps;
        if (time > 10f){
            deleteJoint(connectedJoint);
        }
    }

These 2 booleans are responsible of deleting the joint connection from the way I understood. Is there something extra that must be added or is this the correct approach?

        joint.Broken = true;
        joint.Enabled = false;

 

Link to comment

Hello!

This approach seems to be completely fine and the key trick was contained in the following lines:

Quote

new JointBall(object1.Body, object2.Body, vec);

Thanks!

  • Like 1
Link to comment
×
×
  • Create New...