Recent

Author Topic: Line collision with sliding effect  (Read 2831 times)

lazboy1

  • Newbie
  • Posts: 5
Line collision with sliding effect
« on: January 17, 2017, 03:12:03 am »
ok so im using lazarus and i have a problem.

im trying to make a wall collision sytem similar to games like doom 1/2 ect. so basicly when i hit a wall at an angle it makes the player "slide" along that wall.

what i have is...
a line with starting xy and ending xy points, i allready have working code to detect a collision on the line and get the pixel xy point of the collision on the line. but i cant get the sliding effect to work.

ive found some info on this, but i still cant figure it out...

so im hoping someone could give me some simple working code to do it in like a function or something.

something like...

a function that takes the player view Angle and XY position + the collision point in the line and returns it as a new player XY position.

so im basicly looking for some working code rather than the science behind the method. (my math skills suck!)

any help would be appreciated!  :)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Line collision with sliding effect
« Reply #1 on: January 17, 2017, 10:52:07 am »
You need two more data:
  • Wall normal vector.
  • Object radius.
Then, once you know the point of collision you just need to set the object position to the wall normal vector from the collision point multiplied by the object radius.  Just like the attached picture taken from this flipcode article.

Also, if you're doing games I recommend you to join the Pascal Game Development community.
« Last Edit: January 17, 2017, 10:54:05 am by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

derek.john.evans

  • Guest
Re: Line collision with sliding effect
« Reply #2 on: January 17, 2017, 12:01:15 pm »
I had to dust off some old maths to write this.

Assuming you have the closest point on the line (wall), then its easy to calculate the response (reflex) vector.
Code: Pascal  [Select][+][-]
  1. function CalcReflex2(const ACenter: TVector2; const ARadius: TScalar; const APoint: TVector2; out AReflex: TVector2): boolean;
  2. var
  3.   LDistance: TScalar;
  4. begin
  5.   AReflex := Subtract2(APoint, ACenter);
  6.   LDistance := MagnitudeSquared2(AReflex);
  7.   Result := LDistance < (ARadius * ARadius);
  8.   if Result then begin
  9.     LDistance := Sqrt(LDistance);
  10.     AReflex := Multiply2(AReflex, (LDistance - ARadius) / LDistance);
  11.   end;
  12. end;  
  13.  

The same code is used for collision with monsters. You then decide if you want to bounce or block (or a combination of the two).

Code: Pascal  [Select][+][-]
  1. if CalcReflex2(FBallPos, 8, FClosestPoint, LReflex) then begin
  2.   FBallPos := Addition2(FBallPos, LReflex);
  3.   FBallVel := Addition2(FBallVel, Multiply2(LReflex, 0.2));
  4. end;    
  5.  

Note: The above is a blocking collision with 20% bounce.

Here is a simple demo with some code I'm writing for FPCJVM.

lazboy1

  • Newbie
  • Posts: 5
Re: Line collision with sliding effect
« Reply #3 on: January 17, 2017, 10:11:09 pm »
awesome this was just what i was looking for! thx guys for help!  :)

 

TinyPortal © 2005-2018