Results 1 to 7 of 7

Thread: physics simulation

  1. #1
    Join Date
    Jun 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question 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. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: physics simulation

    Are you using Graphics View?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: physics simulation

    No, I'm using QPainter.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: physics simulation

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default 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.

Similar Threads

  1. QTimer or recursive calls for simulation?
    By Morea in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2006, 01:19

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.