Results 1 to 10 of 10

Thread: Collision done directly with OnCollision ( don't understand it, help me understand)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Your main problem is that you are not identifying which side you are hitting, and code you posted above has values after VK_UP all illogical.

    Maybe something like this. I'm turning the logics upside down, by assuming that DoCollision is called only when and with sprite that it collides with. So all i do is check which side the block is in relation to player and move (untested):
    Code:
    procedure TBlurp.DoCollision(Sprite:TSprite; var Done:boolean);
    var
      TileLeft, TileRight, TileTop, TileBottom: Integer;
    begin
      if Sprite is TPlatform then begin
        TileLeft := Trunc(TPlatform(Sprite).X);
        TileTop := Trunc(TPlatform(Sprite).Y);
        TileRight := TileLeft + TPlatform(Sprite).Image.Width;
        TileBottom := TileTop + TPlatform(Sprite).Image.Height;
        
        if TileLeft div TileW = trunc(X+Width/2) div TileW then 
          if TileTop div TileH < trunc(Y+Height/2) div TileH then begin
            Y := TileBottom;
          end else if TileTop div TileH > trunc(Y+Height/2) div TileH then begin
            Y := TileTop - Height;
          end;
        if TileTop div TileH = trunc(Y+Height/2) div TileH then
          if TileLeft div TileW < trunc(X+Width/2) div TileW then begin
            X := TileRight;
          end else if TileLeft div TileW > trunc(X+Width/2) div TileW then begin
            X := TileLeft - Width;
          end;
      end;
    end;
    Edited. Noticed it might have acted wrong without extra if's. And another major error hopefully fixed. Now i notice this could use more precalculation to vars... The code blew up in hands.

    Also wanted to mention this earlier. By math logics this can be simplified:
    Code:
    camera_x:=(X*-1)+320-TileW;
    Code:
    camera_x:=320-X-TileW;

    Last edit: Realising that this code of mine will not cause collision when moving towards corners, it's almost as good as nothing. So i'll stick to what i said earlier, you need something really different kind of collision method for this problem.

    The versions you showed earlier with gravity seemed to work almost fine. Just a few fixable bugs but they had overall more potential to work. One thing for example, when the jumping state is on and player hits block above, you must set the vertical velocity 0 and make player start dropping. This caused player to stick to the roof for small time.

    Last edit2 (lol i'm just bored): I think it might work with this kind of doCollision too if instead of just checking player position, use the player movement direction (vx, vy) as help to determine what wall to collide with. What mainly boggles my mind is situation where there is 3 roof blocks above, and player holds keys Left and Up. First collision test will be to the top left block which will same time cause player to stop vertical and horizontal movement in corner. That is why in above code i ignore corner blocks, but that causes another issue when there is only corner and nothing above, it wouldn't collide.
    Last edited by User137; 29-08-2011 at 11:57 AM.

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
  •