Mathematics help

I have a cylinder model, which has vertically positioned. I would like to change it orientation by providing the two end points (computing from somewhere else).

I try to use the two end points to compute center of the cylinder and the setHPR (actually only P and R). The result is not correct.

        center = (v + vb) / 2
        p = atan((vb[1]-v[1])/(v[2]-vb[2])) / pi * 180
        r = atan((v[0]-vb[0])/(v[2]-vb[2])) / pi * 180
        self.np.setPos(*center)
        self.np.setHpr(0,p,r)

I believe there should be much cleaner/better ways to do it ?

Hmm, if you have two points, can’t you just setPos(point1), then lookAt(point2), and set the length to Vec3(point2-point1).length() ? If the origin is in the center of the cylinder, you might wanna set an offset of 0.5*length.

My 3D maths is bad.

Since the cylinder is standing vertically, I try lookAt the end point and the model got 90 degrees rotated on screen.
I then add a dummy node to hold the model and rotate it by setP(90). It then works as expected. Thank you.

How can I archive the same effect without adding the dummy node ? Actually I think it shall be lookAt the direction of the normal vector of the cylinder’s end points.

To achieve that, use setP(90) first, then call flattenLight.

Works very well now. Thank you for your help.