Results 1 to 7 of 7

Thread: Problem with collision detection

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Decided to ditch this part of the code and just use Point in Polygon(PNPoly) function.


    Code:
    // COLLISION DETECTION
            for I := 0 to EM.Entities.Count - 1 do
                for J := Low(A.Polygon) to High(A.Polygon) do
                    if LinesIntersect(A.Polygon[J].x1, A.Polygon[J].y1, A.Polygon[J].x2,
                        A.Polygon[J].y2, EM.Entities[I].Pos.X, EM.Entities[I].Pos.Y,
                        EM.Entities[I].Pos.X + EM.Entities[I].xSpeed,
                        EM.Entities[I].Pos.Y + EM.Entities[I].ySpeed, Inter) then
                    begin
                        EM.Entities[I].Target := Inter;
                        EM.Entities[I].Pos := Inter;
                    end;
    The collision detection above works nicely with non-moving objects, but once they start moving or rotating, it's done. When the asteroid moves, it's lines jump over the last or future position of the bullet, the bullet just passes and the check fails. The PNPoly function also works nicely with really fast moving objects, unless the speed is higher than the asteroid diameter itself.
    Here is the PNPoly function i have if anyone finds it useful:

    Code:
    function PNPoly(X, Y: Double; Polygon: array of TPointF): Boolean;
    var
        I, J, pLength: Integer;
        C: Boolean;
    begin
        pLength := Length(Polygon) - 1;
        J := pLength;
        C := False;
        for I := 0 to pLength do
        begin
            if ((((Polygon[I].Y <= Y) and (Y < Polygon[J].Y)) or
                ((Polygon[J].Y <= Y) and (Y < Polygon[I].Y))) and
                (X > (Polygon[J].X - Polygon[I].X) * (Y - Polygon[I].Y) /
                (Polygon[J].Y - Polygon[I].Y) + Polygon[I].X)) then
                C := not C;
            J := I;
        end;
        Result := C;
    end;
    Does anybody how i can find the collision point between the bullet and the polygon with the function above? I still have the LastX and LastY of the bullet, and i tried to check for Line Intersect between the current Bullet pos and last Pos, but it fails if last Pos is inside the asteroid...
    Last edited by IroncrossBG; 16-06-2019 at 09:51 AM.

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
  •