I need to shoot a ball with a curve effect.
I use AmmoJS.
To shoot the ball I do:
let localRefPoint= new BABYLON.Vector3(10, 0, 0);
ball.physicsImposter.applyImpulse(
ImpulseVector,
ball.getAbsolutePosition().add(localRefPoint)
);
The localRefPoint causes the ball to curve, but only when it touches the ground while moving. When the ball is shot into the air (with a y-vector of 0.5 in the ImpulseVector), it doesn’t curve.
Also, I need to remove the impulse somehow when the ball is in the air or when it touches another object, because otherwise strange things happen. The curving will go on, after touching the ground or another object, which looks strange.
I understand. I’m just getting started with 3D, so I don’t have a playground ready yet.
I found out that I can use applyImpulse multiple times though. As long as the ball is in the air, I could apply small impulses with a perpendicular direction as the ball is flying, to curve it a bit.
I’m try to implement this using scene.registerAfterRender:
scene.registerAfterRender(function () {
if (ballmoving) {
let impulsevector = new BABYLON.Vector3(-0.1, 0, 0);
ball.physicsImpostor.applyImpulse(impulsevector , ball.getAbsolutePosition());
}
});
and having some success .(Of course, It’s only curving to the left right now, an only when I shoot straight ahead but you get the idea). I find it difficult to calculate for how long the ball is flying, so when to set the global variable ballmoving to false again. This should happen when the ball hits anything, but I can’t find out how to create one collider event for all objects in the scene.
I also experimented with using a timeout to do this, but again, it’s hard to calculate the time that the ball is in mid air.
The playground is really simple to use, there’s no need to have prior 3D experience Playground | Babylon.js Documentation (babylonjs.com) and it already comes with debugger and everything included to make the experience easier