I am trying to code a snooker game in JavaScript with only using the libraries: p5.min.js, p5.sound.min.js and matter.js.
Here is my code:
function mousePressed() {
var force = 1000;
var forceX = (cueBall.position.x-mouseX)/force;
var forceY = (cueBall.position.y- mouseY)/force;
var magnitude = Math.sqrt(forceX * forceX + forceY * forceY);
if (magnitude > 1) {
var scale = 1 / magnitude;
forceX *= scale;
forceY *= scale;
}
var appliedForce = {x:forceX,y:forceY};
print(forceX,forceY);
Body.applyForce(cueBall,{x:cueBall.position.x,y:cueBall.position.y},appliedForce);
}
This is to meant to apply a force on the cue ball by clicking on the mouse.
I am wondering if there a better way to apply the force and also limit the force on the cue ball?