1. Beginner
Join Date
Jun 2009
Posts
8
Thanks
1
Qt products
Platforms

physics simulation

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.

2. Re: physics simulation

Are you using Graphics View?

3. Beginner
Join Date
Jun 2009
Posts
8
Thanks
1
Qt products
Platforms

Re: physics simulation

No, I'm using QPainter.

5. Beginner
Join Date
Jun 2009
Posts
8
Thanks
1
Qt products
Platforms

Re: physics simulation

I might try that for future programming, but rather that starting this project again, do you have any idea how to solve the original problem?

6. Re: physics simulation

You have to calculate everything manually yourself. You can use QLineF and QPointF to calculate intersections but the rest requires pure math skills. That's one of the reasons I'm suggesting GraphicsView.

Join Date
Aug 2008
Location
Algarve, Portugal
Posts
287
Thanks
23
Thanked 31 Times in 27 Posts
Qt products
Platforms

Re: physics simulation

I did a program like that where points would move inside a rectangle, like balls in a pool table. If I correctly understood, you have user draw lines with different angles than the horizontal and vertical lines of the rectangle. ItÂ´s all math like Wysota said, but if you think it can help you, I can try to find my code and show you, but it's old and made in MFC6 with opengl.