• Advertisement

Archived

This topic is now archived and is closed to further replies.

Collision Probs

This topic is 5533 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

Hi, I''ve been working on a Pong game, and managed to implement the collision successfully. However, I tried to make sure it would add to the player score when it went off-screen, but instead it freezes or does funny things. Here''s the source:
  
bool collide(float batheight,float ballwidth,bool playeroropp)
{
 if (playeroropp==true)
 {
 if (ball.x>opp.x-0.25 && ball.y>opp.y-(batheight/2) && ball.y<opp.y+(batheight/2))
 {
  return true;
 }else{
 return false;
 }
 }

 if (playeroropp==false)
 {
  if (ball.x<player.x+0.25 && ball.y>player.y-(batheight/2) && ball.y<player.y+(batheight/2))
  {
   return true;
   }else{
   return false;
  }
 }
}

void UpdateBall()
{
  if (ball.y<-2.4f || ball.y>2.2f)
  {
   ball.speedy=-ball.speedy;
  }

  ball.x+=ball.speedx;

  ball.y+=ball.speedy;

  if (collide(0.6f,0.1f,true) && ball.x>3.2)
  {
    ball.speedx=-ball.speedx;
    }else{
    ball.x=-3.2;
    ball.y=1.6;

   player.score+=10;
  }

  if (collide(0.6f,0.1f,false) && ball.x<-3.3)
  {
    ball.speedx=-ball.speedx;
    }else{
   ball.x=-3.2;
   ball.y=1.6;

   opp.score+=10;
  }
}
  
The Collide function needs a lot of omtipmizing. Any ideas? Thanks in advance, CloudNine

Share this post


Link to post
Share on other sites
Advertisement
well, what you should do to simplify things is make it all one function. here''s what it should do:
if ball_x -if collision
-reverse speed
-else
-output new score
if ball_x>maximum
-if coliision
-revrse speed
-else
-output new score
and to check for a collision all you have to do is use simple bounding box colliision like you already have set up. don''t make it more complicated than it needs to be.

My Homepage
Everything is relative. Even stupidity.

Share this post


Link to post
Share on other sites
it might have something to do with how you''re reseting the ball. if it''s not getting set corectly, than it could do weird things.

My Homepage
Everything is relative. Even stupidity.

Share this post


Link to post
Share on other sites
Here''s my new code. Im convinced I''m setting the ball right


  
void UpdatePlayers()
{
if (keys[VK_UP])
{
player.y+=0.005f;
}

if (keys[VK_DOWN])
{
player.y-=0.005f;
}

if (player.y>1.7f)
{
player.y=1.7f;
}

if (player.y<-1.9f)
{
player.y=-1.9f;
}

if (keys[''A''])
{
opp.y+=0.005f;
}

if (keys[''Z''])
{
opp.y-=0.005f;
}

if (opp.y>1.7f)
{
opp.y=1.7f;
}

if (opp.y<-1.9f)
{
opp.y=-1.9f;
}
}

void UpdateBall()
{
if (ball.y<-2.4f || ball.y>2.2f)
{
ball.speedy=-ball.speedy;
}

ball.x+=ball.speedx;

ball.y+=ball.speedy;

if (ball.x>3.2)
{
if (ball.x>opp.x-0.25 && ball.y>opp.y-0.3 && ball.y<opp.y+0.3)
{
ball.speedx=-ball.speedx;
}else{
ball.x=-2.6;
ball.y=1.6;
player.score+=10;
}

}

if (ball.x<-3.3)
{
if (ball.x<player.x+0.25 && ball.y>player.y-0.3 && ball.y<player.y+0.3)
{
ball.speedx=-ball.speedx;
}else{
ball.x=-2.6;
ball.y=1.6;
opp.score+=10;
}
}
}


I can''t seem to find any massive errors

Any ideas?

Share this post


Link to post
Share on other sites

  • Advertisement