Hi guys.
I've reached a stage in my project in which I can afford to spend some time working on effects and a little eye candy. Right now I want to add decals (bullet holes) at collision points between the bullet and the wall. I'm still at work, so I can't code it, but I came out with an approach on how to do it. I still have 6 hours left till I go home, so I wanted to ask you guys what approach you use when it comes to using decals. What I plan on doing is this:
Retrieve our object’s width and height.
Obj1.Width = 40; //dbGetObjectWidth(..);
Obj1.Height = 40; //dbGetObjectHeight(..);
Get texture width, height.
Texture1.Width = 1024; //dbGetImageWidth(..);
Texture1.Height = 1024; //dbGetImageHeight(..);
Get our Obj/Texture ratio:
DecalPos.X = Obj1.Width/Texture1.Width;
DecalsPos.Y = Obj1.Height/Texture1.Height;
Now we check out where the bullet collided with the object
PaintAt.PosX = Bullet.getcollisionX()
PaintAt.PosY = Bullet.getcollisionY()
PaintAt.PosZ = Bullet.getcollisionZ()
We know at what position the bullet collided with the wall object. Now let’s check where on the object it hit. We can do this by checking the wall’s position, size, with the bullet’s position and size.
Obj1.PosX = wallGetPosX();
Obj1.PosY = wallGetPosY();
Obj1.PosZ = wallGetPosZ();
We then substract the Objects Position from the Decal position to know the offset of the decal on that object.
Now everything is just a matter of converting those position to the texture coordinates.
TexturePosX = PaintAt.PosX / 1024 [1 px]
TexturePosY = PaintAtPosZ / 1024 [1 px]
TexturPosZ = 0; // No depth in 2D;
Now we offset the decal image to the right position on the wall.
FinDecalPosX = TexturePosX + (PaitAtPosX – Obj1.PosX)
FinDecalPosY = TexturePosY + (PaintAtPosY - Obj1.PosY)
PaintDecal();
What do you guys think? Do you see a flaw in my logic? Any idea if there is a better approach to it?
P.S. when I prototyped the game, I would create a plane object, with the bullet_hole texture at the collision point. But I had two problems: The rotation of the plane, which was set to the bullet's rotation on impact with the wall, and that if the bullet collided with a corner, half the decal would just "float", being offseted outside the wall.
Looking forward to your suggestions.