• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Bullet Hole Billboard

20 posts in this topic

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?

0

Share this post


Link to post
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.

0

Share this post


Link to post
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;
0

Share this post


Link to post
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":

 

trianglepick-2013-04-19-.png

 

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

Edited by belfegor
1

Share this post


Link to post
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...
}
0

Share this post


Link to post
Share on other sites

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

 

 

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

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

 

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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);
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0