Results 1 to 2 of 2

Thread: how to detect collision in qpainterpath

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to detect collision in qpainterpath

    hi friends/experts,
    I have created a free draw on qgraphicsscene using a qgraphicsitem with QPainterpath. i can able to draw or mark anything on the scene using mousepress and mouseMove.
    but how i can detect if the painterpath the user drawn crossed with the starting or any part of the path the user started..?
    because if the painterpath is not completed by joining the starting point to the end point i have to draw a line to complete .i did it . but if user already crossed or joined the path at some point in the path , i have to clear the extra path lines.
    so i want to detect how i can come to know if the path already collided with the path drawn .collision.png

    i have marked the collision area in red circle .. please help me ..
    "Behind every great fortune lies a crime" - Balzac

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to detect collision in qpainterpath

    First thing I can think of is to keep track of bounding rectangles of the path's segments, like on this poor reference image I made
    path.jpg
    Ok so my idea is that rectangle intersection tests are very fast, and it will be more efficient to first test if a part of the path is a good candidate for intersection with another part of the path by testing the bounding rectangles. If bounding rect test fails, you can move on to next part of the path.
    The fact that old parts of your path does not change allows you to optimize the search by testing only the new parts added by user (if two parts of the curve never intersected, they never will, so you can skip the tests).
    Now what to do if you find a good candidate for intersection ?
    intersect.jpg
    You can apply a brute force algorithm that will test each line from green rect vs. each line from blue rect. If they in fact intersects you will find the intersection point this way, eventually.
    Another approach would be to apply further subdivision of the green and blue bounding rects, and first test the sub-rects before doing actual test on the line segments.
    How many line segments should be included in one bounding rectangle ? I don't know Remember that if you include many line segments in one bounding rect, you'll have to test for interseciton inside the bounding rect as well (but again, this is calculated only once).
    Testing the intersection of two line segments is an easy mathematical excercise
    Unfortunately I don't think there is a ready-made solution - maybe you can somehow use the QGraphicsScene::collidingItems method, but it depends on how you store the path data (it can test item vs. item collision). You could also use some existing 2d collision detection library.

  3. The following user says thank you to stampede for this useful post:

    wagmare (4th December 2013)

Similar Threads

  1. Need Help Regarding Qt GraphicsItem Collision
    By RE in forum Qt Programming
    Replies: 4
    Last Post: 28th September 2012, 11:12
  2. QPainterPath::quadTo(...) calls QPainterPath::cubicTo(...) ?
    By brucelamond in forum Qt Programming
    Replies: 0
    Last Post: 28th April 2011, 23:30
  3. collision detection...
    By Muffin in forum Newbie
    Replies: 1
    Last Post: 8th January 2010, 10:28
  4. Collision detect in QGraphicsScene
    By sophister in forum Qt Programming
    Replies: 6
    Last Post: 5th December 2009, 01:38
  5. Collision handling 101
    By Morea in forum Qt Programming
    Replies: 9
    Last Post: 25th November 2006, 20:17

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.