I'm trying to bounce a ball off of a wall based on angular movement. The ball comes in at angle X ( lets say 30 ). Bouncing off with the equation 360-ballangle works for the left and right walls 180-angle works for top and bottom walls.
Edit Sorry for the vagueness - heres whats going on When a ball hits a wall on any side, the ball bounces right back on the same angle . I'm not sure why it does this - I am trying to have the ball flip its X velocity on a horizontal collision, and flip its Y velocity on a verticle collision. Because I am using angular movement, I need to do this by modifying the ball's angle value, and I am not sure how to set it so it bounces correctly.
(wall.getDimensions returns a rectangle with the walls size and location)
public void moveBullet(Bullet b) {
float xpos = (float) (speed * 2 * Math.sin(Math.toRadians(b.angle)));
float ypos = (float) (speed * 2 * Math.cos(Math.toRadians(b.angle)));
Rectangle bulletbounds = new Rectangle((int) (b.bulletx + xpos), (int) (b.bullety + ypos), 10, 10);
for (Wall wall : walls) {
if (bulletbounds.intersects(wall.getDimensions())) {
if (b.bulletx + 10 > wall.getDimensions().x || b.bulletx < wall.getDimensions().x + wall.getDimensions().width) {
b.angle = (360 - b.angle);
}
if (b.bullety - 10 < wall.getDimensions().y + wall.getDimensions().height || b.bullety > wall.getDimensions().y) {
b.angle = (180 - b.angle);
}
}
}
xpos = (float) (speed * 2 * Math.sin(Math.toRadians(b.angle)));
ypos = (float) (speed * 2 * Math.cos(Math.toRadians(b.angle)));
b.bulletx += xpos;
b.bullety += ypos;
}
trying to combine them both
means? Do you mean when the objects contacts the floor and a wall at the same exact time? In that case, I usually just reverse both velocity components. \$\endgroup\$b.angle
, and which direction do you expect the object to be moving when angle == 0? Your code doesn't seem to conform to the standard unit circle, where normally x = cos(theta) and y = sin(theta). Example 1 Example 2 \$\endgroup\$