1. Introduction:
Collision detection is the most basic and very important part of computer graphics and virtual reality. It is mainly used for: virtual manufacturing, CAD/CAM, computer animation, physical modeling, three-dimensional games, aircraft and vehicle control simulation, robotics, path and motion planning, assembly.
2. Collision Handling:
Collision Detection (Collision Detection): Boolean judgment that returns whether two or more objects collide.
Collision Determination (Collision determination): Find the actual intersection between objects.
Collision Response (Collision Response): determines what action to take for collisions between two objects.
/** * Collision detection test to determine if two of the circle will collide **/ Public classRectanglericlesdemo extends JFrame implements Runnable {Private StaticFinalLongSerialversionuid =1L; /*defines the upper-left corner coordinate, radius of the two circles*/ Private intX1 = the, y1 = $; Private intx2 = *, y2 = -; Private intR1 = -, r2 = -; PublicRectanglericlesdemo () {Settitle ("Collision Detection"); SetSize ( $, -); Setlocationrelativeto (NULL); Setdefaultcloseoperation (Exit_on_close); SetVisible (true); } @Override Public voidPaint (Graphics g) {/*Draw a circle*/g.drawoval (x1, y1,2* R1,2*R1); G.drawoval (x2, y2,2* R2,2*R2); } @Override Public voidrun () {/*determine if the two circles intersect*/ //Two Circle Center coordinates intcenterX1 = x1 + R1, centerY1 = y1 +R1; intCenterX2 = x2 + r2, centerY2 = y2 +R2; //Find the center distance of two circles DoubleLength = Math.sqrt (Math.pow (CENTERX1-CENTERX2,2) + Math.pow (Centery1-centery2,2)); //Judging the relationship between the center distance and the radius of two circles if(Length < (R1 +R2)) {Joptionpane.showmessagedialog (NULL,"Center Distance:"+ length +", it collided."); } Else{Joptionpane.showmessagedialog (NULL,"Center Distance:"+ length +", no collisions"); } } Public Static voidMain (string[] args) {NewThread (NewRectanglericlesdemo ()). Start (); }}
Collision Detection in Java