I have created a wire on a face and an RsRobTarget at a point P on the wire.
I have created an RsTarget from the RsRobtarget and have set its transform so that it is correctly located at P.
I have calculated the Vector3 normal to the face at P, and the Vector3 tangent to the wire at P.
I would appreciate advice on how to align the target Z axis with the normal and the Y axis with the tangent.
The x/y/z members of Matrix4 correspond to the axes of the coordinate system. So basically you set z = normal, y = tangent, and calculate x so you get a right-handed system.
// Axes must be perpendicular
System.Diagnostics.Debug.Assert(normal.Dot(tangent) == 0);
Vector3 z = normal;
z.Normalize();
Vector3 y = tangent;
y.Normalize();
Vector3 x = y.Cross(z); // Matrix must be right-handed
Matrix4 mat = target.Transform.GlobalMatrix;
mat.UpperLeft = new Matrix3(x, y, z);
target.Transform.GlobalMatrix = mat;
[/code]
That positioned the target at the world origin, so I repositioned it to the selected ‘point’ using:trg.Transform.X = point.x;
trg.Transform.Y = point.y;
trg.Transform.Z = point.z;
The alignment appears not quite perfect. You can see that the Y and Z axes are deviating from the black normal and tangent lines. Do you have any idea why this might be?
Edit: If I create the target at the other end of the face the deviation from the normal is even greater …
Johannes explained via PM that Normals and Tangents have no position, only a direction.
The normal is found using:
Bool = Face.GetNormalToSurface(testPoint, out hitPoint, out hitPointNormal); … where hitPointNormal is a Vector3 position representing the end point of the unit normal with the normal translated so that its start point is at the world origin.
To draw the normal correctly located at hitPoint it is necessary to use: myNormal = Body.CreateLine(hitPoint, hitPoint + hitPointNormal); instead of myNormal = Body.CreateLine(hitPoint, hitPointNormal);