Jump to content
  • Advertisement
Sign in to follow this  
MPG

AI and collison detection in pong.

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok I've got the AI working pretty good,but the collison detection is way to early and the human collsion detection only works once.
  motion.x = ball.x - place.x;
        if(move ==  2) // move represents the state of the ball
		 {
	       ball.x = ball.x + 1;
		 }
		 if(move == 3)
		 {
			 ball.x = place.x + 1;
		 }
		 if(move == 4)
		 {
              ball.x = ball.x + -1;
		 }
	     if(ai == 2) // self explanatory :)
		 { 
			 place.y = place.y + -1;
		 }
		  if(ai == 3)
		  {
			  place.x = place.y + -1;
		  }
		  if(ai == 4)
		  {
		  place.y = place.y;
		  }

[Edited by - MPG on June 15, 2005 1:29:15 PM]

Share this post


Link to post
Share on other sites
Advertisement
I'm afraid that "if(ai == 2)" isn't actually self-explanatory. What do all those variables represent?

Share this post


Link to post
Share on other sites
Lol sorry, ai represents the state of the cpu controled wether the paddle need to go up or down or stop.

p.s. spelling is my weakest suit.

Share this post


Link to post
Share on other sites
I am confused as to what this is. You say
Quote:
Orignal post by MPG
Ok I've got the AI working pretty good,but the collison detection is way to early and the human collsion detection only works once.


So I was wondering just what you are talking about, I mean I think I have gathered that it is a pong game maybe. Also the code you posted is wierd to, could you maybe show the whole function?

Share this post


Link to post
Share on other sites
Yes it is a pong game and here is some more code sorry I thought I highlighted this aswell:

  if( ball.x > place.x  && ball.y < place.y || ball.y > place.y)
{

move = 2;
}
if(ball.x == place.x + motion.x)
{
if(ball.y < place.y)
{
ai = 2;
}
}
if(move == 2)
{
ai = 4;
}

Share this post


Link to post
Share on other sites
I think I did some similar stuff in the past, however, notice your source code as said by others is not self-explanatory, so, If you just would add comments on what each variable represents and what most of the code does, then, we could be able to help you out in a better way

cheers

Share this post


Link to post
Share on other sites
Use enums to make the code clearer.
enum paddleState { UP = 2, DOWN, STATIONARY };

...

paddleState ai = UP;

...

if( ai == UP ) {
place.y = place.y + -1;
}

if( ai == DOWN ) {
place.x = place.y + -1;
}

if( ai == STATIONARY ) {
place.y = place.y;
}


Edit: And give more information about the classes/structs/whatever

Share this post


Link to post
Share on other sites
Now my collision test between ball and dest do not work but anyways,I added the comments to the code so here it is:

SDL_Rect motion; ///represents the distance between ball.x and place.x
motion.x = ball.x - place.x;

enum State { UP = 2 , DOWN,STOP,LEFT,RIGHT };
State move;// this represents the state of the ball
move = STOP;

State ai; // this represents
ai = STOP ;
obj = SDL_LoadBMP("ball.bmp");
pic = SDL_LoadBMP("rect.bmp");
red = SDL_LoadBMP("red.bmp");
game = SDL_LoadBMP("icon");
main = SDL_LoadBMP("main");
while(k == true)
{


int b=5;
SDL_Event event;
if(b == 6)
{
SDL_BlitSurface(main,NULL,screen,&menu);
SDL_BlitSurface(game,NULL,screen,&ui);
}
if(b==5)
{
SDL_BlitSurface(obj,NULL,screen,&ball);
SDL_BlitSurface(pic,NULL,screen,&dest);
SDL_BlitSurface(red,NULL,screen,&place);

}
SDL_Flip(screen);
if(move == LEFT) // move represents the state of the ball
{
ball.x = ball.x + 1; //ball represents the ball
}
if(move == 3)
{
ball.x = place.x + 1; // place.x represents the ai controlled paddle
}
if(move == RIGHT)
{
ball.x = ball.x - 1;
}
if(ai == UP)
{
place.y = place.y + -1;
}
if(ai == 3)
{
place.x = place.y + -1;
}
if(ai == STOP)
{
place.y = place.y;
}
SDL_FillRect(screen,0,0);
SDL_BlitSurface(obj,NULL,screen,&ball);
SDL_Flip(screen);
SDL_PollEvent(&event);


//updates y position

if( ball.x > dest.x && ball.y < dest.y || ball.y > dest.y) // dest is another image
{

move = RIGHT;


}

SDL_FillRect(screen,0,0);
SDL_BlitSurface(obj,NULL,screen,&ball);
SDL_Flip(screen);

if( ball.x > place.x && ball.y < place.y || ball.y > place.y)
{

move = LEFT;
}
if(ball.x == place.x + motion.x)
{
if(ball.y < place.y)
{
ai = UP;
}
}
if(move == RIGHT)
{
ai = STOP;
}
SDL_FillRect(screen,0,0);
SDL_BlitSurface(red,NULL,screen,&place);
SDL_Flip(screen);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!