# Bullet Hole Billboard

## Recommended Posts

Medo Mex    891

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
Jason Z    6434

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
Medo Mex    891

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
belfegor    2834

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
Medo Mex    891

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

[attachment=15008:wr.png]

##### Share on other sites
belfegor    2834

How do you obtain your triangle points?

##### Share on other sites
Medo Mex    891

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
belfegor    2834

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
Medo Mex    891

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...
}


##### Share on other sites
Medo Mex    891

I notice that the triangles are only valid when the mesh position is XYZ: (0.0f, 0.0f, 0.0f) but when the mesh move to any other position, the results assume that the position is still 0.0f, 0.0f, 0.0f.

Edited by Medo3337

##### Share on other sites
belfegor    2834

Show some more code. How do you obtain bRayFrom & bRayTo?

EDIT: When drawing billboards did you transform them with objects transform with which they are "attached"?

Edited by belfegor

##### Share on other sites
Medo Mex    891

No, it's not attached yet, to attach it, do I need to multiply the mesh transformation matrix with the billboard vertices position? Any example how to?

I get bRayFrom and bRayTo from bullet raycasting.

##### Share on other sites
belfegor    2834

When you click on an object you want billboard to be attached to it, right? Then you need to transform your billboard with that objects transformation matrix, usually you set this up just before you draw your billboard:

// from your code above
D3DXMATRIX matWorld = GetMeshWorldMatrix();// object matrix you have clicked
D3DXMATRIX matInverse; // not this!
D3DXMatrixInverse(&matInverse, NULL, &matWorld);
...
// billboard is now a "child" of that object
d3d9device->SetTransform(D3DTS_WORLD, &matWorld);
DrawBillboard();


You need a way to "remember" which billboard is attached to which object.

##### Share on other sites
Medo Mex    891

As you know, I'm drawing all bullet hole billboards with one single draw call, so the transformation matrix must be set to identity, I want a way to change this:

// Billboard vertices position
D3DXMATRIX matWorld = GetMeshWorldMatrix();
pVertices[j*4+0] = CUSTOM_VERTEX(billboard[i].Q1, 0.0f, 0.0f);
pVertices[j*4+1] = CUSTOM_VERTEX(billboard[i].Q2, 0.0f, 1.0f);
pVertices[j*4+2] = CUSTOM_VERTEX(billboard[i].Q3, 1.0f, 1.0f);
pVertices[j*4+3] = CUSTOM_VERTEX(billboard[i].Q4, 1.0f, 0.0f);

I want to make the calculation of the billboard position with respect to (matWorld).

##### Share on other sites
belfegor    2834

Then you transform billboard vertices at a time you place them on object:

D3DXVec3TransformCoord(&bulletBillboardV1, &bulletBillboardV1, &matWorld);
D3DXVec3TransformCoord(&bulletBillboardV2, &bulletBillboardV2, &matWorld);
D3DXVec3TransformCoord(&bulletBillboardV3, &bulletBillboardV3, &matWorld);
D3DXVec3TransformCoord(&bulletBillboardV4, &bulletBillboardV4, &matWorld);


##### Share on other sites
Medo Mex    891

That should attach the billboard to the model, the problem is now with the intersected triangle, I'm only getting a valid intersected triangle if the mesh position is: 0.0f, 0.0f, 0.0f.

##### Share on other sites
belfegor    2834

That should attach the billboard to the model, the problem is now with the intersected triangle...

It would be a lot easier to help you if you could attach your whole project for us to examine?

Then you might want to consider different approach like instancing (if you want 1 draw call), but you'll then need to learn how to use shaders (as i know instancing is not possible in "fixed function pipeline"). You could store in vertex declaration quaternion (float4) for rotation and position + uniform scale scalar(float4) to build a transfom matrix in vertex shader. Then you just lock->write-unlock for "billboards" that is attached to objects that might move in the scene. With this approach you might want to consider to specialize 2 classes, one for static (no need to lock->write->unlock) and other for dynamic objects.

##### Share on other sites
Medo Mex    891

@belfegor: The current problem is with getting a valid intersected triangle, not the billboards.

The intersected triangle that I get ASSUME that the model is located in XYZ: 0.0f, 0.0f, 0.0f

When I use D3DXIntersect() to get the intersected triangle, the result is only valid if the model position is: 0.0f, 0.0f, 0.0f

I tried:

// Transform intersected triangle v0, v1, v2 according to the model location in the world
D3DXVec3TransformCoord(&v0, &v0, &matWorld);
D3DXVec3TransformCoord(&v1, &v1, &matWorld);
D3DXVec3TransformCoord(&v1, &v1, &matWorld);


But it's not working, I need to find a way to make v0, v1, v2 results according to the model transformation.

##### Share on other sites
belfegor    2834

You have to show some more code!

How do you visualise triangle that you have picked? Maybie the problem is elsewhere.

##### Share on other sites
unbird    8336
D3DXVec3TransformNormal(&rayDir, &bRayTo, &matInverse);

Hmmm, did Postie et.al. here not already point out that or a similar problem ?

##### Share on other sites
Medo Mex    891

@unbird: It's a similar problem, but here I'm giving better details on what is happening exactly.

I think I need to transform the intersected triangle v0, v1, v2 with the model world matrix, but not sure how it should be done.