Collision Detection Technology
In the game, collisions are the most frequently encountered technology. Of course, many game engines have already done collision detection, and we just need to call them. This lesson is explained from the principle of collision, we have to achieve basic collision detection.
Principle of rectangle detection
In the game, whether more than one element touches together, in fact, is usually implemented with the principle of "rectangle detection". As we mentioned earlier, all objects in the game can be abstracted as "rectangles", so we just need to determine if the two rectangles intersect. For some complex polygons, irregular objects, in fact, he decomposed into multiple rectangles, continue to do the rectangle detection.
Java's API provides us with the rectangle class to represent information about rectangles, and provides a intersects () method that directly determines whether a rectangle intersects.
When we designed the base class in front of Gameobject, we added a method:
/**
* Returns the corresponding rectangular area of the object for subsequent use in collision detection
* @return
*/
Public Rectangle GetRect () {
return new Rectangle ((int) x, (int) y, width, height);
}
In other words, all objects in the game can get their own rectangular object.
Artillery and aircraft collision detection
Our game logic is: "The plane hit the shells, then death." In other words, we need to detect: "The plane and all the shells collide." If there are 50 shells, then 50 pairs are detected.
We modify the paint () method of the Mygameframe class, as shown in Example 1.
"Example 1" Mygameframe class: Increased collision detection
public void Paint (Graphics g) {
G.drawimage (bgimg, 0, 0, NULL);
Plane.drawmyself (g); Draw the plane itself
Draw all the bullets in the container
for (int i=0;i<shelllist.size (); i++) {
Shell B = Shelllist.get (i);
B.draw (g);
Rectangular inspection of aircraft and all projectiles
Boolean peng = B.getrect (). intersects (Plane.getrect ());
if (Peng) {
Plane.live = false; The plane dies, the screen doesn't show.
}
}
}
The above logic requires: When Plane.live=false, the plane disappears. So, we also need to modify the plane code.
"Example 2" plane class: Determine if the aircraft has disappeared according to the aircraft status
public void Drawmyself (Graphics g) {
if (live) {
Super.drawmyself (g);
Calculates the aircraft's new coordinates according to the direction
if (left) {
X-= speed;
}
if (right) {
x + = speed;
}
if (UP) {
Y-= speed;
}
if (down) {
Y + = speed;
}
}
}
In this way, when the program is run, a collision of shells and planes occurs, and the plane disappears, as shown in result 1:
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/9D/B1/wKiom1mEE3iwB7qLAAGDTKvaLm0601.png-wh_500x0-wm_ 3-wmp_4-s_533465669.png "title=" Figure 1.png "alt=" wkiom1mee3iwb7qlaagdtkvalm0601.png-wh_50 "/> 650) this.width=650;" Src= "Https://s5.51cto.com/wyfs02/M02/9D/B1/wKioL1mEE2HCfaGdAAGDTKvaLm0728.png-wh_500x0-wm_3-wmp_4-s_ 1924396513.png "title=" Figure 1.png "alt=" Wkiol1mee2hcfagdaagdtkvalm0728.png-wh_50 "/>
"Full stack Java notes" is a can help you from Zeto long for the full stack of Java Engineer Series of notes. The author is called Mr. G,10 years Java research and development experience, has been in the digital, Aerospace Institute of a Research and development center engaged in software design and research and development work, from small white gradually achieve engineers, senior engineers, architects. Proficient in Java Platform Software development, Master Java EE, familiar with various popular development framework.
The notes contain six parts from shallow into deep:
A-java Introductory Phase
B-Database from beginner to proficient
C-hand Edge mobile front end and web front end
D-j2ee from understanding to combat
E-java Advanced Frame Fine Solution
F-linux and Hadoop
This article is from the "full stack Java Notes" blog, be sure to keep this source http://javanew.blog.51cto.com/12931675/1953574
13.7-Full Stack Java notes: Flying Game Project | rectangle|intersects| Plane