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
    Hello,

    Thank you Paul for the suggestion... I guess in my confusion I confused you as well sorry... Andorra 2d also top left origin... anyway I was banging my head against the wall the whole day (probably overcomplicating something that is not that complex...) . I've decided I give a try to what Traveler said before... to include direction in my testforwall function. And this time (kinda like a wonder) it really works :

    TestWall function
    Code:
    function TBlurp.testForWall(spriteXpos,spriteYpos:single;spriteWidth,spriteHeight:integer;direction:string):boolean;
    begin
      testForWall := false;
      if direction = 'left' then
        begin
          if not (TileInfo[round(spriteXpos) div 64, round(spriteYpos) div 64].tilenr in [2]) or not
          (TileInfo[round(spriteXpos)div 64, (round(spriteYpos)+spriteHeight) div 64].tilenr in [2]) then testForWall := true;
        end;
      if direction = 'right' then
        begin
          if not(TileInfo[(round(spriteXpos)+spriteWidth) div 64, round(spriteYpos) 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;
      if direction = 'up' then
        begin
          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]) then testForWall :=true;
        end;
      if direction = 'down' then
        begin
          if 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;
    end;
    controlling of character
    Code:
        ActTime := ActTime + AdPerCounter.TimeGap;
        if ActTime > 25 then
        begin
              if GetKeyState(VK_LEFT) < 0 then
                begin
                  if not Blurp.testForWall(Blurp.X-4,Blurp.Y,Blurp.Image.Width,Blurp.Image.Height-1,'left') 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,Blurp.Y,Blurp.Image.Width,Blurp.Image.Height-1,'right') 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-1,Blurp.Image.Height,'up') 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,Blurp.Image.Width-1,Blurp.Image.Height,'down') then
                    begin
                      Blurp.Y:=Blurp.Y+4;
                      AdSpriteEngine.Y:=AdSpriteEngine.Y-4;
                    end;
                end;
          ActTime := 0;
        end;
    Right now I'm doing the movement onIdle of TFORM ... which means I'm not using TimeGap for movement... which is bad I know, I just wanted to make it real simple for now (so that I can sleep well, that atleast I've acomplished half of what I wanted).... tomorrow I'll modify my code so that the movement is done in the Sprites.OnMove , and I only give x or y velocity here...

    Anyway I've attached my code with the compiled executable, if I may ask you to please test it, it works. And I'm hoping it works just as well as the one you've suggested. (multiple solutions for the same problem).

    egyszeru 7 5-os maskepp.zip

    Greetings
    Rob

  2. #2
    It works for me just fine (after I downloaded the missing directx dll (d3dx9_33.dll)...the last version I tried needed the d3dx9_31.dll version for some reason?

    I was thinking...if go for the opengl version dll for Andorra 2d, then your game (when finished) could be cross-platform...just a thought, and my $0.02

    cheers,
    Paul

  3. #3

    Movement using the DoMove (not really a question) comments welcome if this is wrong

    Hi,

    I was playing a bit further with my program, and modified so I do the movement in the SpriteEngine.DoMove procedure , and it works fine regardles if velocity is 80, 95, or whatever... what I modified compared to my older source is :

    TForm1.OnIDLE

    Here is one obvious problem, which I did not even tried to solve... for now, somehow my Figure moves faster then the
    screen is scroll. ( I was hoping the speed would be the same...but there is a slight difference... will play with it later... was not
    the object here to solve that ... but rather to implement the movement more properly)

    Code:
        ActTime := ActTime + AdPerCounter.TimeGap;
        if ActTime > 25 then
        begin
          Blurp.vx:=0;
          Blurp.vy:=0;
              if GetKeyState(VK_LEFT) < 0 then
                begin
                      Blurp.vx:=-95;
                      AdSpriteEngine.X:=AdSpriteEngine.X-Blurp.vx*(AdPerCounter.TimeGap/1000);
                end;
              if GetKeyState(VK_RIGHT) < 0 then
                begin
                      Blurp.vx:=95;
                      AdSpriteEngine.X:=AdSpriteEngine.X-Blurp.vx*(AdPerCounter.TimeGap/1000);
                end;
              if GetKeyState(VK_UP) < 0 then
                begin
                      Blurp.vy:=-95;
                      AdSpriteEngine.Y:=AdSpriteEngine.Y-Blurp.vy*(AdPerCounter.TimeGap/1000);
                end;
              if GetKeyState(VK_DOWN) < 0 then
                begin
                      Blurp.vy:=95;
                      AdSpriteEngine.Y:=AdSpriteEngine.Y-Blurp.vy*(AdPerCounter.TimeGap/1000);
                end;
          ActTime := 0;
        end;
    Here Is my modified DoMove , for now works great, been playing with it , using different velocity... always stops where it should yippeee

    Code:
    procedure TBlurp.DoMove(TimeGap: Double);
    begin
      inherited;
      // left
      if vx < 0 then
        begin
          if not testForWall(X+vx*TimeGap,Y,Image.Width,Image.Height-1,'left') then
            begin
              X:=X+round(vx*TimeGap) ;
            end;
        end;
      // right
      if vx > 0 then
        begin
         if not testForWall(X,Y,Image.Width,Image.Height-1,'right') then
           begin
             X:=X+round(vx*TimeGap);
           end;
        end;
      // up
      if vy < 0 then
        begin
          if not testForWall(X,Y+vy*TimeGap,Image.Width-1,Image.Height,'up') then
            begin
              Y:=Y+round(vy*TimeGap);
            end;
        end;
      // down
      if vy > 0 then
        begin
          if not testForWall(X,Y,Image.Width-1,Image.Height,'down') then
            begin
              Y:=Y+round(vy*TimeGap);
            end;
        end;
    
      Collision;
    end;
    Also one unexpected sidefect is ... before it was important where my Start position for my character was... now it's not , X can be 128,139,131 ... it works the same.

    Anyway I'll continue now with implementing gravity And checking the scrolling part.

    Greetings
    Rob

  4. #4

    Scrolling works now , ... and unfortunatelly gotta go get some sleep... :(

    Code:
        if ActTime > 10 then
        begin
          Blurp.vx:=0;
          Blurp.vy:=0;
              if GetKeyState(VK_LEFT) < 0 then
                begin
                      Blurp.vx:=-80;
                      if (Blurp.X > 320) and (Blurp.X < (40*64)-320) then
                        begin
                          AdSpriteEngine.X:=(Blurp.X*-1)+320;
                        end;
                end;
              if GetKeyState(VK_RIGHT) < 0 then
                begin
                      Blurp.vx:=80;
                      if (Blurp.X > 320) and (Blurp.X < (40*64)-320) then
                        begin
                          AdSpriteEngine.X:=(Blurp.X*-1)+320;
                        end;
                end;
              if GetKeyState(VK_UP) < 0 then
                begin
                      Blurp.vy:=-80;
                      if (Blurp.Y > 240) and (Blurp.Y < (29*64)-240) then
                        begin
                          AdSpriteEngine.Y:=(Blurp.Y*-1)+240;
                        end;
                end;
              if GetKeyState(VK_DOWN) < 0 then
                begin
                      Blurp.vy:=80;
                      if (Blurp.Y > 240) and (Blurp.Y < (29*64)-240) then
                        begin
                          AdSpriteEngine.Y:=(Blurp.Y*-1)+240;
                        end;
                end;
          ActTime := 0;
        end;
    Due to the fact that I use 60 FPS limit... had to decrease ACT TIME to 10... I don't know what to call this effect but when ACT TIME was 60... the screen was moving... I dunnot a bit erm... tear effect ... the outer lines of my character seemed to be jumping in and out of the character when it was redrawn...

    Anyway now it works great , and was pretty simple to do to. Only thing, gravity...tomorrow.

    Greetings
    Rob

    ps. : once I'm finished with my experiments...I'll use const and more variables instead of manually putting in numbers....
    Last edited by robert83; 21-08-2011 at 12:56 AM. Reason: changed source

  5. #5
    Hi Rob, glad to hear that it is now working for you

    cheers,
    Paul

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
  •