• 11
• 9
• 10
• 9
• 10

# Bullet Hole Billboard

This topic is 1827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

When I do raycasting, I can get the intersected triangle and the hit point, I want to place a bullet hole on the hit point.

How do I calculate the billboard position from the hit point and intersected triangle?

##### Share on other sites

If you have the surface normal from the triangle, and the location to put the bullet hole, then you can simply construct the four vertices needed for your billboard.  The surface normal is always perpendicular to the triangle face, so you just need to find any vector that is 90 degrees away from the normal.  Once you have that vector, you can then use it to offset into the four directions around the hit point and place vertices in each corner.

Finding that first vector can be accomplished a number of ways.  You can literally rotate the normal vector, or you can assume an up direction (i.e. [0,1,0]) and take the cross product of the normal and the up vector to find another vector that is perpendicular to both of the input vectors.

##### Share on other sites

I'm not sure how I should start, also I'm not really good in mathematics.

Here is what I got:

D3DXVECTOR3 triangleV1, triangleV2, triangleV3;
D3DXVECTOR3 intersectedPoint;


Let me know EXACTLY how do I get:

D3DXVECTOR3 bulletBillboardV1, bulletBillboardV2, bulletBillboardV3, bulletBillboardV4;

##### Share on other sites

I am not good at math either, but try this:

D3DXVECTOR3 GetTriNormal(const D3DXVECTOR3& v0, const D3DXVECTOR3& v1, const D3DXVECTOR3& v2)
{
D3DXVECTOR3 edge1 = v1 - v0;
D3DXVECTOR3 edge2 = v2 - v0;
D3DXVECTOR3 normal;
D3DXVec3Cross(&normal, &edge1, &edge2);
return normal;
}
...
D3DXVECTOR3 up = triangleV1 - intersectedPoint;
D3DXVECTOR3 nrm = GetTriNormal(triangleV1, triangleV2, triangleV13);
D3DXVECTOR3 right;
D3DXVec3Cross(&right, &nrm, &up);
D3DXVec3Normalize(&up, &up);
D3DXVec3Normalize(&nrm, &nrm);
D3DXVec3Normalize(&right, &right);
...
float size = 0.3f;
D3DXVECTOR3 horizontal = right * size;
D3DXVECTOR3 vertical = up * size;
bulletBillboardV1 = intersectedPoint - horizontal + vertical; // up-left
bulletBillboardV2 = intersectedPoint + horizontal + vertical; // up-right
bulletBillboardV3 = intersectedPoint + horizontal - vertical; // down-right
bulletBillboardV4 = intersectedPoint - horizontal - vertical; // down-left


no guarantee that it will work. Maybie someone could correct me if i am wrong.

EDIT: It works actually, just tried it with my old "triangle picking" project. Here on the image i just draw lines around "billboards":

If it doesn't show for you or triangles are missing, make sure you got your winding/indices right.

Edited by belfegor

##### Share on other sites

@belfegor: It works, however, I have a problem when I shoot another side of a box:

[attachment=15008:wr.png]

##### Share on other sites

How do you obtain your triangle points?

##### Share on other sites

For testing, I tried to draw 3 spheres on the triangle points, it works very well with the terrain, but it doesn't work correctly with the box, I get points in a completely different position.

Here is how I get the triangle:

D3DXIntersect(mesh, &rayFrom, &rayDir, &hit, &faceIndex, &pU, &pV, &distance, NULL, NULL);
if (hit)
{
D3DXVECTOR3 V1 = pVertices[pIndices[3 * faceIndex + 0]].p;
D3DXVECTOR3 V2 = pVertices[pIndices[3 * faceIndex+ 1]].p;
D3DXVECTOR3 V3 = pVertices[pIndices[3 * faceIndex + 2]].p;

// Code to draw bullet billboard here...
}

##### Share on other sites

That is correct code for triangle points, but i don't know why do you get them at different position then expected.

##### Share on other sites

Let me show you more code, maybe you could spot something wrong:

mesh->LockVertexBuffer(D3DLOCK_READONLY, ( void** )&pVertices);

D3DXMATRIX matWorld = GetMeshWorldMatrix();
D3DXMATRIX matInverse;
D3DXMatrixInverse(&matInverse, NULL, &matWorld);

D3DXVECTOR3 rayFrom, rayDir;
D3DXVec3TransformCoord(&rayFrom, &bRayFrom, &matInverse);
D3DXVec3TransformNormal(&rayDir, &bRayTo, &matInverse);

D3DXIntersect(mesh, &rayFrom, &rayDir, &hit, &faceIndex, &pU, &pV, &distance, NULL, NULL);
if (hit)
{
D3DXVECTOR3 V1 = pVertices[pIndices[3 * faceIndex + 0]].p;
D3DXVECTOR3 V2 = pVertices[pIndices[3 * faceIndex+ 1]].p;
D3DXVECTOR3 V3 = pVertices[pIndices[3 * faceIndex + 2]].p;

// Code to draw bullet billboard here...
}