Hi, I’m looking for verification on the math I’m using for simple Pong-like physics for arbitrary convex polygons (i.e. collisions always involve a static structure). So here’s my solution:
- Two bodies collide one of which has a velocity vector and create an intersection
- The normal to the surface is found by finding the angle from the intersection center to the center of the moving object
- The velocity vector is reversed by rotating 180 degrees
- The new velocity vector is flipped over normal angle by adding the angle between the velocity vector and the normal to the normal.
Like this:
Here’s my code:
Rectangle me = shape.getBounds();
Rectangle him = ob.getShape().getBounds();
Rectangle col = me.intersection(him);
Point meCent = new Point((int)(me.getX()+me.getWidth()/2),(int)(me.getY()+me.getHeight()/2));
Point colCent = new Point((int)(col.getX()+col.getWidth()/2),(int)(col.getY()+col.getHeight()/2));
double normAngle = Math.atan2(meCent.getY()-colCent.getY(),meCent.getX()-colCent.getX());
double velAngle = Math.atan2(dy,dx);
double newAngle = normAngle+(normAngle-(velAngle-Math.PI));
//I know this is not the best way to push out of the collision
move(-5*Math.cos(velAngle), -5*Math.sin(velAngle));
dx = 50*Math.cos(newAngle);
dy = 50*Math.sin(newAngle);
I am fairly sure this is conceptually correct, but it never hurts to be sure, besides I got a few questions about it:
-Is this the most efficient way of doing things? (Specifically getting intersection and angles)
-Am I guaranteed that the normal angle will be correct (Assuming I never sink into a collision past the mid-point)
-Am I right in thinking that adding in force, friction, and collisions between 2 moving bodies would not be far off from this?
-Is there a way to easily/effeciently get perfect intersections rather than rectangular approximations
Thanks for the time