I am currently trying to implement a server authoritative tank game and i have issues with server side hit detection
in the code below i register an action with action manager so when a projectile intersects with one of the tanks from my tanks dictionary it should (for now) print “hit” to console
but i never get the hit in console but i know the projectile should hit since i run the exact same (same code and also babylon with cannon) physics simulation on the client and there the projectile goes through the other tank
if you had a similar issue or have any ideas on how i can solve this please help
import {tanks} from "../MyRoom";
export class Projectile {
constructor(scene, position, direction, velocity, damage) {
this.scene = scene;
this.velocity = velocity;
// Create a box instead of a sphere and set its size to create a long rectangle
this.mesh = BABYLON.MeshBuilder.CreateBox("projectile", {width: 0.5, height: 0.5, depth: 8}, this.scene);
this.mesh.position = position;
// Create a yellow material and assign it to the box
var material = new BABYLON.StandardMaterial("mat", this.scene);
material.diffuseColor = new BABYLON.Color3(1, 1, 0); // RGB values of yellow
this.mesh.material = material;
// Align the box with the direction vector
var forward = new BABYLON.Vector3(0,0,1);
var directionToLookAt = direction;
var angle = Math.acos(BABYLON.Vector3.Dot(forward, directionToLookAt));
// Correct the angle
if (directionToLookAt.x < 0) angle *= -1;
this.mesh.rotation.y = angle;
// Physics settings
this.mesh.physicsImpostor = new BABYLON.PhysicsImpostor(
this.mesh,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 1, restitution: 0.9 },
this.scene
);
// Apply the impulse in the given direction
this.mesh.physicsImpostor.applyImpulse(
direction.scale(this.velocity),
this.mesh.getAbsolutePosition()
);
this.mesh.actionManager = new BABYLON.ActionManager(this.scene);
for (let tankId in tanks) {
let tank = tanks[tankId];
if(tank){
[tank.hullMesh, tank.turretMesh].forEach(mesh => {
this.mesh.actionManager.registerAction(
new BABYLON.ExecuteCodeAction(
{
trigger: BABYLON.ActionManager.OnIntersectionEnterTrigger,
parameter: mesh
},
() => {
console.log("hit")
this.destroy();
}
)
);
});
}
}
setTimeout(() => {
console.log("destroy")
this.destroy();
}, 1000);
}
destroy() {
// Remove the mesh from the scene
this.mesh.dispose();
}
}