### Get angle of bullet after bouncing

I'm trying to make a bullet bounce after it hits a wall. I think bouncing on the top and bottom wall works perfectly, but it won't bounce off of the left and right walls. How would I get it to bounce? This is how I get the direction the bullet it going whenever I shoot.

 12345 player.dir = GetAngle(player.mouseX, player.mouseY, player.x, player.y); float GetAngle(float x1, float y1, float x2, float y2) { return atan2(y1 - y2, x1 - x2); }

Also what I was trying to do to make the bullet bounce.

 12345 if (bullets[i].x < 10 || bullets[i].x > screenWidth - 10 || bullets[i].y < 10 || bullets[i].y > screenHeight - 10) { if (bullets[i].type == 8) bullets[i].dir *= -1; }
You don't need the angle explicitly:
http://en.wikipedia.org/wiki/Specular_reflection
Well I tried some more and this uh almost works...

 12345678910111213141516171819 if (bullets[i].x < 10 || bullets[i].x > screenWidth - 10 || bullets[i].y < 10 || bullets[i].y > screenHeight - 10) { if (bullets[i].type == 8) { if (bullets[i].dir < 0) bullets[i].dir += 1.5; else if (bullets[i].dir > 0) bullets[i].dir -= 1.5; else bullets[i].dir += 3; if (bullets[i].x < 10) bullets[i].x = 10; else if (bullets[i].x > screenWidth - 10) bullets[i].x = screenWidth - 10; if (bullets[i].y < 10) bullets[i].y = 10; else if (bullets[i].y > screenHeight - 10) bullets[i].y = screenHeight - 10; } }
Last edited on
I tried some more code and I still can't get it working right. It kinda works on 3 walls from the right angle.

 1234567891011121314151617181920212223242526272829303132 if (bullets[i].x < 10 || bullets[i].x > screenWidth - 10 || bullets[i].y < 10 || bullets[i].y > screenHeight - 10) { if (bullets[i].type == 8) { float velX, velY; POINT newPos; POINT newPos2; newPos.x = bullets[i].x + (bullets[i].speed * 2) * cos(bullets[i].dir); newPos.y = bullets[i].y + (bullets[i].speed * 2) * sin(bullets[i].dir); velX = abs(bullets[i].x - newPos.x); velY = abs(bullets[i].y - newPos.y); if (bullets[i].x < 10) { bullets[i].x = 10; velX *= -1; } else if (bullets[i].x > screenWidth - 10) { bullets[i].x = screenWidth - 10; velX *= -1; } if (bullets[i].y < 10) { bullets[i].y = 10; velY *= -1; } else if (bullets[i].y > screenHeight - 10) { bullets[i].y = screenHeight - 10; velY *= -1; } newPos2.x = newPos.x + (bullets[i].speed * 2) * cos(GetAngle(newPos.x, newPos.y, newPos2.x, newPos2.y)); newPos2.y = newPos.y + (bullets[i].speed * 2) * sin(GetAngle(newPos.x, newPos.y, newPos2.x, newPos2.y)); bullets[i].dir = GetAngle(newPos.x, newPos.y, newPos2.x, newPos2.y); } }
Topic archived. No new replies allowed.