Hi guys,
I've been struggling to create a function to draw an arrow with 2 lines making up the arrowhead.
The challenge is that I somehow can't get the rotation right.
I've pasted the code below and an image of the result.
Findings so far, when I replace the 3x3 part of the transform matrix to identity, I get the same visual result.
Also when I switch colums A and C in the matrix, this specific arrow looks good (with is pointing in the positive X direction).
Any input would be appreciated.
bool CDebugDraw::AddArrow(const CR_VECTOR3 &pPos1, const CR_VECTOR3 &pPos2, const CR_VECTOR3 &pColor, const bool pDepthEnabled, const bool pPersistent, const uint64_t pLifeTime)
{
if(!AddLine(pPos1, pPos2, pColor, pDepthEnabled, pPersistent, pLifeTime)) return false;
/*
p1 ------- X
/ | \ |
/ | \ |
/ | \ |
p2 -----|-----p3 | Z
|
|
|
|
p0
*/
CR_VECTOR3 arrowDir = pPos2 - pPos1;
arrowDir.Normalize();
// model space
CR_VECTOR3 p1 = CR_VECTOR3(0.0f, 0.0f, 0.0f);
CR_VECTOR3 p2 = CR_VECTOR3(0.2f, 0.0f, -0.2f);
CR_VECTOR3 p3 = CR_VECTOR3(-0.2f, 0.0f, -0.2f);
// transformation: translate and rotate
CR_VECTOR3 transl = pPos2;
CR_VECTOR3 colA = arrowDir;
CR_VECTOR3 tVec;
if(colA.x != 0 && colA.z != 0) tVec = CR_VECTOR3(0.0f, 1.0f, 0.0);
else tVec = CR_VECTOR3(0.0f, 0.0f, 1.0f);
CR_VECTOR3 colB = CMathHelper::CrossVec3(colA, tVec);
CR_VECTOR3 colC = CMathHelper::CrossVec3(colB, colA);
CR_MATRIX4X4 transform;
transform.m11 = colA.x; transform.m12 = colB.x; transform.m13 = colC.x; transform.m14 = 0.0f;
transform.m21 = colA.y; transform.m22 = colB.y; transform.m23 = colC.y; transform.m24 = 0.0f;
transform.m31 = colA.z; transform.m32 = colB.z; transform.m33 = colC.z; transform.m34 = 0.0f;
transform.m41 = transl.x; transform.m42 = transl.y; transform.m43 = transl.z; transform.m44 = 1.0f;
// transform = CMathHelper::ComposeWorldMatrix(transform, CR_VECTOR3(1.0f), CR_VECTOR3(0.0f, 90.0f, 0.0f), pPos2);
// transform to worldspace
p1 = CMathHelper::TransformVec3Coord(p1, transform);
p2 = CMathHelper::TransformVec3Coord(p2, transform);
p3 = CMathHelper::TransformVec3Coord(p3, transform);
if(!AddLine(p1, p2, CR_VECTOR3(1.0f, 0.0f, 0.0f), pDepthEnabled, pPersistent, pLifeTime)) return false;
if(!AddLine(p1, p3, CR_VECTOR3(1.0f, 0.0f, 0.0f), pDepthEnabled, pPersistent, pLifeTime)) return false;
if(!AddCross(p2, 0.02f, CR_VECTOR3(0.0f, 0.0f, 1.0f), pDepthEnabled, pPersistent, pLifeTime)) return false;
if(!AddCross(p3, 0.02f, CR_VECTOR3(0.0f, 0.0f, 1.0f), pDepthEnabled, pPersistent, pLifeTime)) return false;
return true;
}
Incorrect result:
Aimed/ correct result (independent of arrow direction):