Results 1 to 10 of 31

Thread: Collission detection on level ( source Travels doc + some other examples)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    There are two ways to counter the-getting-stuck-in-the-wall problem.

    1) Move away from the wall when you're inside it. (the preferred method)
    For example, you're moving to the left at 3 pixel/cycle. You're going to hit the wall in 11px. Meaning you'll end up in the wall (and get stuck) in 4 cycles. At the fourth cycle your position is 1px inside the wall, so you correct that by setting the sprite position 2 pixels back.

    2) Change the testwall function to include the direction. Meaning when you go to the right you only test if the right side of your sprite is inside a wall. If you're going left, only test if you with with the left part. This way you can still move if one side of your sprite is inside a wall.
    Last edited by Traveler; 15-08-2011 at 10:08 PM.

  2. #2
    The tutorial site I posted seems to do the testing like this:

    Code:
    find new object position (position + velocity).
    if edge inside a wall
      move object so it touches edge of wall 
    else 
      do full movement in that direction
    Oh, and it breaks up the movement and does it vertically then horizontally (or is it the other way around...I forget LOL)

    I hope this helps

    cheers,
    Paul

  3. #3
    @Robert: hmm...I tried running your test program to take a look, but it doesn't run for me

    I get the dialog box error below:
    ---------------------------
    Dreamworld: DreamWorld.exe - Unable To Locate Component
    ---------------------------
    This application has failed to start because d3dx9_33.dll was not found. Re-installing the application may fix this problem.
    ---------------------------
    OK
    ---------------------------
    cheers,
    Paul

  4. #4
    you can download those dll files manually.

  5. #5
    ok, thanks

    cheers,
    Paul

  6. #6
    Hello,

    I've changed the movement from 3 to 4 now, so that it can be divided correctly... since my level is made of 64x64 tiles ...

    I think the problem is here but don't know how to fix....

    Left and Top works always on my level everywhere... it stops right there when it reaches the wall (no pixel left out...) , and I can continue to move in any other direction ...

    Left = works
    Top = works
    Right = stops 4 pixels sooner then it should...always
    Bottom = stops 4 pixels sooner then it should...always

    meanwhile I've also found out that it's important where my character starts... so I've set the starting position to

    X (3x64)-32
    Y (3x64)-32

    I hope I'm not wrong here, but currently my tiles are 64x64, whereas my character is 32x32.

    I've checked the Right and Bottom testwall functions... it works as it should because it is right that if it lets another +4 to x or +4 to y ... it is true... only thing is I can't seem to firugre it out... how the heck does it work correctly for Left and Top...
    Code:
     
     
               if GetKeyState(VK_LEFT) < 0 then
                begin
                  if not Blurp.testForWall(Blurp.X-4,Blurp.Y,Blurp.Image.Width,Blurp.Image.Height) then
                    begin
                      Blurp.X:=Blurp.X-4;
                      AdSpriteEngine.X:=AdSpriteEngine.X+4;
                    end;
                end;
              if GetKeyState(VK_RIGHT) < 0 then
                begin
                  if not Blurp.testForWall(Blurp.X+4,Blurp.Y,Blurp.Image.Width,Blurp.Image.Height) then
                    begin
                      Blurp.X:=Blurp.X+4;
                      AdSpriteEngine.X:=AdSpriteEngine.X-4;
                    end;
                end;
              if GetKeyState(VK_UP) < 0 then
                begin
                  if not Blurp.testForWall(Blurp.X,Blurp.Y-4,Blurp.Image.Width,Blurp.Image.Height) then
                    begin
                      Blurp.Y:=Blurp.Y-4;
                      AdSpriteEngine.Y:=AdSpriteEngine.Y+4;
                    end;
                end;
              if GetKeyState(VK_DOWN) < 0 then
                begin
                  if not Blurp.testForWall(Blurp.X,Blurp.Y+4,Blurp.Image.Width,Blurp.Image.Height) then
                    begin
                      Blurp.Y:=Blurp.Y+4;
                      AdSpriteEngine.Y:=AdSpriteEngine.Y-4;
                    end;
    Code:
    function TBlurp.testForWall(spriteXpos,spriteYpos:single;spriteWidth,spriteHeight:integer):boolean;
    begin
      testForWall := false;
      if not (TileInfo[round(spriteXpos) div 64, round(spriteYpos) div 64].tilenr in [2]) or not
      (TileInfo[(round(spriteXpos)+spriteWidth) div 64, round(spriteYpos) div 64].tilenr in [2]) or not
      (TileInfo[round(spriteXpos)div 64, (round(spriteYpos)+spriteHeight) div 64].tilenr in [2]) or not
      (TileInfo[(round(spriteXpos)+spriteWidth) div 64, (round(spriteYpos)+spriteHeight) div 64].tilenr in [2]) then testForWall :=true;
    end;
    Greetings
    Rob

  7. #7
    I'll throw out this kind of suggestion:
    Code:
              if GetKeyState(VK_LEFT) < 0 then
                begin
                  if not Blurp.testForWall(Blurp.X-4,Blurp.Y) then
                    begin
                      Blurp.X:=Blurp.X-4;
                      AdSpriteEngine.X:=AdSpriteEngine.X+4;
                    end;
                end;
              if GetKeyState(VK_RIGHT) < 0 then
                begin
                  if not Blurp.testForWall(Blurp.X+4+Blurp.Image.Width,Blurp.Y) then
                    begin
                      Blurp.X:=Blurp.X+4;
                      AdSpriteEngine.X:=AdSpriteEngine.X-4;
                    end;
                end;
              if GetKeyState(VK_UP) < 0 then
                begin
                  if not Blurp.testForWall(Blurp.X,Blurp.Y-4) then
                    begin
                      Blurp.Y:=Blurp.Y-4;
                      AdSpriteEngine.Y:=AdSpriteEngine.Y+4;
                    end;
                end;
              if GetKeyState(VK_DOWN) < 0 then
                begin
                  if not Blurp.testForWall(Blurp.X,Blurp.Y+4+Blurp.Image.Height) then
                    begin
                      Blurp.Y:=Blurp.Y+4;
                      AdSpriteEngine.Y:=AdSpriteEngine.Y-4;
                    end;
                end;
    Code:
    function TBlurp.testForWall(pointX,pointY:single):boolean;
    var tilenr: integer;
    begin
      tilenr:=TileInfo[trunc(pointX) div 64, trunc(pointY) div 64].tilenr;
      case tilenr of
        2: result:=true;
        else result:=false;
      end;
    end;
    Last edited by User137; 16-08-2011 at 03:58 PM. Reason: trunc

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
  •