Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    Distance Constraint between 2 Physics Objects

    3D Blocks
    1
    1
    239
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

      Introduction

      When you have enabled the physics engine in a scene, you can add constraints between objects. A constraint is a request that we give to the physics engine, and the physics engine will try its best to satisfy our request.

      For example, a distance constraint is a request that we wish to force the distance between 2 objects to stay the same no matter how they move.

      Note that the physics engine may not always be able to satisfy our constraint requests, so you need to test our program to make sure your constraint requests are realistic.

       
       

      The “Add Distance Constraint” Block

      To add a distance constraint, you can use the following block:

      86204618-755f-4190-9690-2d6b38cbba5e-image.png

       
      The first 2 inputs allow you to specify the first object from any sprite, and the next 2 inputs allow you to specify a second object from any sprite. Note that if one of these 2 objects is static (mass = 0), then you should specify that object’s name and sprite in the first 2 inputs.

      The last input is optional: it is the name of the constraint you are creating. If you need to remove this constraint later, then you need to refer to this constraint using its name.

      After this block runs, the physics engine will try its best to keep these 2 objects at the same distance as before this block runs. These objects can still move, but their distance will stay the same.

       
       

      Example

      In this program, we add 2 spheres A and B. Sphere A does not move, while sphere B can move freely. Sphere B is added at a distance of 200 to the right of sphere A, so after we add the distance constraint, their distance will remain at 200, but sphere B will be swinging around A due to gravity.

      5060cbf6-f2e4-4d85-82f9-39d24e2bcbf3-image.png

       
      Here is how it looks:

      r3.gif

       
       

      The “Remove Constraint” Block

      You can remove a constraint at any time using the following block:

      971d6711-1ced-4d83-bd81-b310d8b811be-image.png

       
      Note that this block requires you to input the name of the constraint to be removed.

       
       

      Demo

      Suppose we are running the example program above. If we click the “remove constraint” block while sphere B is swinging, then sphere B will start to fall freely:

      1 Reply Last reply Reply Quote 0
      • First post
        Last post