Skip to main content
GameDev.net gamedev.net
🔒 Locked

Collision Probs

Started by CloudNine Dec 26, 2002 at 11:01 AM 4 replies 500+ views
Original Post
CloudNine
CloudNine
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
Julio
Julio
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.
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
Julio
Julio
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.
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
CloudNine
CloudNine
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?
CloudNine
CloudNine
Also, the players do funny things. If you press up, you won''t be able to press down

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.