Results 1 to 10 of 21

Thread: Circle vs pie segment collision detection

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.

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
  •