I am trying to make a basic physics simulator, where 2 2d balls move around the screen and are controlled by various forces. One of the features I am currently developing is where the user can draw lines which the balls can bounce off of.
I have a variables for the coordinates of the points at either end of the line, and the coordinates for the ball (which has a diameter of 20). I also have two variables (which can be positive or negative) to show the vector that the balls are currently moving in.
I need a way to detect if the ball is touching the line, and to change it's vector so it bounces off. In the simulation I am assuming the balls are not spinning and no other forces are applied at collision, so the bounce off at the opposite angle that they hit the line (like a ray of light on a mirror).
I have tried working it out with trigonometry, but it does not work for all the directions, and the ball just moves towards (0, 0).
Any help is appreciated.


Reply With Quote


Bookmarks