Results 1 to 10 of 18

Thread: Ponx - High DPI aware PONG style game for Linux

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Quote Originally Posted by Jonax View Post
    The reason for this to simplify the programming of course. I'm afraid letting the ball cross that line may cause a host of new programming headaches. If I had tried that apporoach I'd probably still be sweating over special cases and have nothing to show. On the other hand it may not be so bad and improved gameplay is priceless.
    If you start using relative distance from the center of the ball against center of the playfield I believe you could perhaps even simplify your programming quite a bit.

    First you need to make sure that your playfield coordinates are position so that the position 0:0 will be in the center of your playfield. I'm guessing you are now working with Window coordinates. So you would make this transformation by simply subtracting half of the width of your playfield window width from X coordinate and half of your playfield window height from Y coordinate.

    For instance in my quick mock up I use mouse for moving the ball so I transform my mouse coordinates to PlayfIeld coordinates using

    Code:
    PlayfieldX := X-(PaintBox1.ClientWidth div 2);
    PlayfieldY := Y-(PaintBox1.ClientHeight div 2);
    I then pass this Playfield coordinates to my Collision detection procedure that looks like this


    Code:
    procedure TForm5.CheckCollision(X,Y: Integer);begin
      //Reset collisions
      TopBorderCollision := False;
      BottomBorderCollision := False;
      LeftBorderCollision := False;
      RightBorderCollision := False;
    
    
      //Check for top/bottom collisions
      if Abs(Y) > ((PaintBox1.ClientHeight div 2)-BorderThickenss) then
      begin
        if Y < 0 then
        begin
          TopBorderCollision := True;
        end
        else BottomBorderCollision := True;
      end;
    
    
      //Check for left/right border collisions
      if Abs(X) > ((PaintBox1.ClientWidth div 2)-BorderThickenss) then
      begin
        if X < 0 then
        begin
          LeftBorderCollision := True;
        end
        else RightBorderCollision := True;
      end;
    
    
      //Check to see if ball is in slowdown area then reduce its speed
      if ABS(X) > ((PaintBox1.ClientWidth div 2)-BorderThickenss-SlowdownAreaWidth) then BallSpeed := 20
      //else keep it moving at full speed
      else BallSpeed := 20;
    
    
      //Check paddle collision
      if X > 0 then
      //Player padle collision
      begin
        //Simple collision checling against paddle using X coordinate of the ball
        //Ball Y position si within player paddle area
        if (Y >= PlayerPaddle.Top) and (Y <= (PlayerPaddle.Bottom)) then
        begin
          if ABS(X) > ((PaintBox1.ClientWidth div 2)-PlayerPaddle.Width) then //Bounce the ball from the paddle by multiplying X speed by -1;
        end
        //Because the ball Y position is not within the PlayerPaddle area it means we will have to check collision
        //against PlayePaddle corners instead
        else
        begin
          //Top corner
          if System.Math.Hypot(ABS(X-PlayerPaddle.Right),ABS(Y-PlayerPaddle.Top)) < BallDiameter then
          begin
            //Bounce ball from top paddle corner. It would probably result in great change of the ball trajectory angle
          end;
          //Bottom corner
          if System.Math.Hypot(ABS(X-PlayerPaddle.Right),ABS(Y-PlayerPaddle.Bottom)) < BallDiameter then
          begin
            //Bounce ball from top paddle corner. It would probably result in great change of the ball trajectory angle
          end;
        end;
      end;
    end;
    I hope my code mock up can give you good enough idea of how to work with such approach and how easy it actually is. I tried to comment it as good as I can so ti can be even more understandable.
    I'm guessing you will have to make some changes so that it will work with your data structures
    Last edited by SilverWarior; 14-05-2022 at 06:50 PM.

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
  •