Results 1 to 10 of 21

Thread: Circle vs pie segment collision detection

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Circle vs pie segment collision detection?

    Hi all,
    I have added to The Probe a new element, a Sentry object (see figure 1). This object sweeps a beam (shaped like a pie segment) around itself (see figure 2), and I now want to add collision detection so that I know when the beam is touching the player.

    Figure 1
    sentry.jpg

    Figure 2
    circle vs pie segment collision.png

    I know the beam center point, the radius, and the width (angle).

    I know the player center point, the radius.

    Any ideas as to how I can quickly return true if the beam is over the circle representing the player?

    cheers,
    Paul
    Last edited by paul_nicholls; 26-10-2010 at 11:00 PM.

  2. #2
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25
    Well for a circle, if you know the centre, just use a simple distance calculation to determine if it's inside it. If you need only a 'slice' (or beam as you mentioned) to be detected, get the two angles of the line segments from centre and determine if it's within those.

    I don't have my AdvancedMath.pas unit handy or I'd throw up the function for you.

    For your solution you'd need to compare distance then determine the angle of the object to your sentry object's beam emitter. Once you have all of that information, just use the angles of your beam arcs and the rest is comparison. Just be 0/360 conscious when doing your angle range checks.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  3. #3
    I am already comparing the distance between the circle and the beam, I just wasn't sure how to compare a circle against the two angles...

    hmm...I might try looking at dot products or similar + the line normal and see if I can test against the 2 lines and circle that way

    cheers,
    Paul

  4. #4
    An idea: transform the object/player coordinates to polar (you know: <angle, distance>) relative to the origin of your beam. In pseudocode:
    Code:
      IF vector_length (object.position - beam.position) < beam.radius THEN
        new_object.position := object.position - beam.position;
        polar_object := polar (new_object.position);
        IF polar_object.angle BETWEEN beam.angle1 AND beam.angle2 THEN
          object.hit ();
        END IF;
      END IF;
    No signature provided yet.

  5. #5
    With functions in my lib:
    Code:
    // Angle between 2 vectors, given in radians
    function Angle(src,dest: single): single;
    begin
      result:=src-dest;
      while result<-PI do result:=result+PI*2;
      while result>PI do result:=result-PI*2;
    end;
    
    // By pythagoras a^2+b^2=c^2 you don't need Sqrt() to determine the equality
    function PointInCircle(const p,circle: TVector2f; const radius: single): boolean;
    var x,y: single;
    begin
      x:=circle.x-p.x; y:=circle.y-p.y;
      PointInCircle:=(x*x+y*y)<(radius*radius);
    end;
    And knowing the angle_delta that is half of pie's slice angle, we can count if point is between them:

    Code:
    if PointInCircle(beam.position, player.position, radius) then
      if abs(Angle(beam.angle, angle_from_beam_to_player)) < angle_delta then begin
        Collide;
      end;
    You can also use this for the angle from beam to player:
    Code:
    function Angle(const px1,py1,px2,py2: single): single;
    begin
      result:=arctan2(py2-py1,px2-px1);
      if result<0 then result:=result+PI*2;
    end;
    Last edited by User137; 27-10-2010 at 01:02 PM.

  6. #6
    @?ëu?±o Mart??nez and User137: thanks guys, but you seem to have overlooked the fact that I want to check if a circle, not a point is between the 2 angle

    cheers,
    Paul

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
  •