PDA

View Full Version : physics simulation



scrasun
7th June 2009, 10:27
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.

wysota
7th June 2009, 10:35
Are you using Graphics View?

scrasun
7th June 2009, 10:46
No, I'm using QPainter.

wysota
7th June 2009, 12:49
Think about using GraphicsView. It would help you greatly with your task. You'd operate on objects instead of mathematical points. And you'd have fast collision detection embedded into the architecture.

scrasun
7th June 2009, 20:56
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?

wysota
7th June 2009, 21:31
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.

john_god
11th June 2009, 02:19
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.