Create vectors and collisions

后端 未结 1 633
梦如初夏
梦如初夏 2020-12-19 19:10

I have a ball and a stick (for a billiard game).

First the ball is placed in a position of a table. On clicking the ball the stick appears, in such a way that we ca

相关标签:
1条回答
  • 2020-12-19 19:28

    You calculate the angle of the billiard stick to the ball like this:

    var dx = ballX - stickX;
    var dy = ballY - stickY;
    var angle = Math.atan2(dy,dx);
    

    Then you can move the ball along that angle like this:

    var newBallX = ballX + desiredRollDistance * Math.cos(angle);
    var newBallY = ballY + desiredRollDistance * Math.sin(angle);
    

    Your desired roll distance would be based on how far the stick was drawn back away from the ball.

    The further the stick was drawn back == the further the ball would travel.

    You can calculate the distance from the stick to the ball like this:

    var dx = ballX - stickX;
    var dy = ballY - stickY;
    var lengthFromStickToBall = Math.sqrt(dx*dx+dy*dy);
    

    Here is a Demo: http://jsfiddle.net/m1erickson/B6K9z/

    0 讨论(0)
提交回复
热议问题