Collision Detection in Java

Source: Internet
Author: User
Tags pow

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.