Connecting Entities


#1

I am hoping to connect two spheres by linking them together with a line. I thought I might be able to add a line entity when one sphere collides with another. I have attached the script (that includes the collision linking code) to the sphere entity by defining the script property in the script that creates the sphere. Here is the code for the “bubble”/sphere that is created when running the script.

// Create a bubble
function addBubble() {
var height = Vec3.sum(MyAvatar.getEyePosition(), Quat.getFront(Camera.getOrientation()));
Vec3.print(“height”, height);
var properties = {
type: “Sphere”,
position: height,
dynamic: true,
collisionless: true,
script: Script.resolvePath(“changeColorOnClickDown.js”)
};
var bubble = Entities.addEntity(properties);
}

addBubble();

Here is the code I have written using some of the code from a collision script on the hifi github page.

(function(){
var linkProperties = {
type: “Line”
};
this.collisionWithEntity = function(myID, otherID, collisionInfo) {
Entities.addEntity(linkProperties);
};
})

Is there a better way to try and link them? I am also wondering if it is possible to attach text to a sphere or attach a text object to a sphere. Any help would be greatly appreciated! :slight_smile:


#2

You may have issues with connecting the two spheres as the start and end of the line isn’t defined, also the type is PolyLine not line :slight_smile: . Forexample pasting this in the console

Entities.addEntity(
{
    type: "PolyLine", 
    position: MyAvatar.position, 
    dimensions: {x:10,y:10,z:10},
    linePoints: [{x:0,y:0,z:0}, {x:0,y:2,z:0}], 
    normals: [{x:1,y:0,z:0},{x:1,y:0,z:0}],
    strokeWidths: [.25,.25]
} 
 )

Will create a line from your avatar, upto a point 2 meters up in the air.
Do Note that PolyLines linePoints are relative to the position it was created

Note: PolyLine entities are quite overly complex to manage and mostly made for stuff like drawing lines on a whiteboard. But can be used to make some advanced ones later.

If you want to consistantly keep the line drawn between the two points, youll have to put it on an update thread and keep updating the line entity and the collisionPoint. But with PolyLines it is very, very annoying as you have to constantly do relative positions of the points and keep updating the list. This is why I have a helper function (I was just happened to be working on something on the worklist, but uses just a cube entity as a line instead of the PolyLine.

Here is the meat of it without the fat:

var MAX_DISTANCE = 500;
// ropeID is the entityId of the rope, fromPosition is from where, and toPosition to where.
function connectRope(ropeId, fromPosition, toPosition) {
        var ropeObject = Entities.getEntityProperties(ropeId, ["dimensions"]);

        var rotation = Quat.multiply(Quat.lookAt(toPosition, fromPosition, Vec3.UP), {
                w: 0.707,
                z: 0,
                x: 0.707,
                y: 0
            });
            var distance = Vec3.distance(fromPosition, toPosition);
            if (distance >= MAX_DISTANCE) {
              Entities.deleteEntity(ropeId); 
              return;
            }

            var scale = ropeObject.dimensions;
            scale.y = distance;
            var between = Vec3.mix(fromPosition, toPosition, 0.5);

            Entities.editEntity(ropeId, {
                dimensions: scale,
                rotation: rotation,
                position: between
            });

        }

       // Create simple entity
       var rope = Entities.addEntity({
              type: "Cube", 
              name: "Rope",
              collisionless: true,
              dimensions: {x:0.25,y:0.25,z:0.25},
              position: MyAvatar.position
       });

       // Connects the entity with the id of rope, from MyAvatar position to the origin of the domain (0,0,0)
       connectRope(rope, MyAvatar.position, {x:0,y:0,z:0});

You can use your collisionInfo variable, which should have alot of variables available to it, forexample collisionInfo.contactPoint gives you the position where the collision happened. You can use the function inside an update loop as well.

If you want to parent the objects together, you make to make sure your set the parentUUID to the ID of the parent defined as the id of the bubble, but this may make some stuff difficult (such as relative rotations and the sort)… This can also be done in the Edit window without any scripts :slight_smile: But then the above may not work as intended if the parent begins to rotate…

Hopefully that helps :slight_smile:
Happy scripting.