Hello I am trying to make a paintball game inside my game. I have a Dynamic Texture in addition to a Shader painting in pink color when I shot a ball. But the pink circle not appears in the correct place of the texture. I am also using physics so my point of collision is on the physiscs event: (Sorry I don’t have
a playground, is a large project)
bullet.physicsImpostor.registerOnPhysicsCollide(colliders, (collider, collided, point) => {
so I get the point that is the world point and I project to the mesh:
// Step 1: Project the world coordinates onto the mesh
var projectedPoint = BABYLON.Vector3.Project(
point,
meshCollided.getWorldMatrix(), //world matrix
scene.getTransformMatrix(), //transform matrix
scene.activeCamera.viewport,//viewport
);
with the projected point I get the UV coordinates:
// Step 2: Get the UV coordinates from the mesh
var pickedInfo = scene.pick(projectedPoint.x, projectedPoint.y);
if (pickedInfo.hit) {
var uv = pickedInfo.getTextureCoordinates();
and with that UV coordinates I paint in the texture a Pink circle:
paintContext.arc(uv.x * paintTexture.getSize().width, (1 - uv.y) * paintTexture.getSize().height, 10, 0, 2 * Math.PI);
Something is wrong because when the ball hits the ground just the center of the grass is painted and when I move the camera also changes the collision painted point that shouldn’t. (see the image)
What is wrong with this code? and about this approach? Why the hit point changes when the camera changes? I think somethings wrong with the projection coordinates but I am not very good with matrices andI don’t know how to solve it. And something is wrong with the scale of painted texture too, because I have edited in debug mode and I just can paint the center of the grass field, now is 512x512 maybe make it greater?. Any suggestion is welcome. Thank you.