简体   繁体   中英

Webgl three.js Dependency of several objects

I have a problem with the dependency of objects. I want to make one object dependent on two other objects. If I change the position of one parent object (for example the y-Position), the dependent object (the child object) should rotate und should also move.

Here is a link where I realized it on a picture. On the left side is the initial state, on the right side the changed condition. The Cylinder should be dependent on the two boxes. That means that the cylinder is the child object and the boxes should be both parent objects of this child object. I tried it with using parent and child properties, but I could not make the child object dependent on two parent objects.

Can someone help me?

在此处输入图片说明

This is my current code, but the lookAt Function does not work correctly.

    //cube 1
    var cube=new THREE.BoxGeometry(4,4,4);
    var material=new THREE.MeshBasicMaterial ({ color: 0xffffff });
    mesh1=new THREE.Mesh(cube,material);
    mesh1.position.set(-2,2,0);
    scene.add(mesh1);

    //cube 2
    var cube2=new THREE.BoxGeometry(2,2,2);
    var material=new THREE.MeshBasicMaterial ({ color:0x000000 });
    mesh2=new THREE.Mesh(cube2,material);
    mesh2.position.x=6;
    mesh2.position.y=2;
    //mesh2.position.y=-2;
    scene.add(mesh2);   

    //cylinder
    var cylinder=new THREE.CylinderGeometry(1,1,6,30);
    var material=new THREE.MeshBasicMaterial({ color:0xff3399 });
    mesh3=new THREE.Mesh(cylinder,material);
    mesh1.add(mesh3);
    mesh3.lookAt(mesh2.position);

You need to two levels of hierarchy, eg:

cube1.add(cylinder)
cylinder.add(cube2)

You may be able to do what you're after with the lookAt function on the Object3D class .

eg.

Add your cylinder as a child of cube1 so moving cube1 will also move the cylinder.

cube1.add(cylinder)

then when cube1 (or / and cube2) is moved call the lookAt function on cylinder to look at the cube2's position.

cylinder.lookAt(cube2.position)

Yeah, 2pha is right actually. I think what he actually needs is something like this (note you'll need to scale the cylinder or move the squares unless they are exactly cylinder height apart):

cylinder.position.x=cube1.position.x
cylinder.position.y=cube1.position.y
cylinder.position.z=cube1.position.z
cylinder.lookAt(cube2.position)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM