SceneKit Object between two points

前端 未结 2 1640
不知归路
不知归路 2021-02-11 09:58

Given 2 points in 3D (a,b) and an SCNCapsule, SCNTorus, SCNTube etc., how to go about to position the object, so that the object starts at poin

2条回答
  •  春和景丽
    2021-02-11 10:24

    There is no easy way to do it, but it should not be that hard either. You should implement an algorithm that go through the following steps:

    • Get A & B's position as SCNVector3
    • Figure out the distance, the middle point and the angle between those vector points (it's basic vector math, you can find many code samples online)
    • Create whichever shape you want, with the following size: (assume d is the previously found distance).
      • For SCNCapsule and SCNTube, set height to the distance
      • For SCNTorus, set ringRadius to d/2-pipeRadius
      • For SCNBox, set any side to d
    • Move that shape to the middle-point you found earlier
    • Rotate the shape using the angle between the points (you might have to adjust the axis)

    Once you do that, the shape you created should meet the points at both ends. You can check this is correct using a bounding box, making sure the right axis is equal to the distance.

    I have no code sample, but give it a try and if it doesn't work I can debug it for you.

提交回复
热议问题