Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
First of all the game is quite nice, but there's a simple method with which you can win every level pretty fast, which kind of kills the fun as it is now.
How can I tell whether the circular sprit intersects with an arbitrary line.

I don't know how familiar you are with linear algebra, it really helps ;-)

(Update: When I talk about "points", I really mean "position vectors to points". Thanks FunkyMonk)

I'll call the end points of your line A and B, the center of your sprite M, the point on the line which is the closest to M is called C (all those are vectors), the radius of the sprite is r (a scalar value).

Since C is on the line between A and B, it must be expressible in the form A + x * (B-A). Since it's the closest point to M, the connection line from C to M is perpendicular to the original line, or as a formula:

(B-A) . (M-C) = 0 or (B-A) . (M - A - x * (B-A)) = 0 # (that's a scalar product)

The only unknown part in this equation is x, which you can calculate from that equation, but to which I'm too tired at the moment.

If 0 <= x <= 1 and |M - C| < r, then it circle touches or intersects the line.

Likewise if |M-A| <= r or |M-B| <= r it also touches or intersects. In all other case it doesn't.

To get a realistic bouncing behaviour, the angle between (dx, dy) and (M-C) after the bounce has to be negative of what it was before the bounce.


In reply to Re: Some simple 2d game-related maths questions by moritz
in thread Some simple 2d game-related maths questions by skx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (9)
    As of 2014-12-29 10:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (185 votes), past polls