Here's a code that does exactly what you're looking for... but it's
for another project:
https://github.com/philogb/jit/blob/master/Source/Geometry/O3D.js#L223
The main idea is to create a "normalized" cylinder (that would have,
say 1 of height and 0.5 or radius) and then apply the scaling,
rotation and translation based on the two points.
The scaling of the cylinder will be based on the distance between the
two points.
The translation of the cylinder will be to the middle point of the
line connecting the two points.
The rotation of the cylinder is the tricky part, and it's based on a
direction vector, and a rotation angle.
You can find how the vector and angle are found here:
https://github.com/philogb/jit/blob/master/Source/Geometry/O3D.js#L223
Then you can create the rotation matrix by using the method rotateAxis:
http://senchalabs.github.com/philogl/doc/math.html#Mat4:rotateAxis
Finally you set the rotation matrix, and apply the scaling and
translation and don't forget to update the object.
I hope this helps, let me know if you have any further questions.
--
Nicolas Garcia Belmonte - http://philogb.github.com/
The updateEdge method creates that same rotation matrix. In some sort
of non-tested PhiloGL pseudo-code the code to set the position,
scaling and rotation of the object would be:
//the two points where you want to align the cylinder
var p1 = new PhiloGL.Vec3(x1, y1, z1),
p2 = new PhiloGL.Vec3(x2, y2, z2),
//distance between the two points
dist = p1.distTo(p2),
//current direction of the cylinder (facing up)
currentDir = new PhiloGL.Vec3(0, 1, 0),
//middle point
mp = p1.add(p2).$scale(0.5),
//direction vector from p1 to p2
dv = p2.sub(p1).$unit();
//now create parameters to fill the rotation matrix
var c = dv.dot(currentDir),
rotAngle = Math.acos(c),
rotAxis = currentDir.$cross(dv).$unit();
//now set rotation translation and scaling to the model
var cylinderMatrix = cylinder.matrix;
//clear the matrix
cylinderMatrix.id();
//translate to the middle point
cylinderMatrix.$translate(mp.x, mp.y, mp.z);
//scale to the distance of the two points
cylinderMatrix.$scale(1, dist, 1);
//rotate around an angle and an axis
cylinderMatrix.$rotateAxis(rotAngle, rotAxis);
This should do the trick. Remember to have cylinders of length 1. Let
me know how it goes.
var rot = new Matrix4();
rot.n11 = t * x * x + c;
rot.n12 = t * x * y - s * z;
rot.n13 = t * x * z + s * y;
rot.n21 = t * x * y + s * z;
rot.n22 = t * y * y + c;
rot.n23 = t * y * z - s * x;
rot.n31 = t * x * z - s * y;
rot.n32 = t * y * z + s * x;
rot.n33 = t * z * z + c;
--
Do not call update on the model, you're already updating the matrix manually by doing what I wrote before :)
https://github.com/metamolecular/webgl-chem/blob/master/lessons/lesson-4/view.js#L129
that line
--
--