hedgewars/uCollisions.pas
changeset 6279 7f724835ea57
parent 6124 bee90df26109
child 6453 11c578d30bd3
equal deleted inserted replaced
6278:654eed7c6b97 6279:7f724835ea57
    50 function  TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt; withGear: boolean = true): boolean;
    50 function  TestCollisionXwithXYShift(Gear: PGear; ShiftX: hwFloat; ShiftY: LongInt; Dir: LongInt; withGear: boolean = true): boolean;
    51 function  TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt; withGear: boolean = true): boolean;
    51 function  TestCollisionYwithXYShift(Gear: PGear; ShiftX, ShiftY: LongInt; Dir: LongInt; withGear: boolean = true): boolean;
    52 
    52 
    53 function  TestRectancleForObstacle(x1, y1, x2, y2: LongInt; landOnly: boolean): boolean;
    53 function  TestRectancleForObstacle(x1, y1, x2, y2: LongInt; landOnly: boolean): boolean;
    54 
    54 
    55 function  CalcSlopeTangentBelowGear(Gear: PGear; var outDeltaX, outDeltaY: LongInt): boolean;
    55 // returns: negative sign if going downhill to left, value is steepness (noslope/error = _0, 45° = _0_5)
       
    56 function  CalcSlopeBelowGear(Gear: PGear): hwFloat;
    56 function  CalcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): Boolean;
    57 function  CalcSlopeTangent(Gear: PGear; collisionX, collisionY: LongInt; var outDeltaX, outDeltaY: LongInt; TestWord: LongWord): Boolean;
    57 
    58 
    58 implementation
    59 implementation
    59 uses uConsts, uLandGraphics, uVariables, uDebug, uGears;
    60 uses uConsts, uLandGraphics, uVariables, uDebug, uGears;
    60 
    61 
   401 
   402 
   402     // we start searching from the direction the gear came from
   403     // we start searching from the direction the gear came from
   403     if (dx.QWordValue > _0_995.QWordValue )
   404     if (dx.QWordValue > _0_995.QWordValue )
   404     or (dy.QWordValue > _0_995.QWordValue ) then
   405     or (dy.QWordValue > _0_995.QWordValue ) then
   405         begin // scale
   406         begin // scale
   406         s := _1 / Distance(dx,dy);
   407         s := _0_995 / Distance(dx,dy);
   407         dx := s * dx;
   408         dx := s * dx;
   408         dy := s * dy;
   409         dy := s * dy;
   409         end;
   410         end;
   410 
   411 
   411     mx:= hwRound(Gear^.X-dx) - hwRound(Gear^.X);
   412     mx:= hwRound(Gear^.X-dx) - hwRound(Gear^.X);
   517 outDeltaX:= ldx;
   518 outDeltaX:= ldx;
   518 outDeltaY:= ldy;
   519 outDeltaY:= ldy;
   519 exit(true);
   520 exit(true);
   520 end;
   521 end;
   521 
   522 
   522 function CalcSlopeTangentBelowGear(Gear: PGear; var outDeltaX, outDeltaY: LongInt): boolean;
   523 function CalcSlopeBelowGear(Gear: PGear): hwFloat;
   523 var dx, dy: hwFloat;
   524 var dx, dy: hwFloat;
   524     collX, i, y, x, gx: LongInt;
   525     collX, i, y, x, gx, sdx, sdy: LongInt;
   525     isColl, succ: Boolean;
   526     isColl, succ: Boolean;
   526 begin
   527 begin
   527 // save original dx/dy
   528 
   528 dx:= Gear^.dX;
       
   529 dy:= Gear^.dY;
       
   530 
       
   531 Gear^.dX.QWordValue:= 0;
       
   532 Gear^.dY:= _1;
       
   533 
   529 
   534 y:= hwRound(Gear^.Y) + Gear^.Radius;
   530 y:= hwRound(Gear^.Y) + Gear^.Radius;
   535 gx:= hwRound(Gear^.X);
   531 gx:= hwRound(Gear^.X);
   536 collX := gx;
   532 collX := gx;
   537 isColl:= false;
   533 isColl:= false;
   551      inc(x)
   547      inc(x)
   552    until (x > i);
   548    until (x > i);
   553    end;
   549    end;
   554 
   550 
   555 if isColl then
   551 if isColl then
   556     succ := CalcSlopeTangent(Gear, collX, y, outDeltaX, outDeltaY, 255)
   552     begin
   557 else
   553     // save original dx/dy
   558     succ := false;
   554     dx := Gear^.dX;
   559 
   555     dy := Gear^.dY;
   560 // restore original dx/dy
   556 
   561 Gear^.dX:= dx;
   557     Gear^.dX.QWordValue:= 0;
   562 Gear^.dY:= dy;
   558     Gear^.dX.isNegative:= (collX >= gx);
   563 
   559     Gear^.dY:= _1;
   564 CalcSlopeTangentBelowGear := succ;
   560 
       
   561     sdx:= 0;
       
   562     sdy:= 0;
       
   563     succ := CalcSlopeTangent(Gear, collX, y, sdx, sdy, 255);
       
   564 
       
   565     // restore original dx/dy
       
   566     Gear^.dX := dx;
       
   567     Gear^.dY := dy;
       
   568 
       
   569     if succ and (sdx <> 0) and (sdy <> 0) then
       
   570     begin
       
   571         dx := int2hwFloat(sdy) / (abs(sdx) + abs(sdy));
       
   572         dx.isNegative := (sdx * sdy) < 0;
       
   573         exit (dx);
       
   574     end;
       
   575     end;
       
   576 
       
   577 CalcSlopeBelowGear := _0;
   565 end;
   578 end;
   566 
   579 
   567 procedure initModule;
   580 procedure initModule;
   568 begin
   581 begin
   569     Count:= 0;
   582     Count:= 0;