Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


moneal2001

Member Since 02 Dec 2011
Offline Last Active Yesterday, 08:28 PM

Topics I've Started

Pong Collision Question [XNA]

14 April 2013 - 01:44 AM

I am working on collision for a pong clone i am working on.  I set up a rectangle for the ball and the paddles and when those intersect, I want to invert the X or Y direction the ball is moving based on the area of the ball that collides with the paddle.

 

Examples.  bottom of ball hits the top of the paddle invert the Y direction of motion, or right side of ball hits the left side of paddle invert X direction of motion.

 

So after the collision call happens I created 4 rectangles representing the ball 1 for each 4th of the ball: top left, top right, bottom left, and bottom right.  and set up if statements to check for intersection between those and the paddle rectangle.

 

here is the collision code i have

 

        private void checkPaddleCollision(List<Paddle> paddles)
        {
            CollisionType type = CollisionType.Left;
            bool collide = false;
            Paddle paddle = new Paddle();
            
                       

            foreach (Paddle p in paddles)
            {
                

                if(rect.Intersects(p.Bounds))
                {

                    paddle = p;

                    collide = true;

                    Rectangle topLeft, topRight, bottomLeft, bottomRight;
                    int height = rect.Height / 2;
                    int width = rect.Width / 2;

                    topLeft = new Rectangle(rect.X, rect.Y, width, height);
                    topRight = new Rectangle(rect.X + width, rect.Y, width, height);
                    bottomLeft = new Rectangle(rect.X, rect.Y + height, width, height);
                    bottomRight = new Rectangle(rect.X + width, rect.Y + height, width, height);

                    if((p.Bounds.Intersects(topLeft) || p.Bounds.Intersects(topRight)) &&(!p.Bounds.Intersects(bottomLeft) || !p.Bounds.Intersects(bottomRight)))
                    {
                        type = CollisionType.Top;
                    }
                    else if ((p.Bounds.Intersects(bottomLeft) || p.Bounds.Intersects(bottomRight)) && (!p.Bounds.Intersects(topLeft) || !p.Bounds.Intersects(topRight)))
                    {
                        type = CollisionType.Bottom;
                    }
                    else if ((p.Bounds.Intersects(bottomLeft) || p.Bounds.Intersects(topLeft)) && (!p.Bounds.Intersects(topRight) || !p.Bounds.Intersects(bottomRight)))
                    {
                        type = CollisionType.Left;
                    }
                    else if ((p.Bounds.Intersects(bottomRight) || p.Bounds.Intersects(topRight)) && (!p.Bounds.Intersects(topLeft) || !p.Bounds.Intersects(bottomLeft)))
                    {
                        type = CollisionType.Right;
                    }
                                    
                }
            }

            if (collide)
            {
                switch (type)
                {
                    case CollisionType.Left:
                        position.X = paddle.Position.X + paddle.Bounds.Width;
                        break;
                    case CollisionType.Right:
                        position.X = paddle.Position.X - texture.Width;
                        break;
                    case CollisionType.Bottom:
                        position.Y = paddle.Position.Y + texture.Height;
                        break;
                    case CollisionType.Top:
                        position.Y = paddle.Position.Y + paddle.Bounds.Height;
                        break;
                }

                if (type == CollisionType.Left || type == CollisionType.Right)
                {
                    direction.X *= -1;
                    direction.Normalize();
                }
                else
                {
                    direction.Y *= -1;
                    direction.Normalize();
                }
            }
        }

The collision works fine when the ball hits the top or bottom of a paddle but it is not regestering correctly when it hits the left or right of a paddle.  It sees it as either top or bottom until the ball is halfway through the paddle then it regesters.  can anyone point me in the right direction.  Thanks.


help developing better coding

22 February 2012 - 12:39 PM

if anyone could be kind enough to download my number guessing game source code and give me some feedback on how to improve the structure and general coding. it can be downloaded here: http://www.mediafire.../?4zlc9wipcsdp1 I can post the code here if no one wants to download it. any help would be greatly appreciated.


edit:

here is my main code

#include "stdafx.h"
using namespace std;
string anWords[] = { "town", "house", "slant", "light", "orange", "time", "workout", "jumprope", "baby", "eraser"};
string word = "";
int misses = 6;
char playAgain = 'y';
int win = 0;

int _tmain(int argc, _TCHAR* argv[])
{
srand(time(0));

while (playAgain != 'n')
{ word = anWords[nrand(0, 9)];
  StartGame(word);
  while (win != 1 && win != 2)
   win = PlayGame(word, misses);
  GameOver(win, word);
  win = 0;
  playAgain = PlayAgain(playAgain);

}

return 0;
}

this is my function code, was mostly doing this to see if i knew how to use multiple code files and headers:

#include "stdafx.h"
using namespace std;
void StartGame(string word)
{
int s = word.length();
cout << "I have a word in my head. It is " << s << " letters long can you gess it.?" << endl;
}
char PlayAgain(char playAgain)
{

cout << "Play again? y or n " << endl;
cin >> playAgain;
while (playAgain != 'y' && playAgain != 'n')
{
  cout << "Choice was not entered correctly please enter y or n again? ";
  cin >> playAgain;
}

return playAgain;
}
int PlayGame(string word, int m )
{
int win = 0;
char guess;
int wordSize = word.length();
string guesses;
string playerWord;
for (int i = 0; i < wordSize; ++i)
{
  playerWord += "_";
}
string tempWord = playerWord;
cout << playerWord << endl;
while (win != 1 && win != 2)
{
  cout << "You have " << m << " guesses left." << endl;
  cout << "Please guess a letter. " << endl;
  cin >> guess;
  unsigned int x;
  for( x = 0; x < guesses.length(); ++x)
  {
   if (guess == guesses[x])
   {
	cout << "You have already guessed that letter." << endl;
	cin >> guess;
	break;
   }
  }
  int i;
  for (i = 0; i < wordSize; ++i)
  {
   if (guess == word[i])
	playerWord[i] = guess;
  }
  if (playerWord == tempWord)
  {
   --m;
   tempWord = playerWord;
  }
  else
   tempWord = playerWord;
	  
  guesses += guess;
  if (playerWord == word)
   return 1;
  else if (m == 0)
   return 2;
  else
  {
   cout << playerWord << endl;
   cout << "You have guessed these letters so far: " << guesses << endl;
  }

}
return 0;
}
void GameOver(int win, string word)
{
if (win == 1)
{
  cout << "You won way to go." << endl;
  cout << "You were right.  The word was " << word << "." << endl;
}
else
{

   cout << "Sorry you lost." << endl;
   cout << "The word was " << word << "." << endl;
}
}

PARTNERS