I'm currently working on a demo-type program involving bouncing objects. Currently, I have a rectangle that can bounce around the screen.
When the rectangle bounces against one of the walls imperfectly, part of it will clip into the side of the window (or other objects, in the future).
I'd like to have the rectangle bounce from an accurate position (as in when/where it begins to collide with the object). Is there a better or more efficient way to do this other than by going back a step and checking over again with smaller steps?
The relevant code is here, and for reference the rectangle's velocity is stored as an angle and speed.
//Update position
rect.setX(rect.getX() + (rect.getVelocity() * Math.cos(rect.getDirection())));
rect.setY(rect.getY() + (rect.getVelocity() * Math.sin(rect.getDirection())));
//Check for collisions
for(Collider c : colliders) {
if(c.isColliding(rect)) {
bounce(rect, c);
System.out.printf("Rect is colliding with %s%n", c);
}
}
Any help would be appreciated.