Advertisement
Guest User

CheckCollision

a guest
Dec 2nd, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1.         public void CheckCollision()
  2.         {
  3.             bool gravitation = true;
  4.             int l = -1;
  5.             if (playerPosY > 26)
  6.             {
  7.                 for (int i = 0; i < 360; i++) //Jeder Winkel wird gecheckt
  8.                 {
  9.                     float posX = (int)(Math.Round(playerPosX) + (Math.Cos(i) * 25)); //Cos(0) = 1
  10.                     float posY = (int)(Math.Round(playerPosY) + (Math.Sin(i) * 25)); //Sin(0) = 0
  11.  
  12.                     if (terrain[(int)Math.Round(posY), (int)Math.Round(posX)] != '+') //Kollision tritt an der Stelle posX,posY auf
  13.                     {
  14.                         if (l == -1)
  15.                             Game1.ShowIntValue(i, spriteBatch);
  16.                         l = 1;
  17.  
  18.                         gravitation = false;    //Am Boden herrscht keine Schwerkraft (verhindert ruckeln auf der Y Achse)
  19.                         for (int j = 0; j < 100; j++)
  20.                         {
  21.                             if (terrain[(int)Math.Round(posY) - j, (int)Math.Round(posX)] != '+' && j != 0) //Wenn der Spieler den Boden durchschlägt, wird überprüft ob sich in den nächsten 10 pixel über ihn Luft befindet und er wird auf diese Position gesetzt
  22.                             {
  23.                                 playerPosY -= 1;
  24.                             }
  25.                         }
  26.  
  27.                         //playerPosY -= 1;
  28.                         playerPosYSpeed *= -1;
  29.                         playerPosYSpeed += 1f;
  30.                         //playerPosXSpeed *= (float)Math.Cos(Convert.ToDouble(i));  //Kollisionserkennung an der X-Achse
  31.                         break;  //Schleife wird verlassen
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             if (gravitation)
  37.                 playerPosYSpeed += 0.04f;   //Gravitation
  38.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement