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.

1
2
3
4
5
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.

1
2
3
4
5
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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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.