I have a big red box and a blue box that needs to collide with each other, the left and right of the box bounced just fine. But for the top and bottom boxes, I couldn't figure out the right code of which I need to write for it to collide. I could do with another code but that bounces to where it came from instead of the alternate direction. So I'm settled with this. I just need to know how it'll work. Any help will be appreciated.
EDIT: Turns out, since using player.Bounds.Intersectswith on the Y-axis conflicts on the 1st timer, I have to use a 2nd timer for it to work. I got the output I wanted.
int enemyL = -10;
int enemyT = 10;
int playerL = 30;
int playerT = -30;
private void mainTimer_Tick(object sender, EventArgs e)
{
//player.Left += playerL;
player.Top += playerT;
//enemy.Left += enemyL;
enemy.Top += enemyT;
if (player.Left < 1 || player.Left + player.Width > ClientSize.Width - 2 || (player.Bounds.IntersectsWith(enemy.Bounds)))
{
playerL = -playerL;
}
if (player.Top < 1 || player.Top + player.Height > ClientSize.Width - 2)
{
playerT = -playerT;
}
if (enemy.Left < 1 || enemy.Left + enemy.Width > ClientSize.Width - 2 || (enemy.Bounds.IntersectsWith(player.Bounds)))
{
enemyL = -enemyL;
}
if (enemy.Top < 1 || enemy.Top + enemy.Height > ClientSize.Width - 2)
{
enemyT = -enemyT;
}