[Translation] irrlicht engine conflict detection and response

Source: Internet
Author: User
This is an example provided by the irrlicht SDK to demonstrate how to detect and respond to conflicts in irricht. There are 13 Other examples which will be translated one after another. I am very poor at English, but I am a little addicted to translation, so I will post it here. please correct me if there is any translation error or poor expression.

 

Example 7: collision detection and response
In this section, I will demonstrate how to perform collision detection in the irrlicht engine. There are three methods: Automatic Collision Detection, such as moving global coordinates when going up or down the stairs; manually picking up triangles and scene nodes.
DemoProgram:

Start!
For the sake of simplicity, let's take the example 2 that loads and displays the quake3 map and walk around and pick up triangles from it. In addition, in order to illustrate the scene node pickup, we put three more genie in it. The following code starts the engine and loads the quake3 map. No more explanation (see example 2 for details ).

# Include <irrlicht. h>
# Include <iostream>
Using namespace IRR;

# Pragma comment (Lib, "irrlicht. lib ")

Int main ()
{
// Let user select driver type
Video: e_driver_type drivertype; printf ("Please select the driver you want for this example: \ n" \ "(a) direct3d 9.0c \ n (B) direct3d 8.1 \ n (c) OpenGL 1.5 \ n "\" (d) software Renderer \ n (e) apfelbaum software Renderer \ n "\" (f) nulldevice \ n (otherkey) Exit \ n "); char I; STD: CIN> I; Switch (I) {Case 'A': drivertype = video :: edt_direct3d9; break; Case 'B': drivertype = video: edt_direct3d8; break; Case 'C': drivertype = video: edt_opengl; break; Case 'D ': drivertype = video: edt_software; break; Case 'E': drivertype = video: edt_software2; break; Case 'F': drivertype = video: edt_null; break; default: return 0 ;}
// Create Device
Irrlichtdevice * Device = createdevice (drivertype,
Core: dimension2d <s32> (640,480), 16, false );
If (device = 0) return 1; // cocould not create selected driver. video: ivideodriver * driver = device-> getvideodriver (); SCENE: iscenemanager * smgr = device-> getscenemanager (); device-> getfilesystem () -> addzipfilearchive (".. /.. /Media/map-20kdm2.pk3 ");

Scene: ianimatedmesh * q3levelmesh = smgr-> getmesh ("20kdm2. BSP ");
Scene: iscenenode * q3node = 0;

If (q3levelmesh)
Q3node = smgr-> addocttreescenenode (q3levelmesh-> getmesh (0 ));


So far, we have loaded the quake3 map as in example 2. The following is different: we have created a triangle selector. This is a class that you can use to pick up triangles from scene nodes for different purposes, such as collision detection. There are several triangle selectors in the engine, which can be created using iscenemanager. In this example, we create octtreetriangleselector. You can literally understand it. Yes, this is a tree selector, and "Tree" is the tree in the data structure, we can optimize the output of the number of triangles. This is useful for maps like quake3.
After the triangle selector is created, it is connected to the scene node, which is q3node. It is not necessary to do this. This is a "lazy" approach. You don't have to worry about selector any more. For example, you don't need to release it.
Code ..................................
For example 2, in order to move in the quake3 map, we add an FPS (first person shooter first person shooting game) camera. However, this time, we need to add a special animator to the camera: an animation with conflicting responses (note 3 ). It will adjust the scene node associated with itself, so that it looks affected by gravity and will not pass through the wall. For animator, the only thing we need to do is to tell it what the world looks like, how big the scene nodes are, and how big the gravity is. After adding an animation with conflicting responses to the camera, you don't have to do anything about Collision Detection-everything is automatic !. The following information about Collision Detection Code Is used to pick up. Another nice and cool feature: you can attach an animation with conflicting responses to any scene node, not just a camera. In addition, it can be used together with other scenario node animator. In this way, it is very simple to use conflict detection and response in the irrlicht engine.
Let's take a look at the parameters of createcollisionresponseanimator. The first parameter is triangleselector, indicating what the world looks like after the collision detection is complete. The second parameter is the scene node, which specifies the object affected by collision detection. In our example, it is the camera. The third defines the size of the object, that is, the elliptical radius. Try: Can I bring the radius closer to the wall? The next parameter is the gravity size and direction. You can set the direction to (0, 0, 0) to eliminate gravity. The last one is only used for conversion: otherwise, the ball that calculates the collision detection will enclose the camera, and the camera is located in the center of the elliptical. But from the human perspective, we are used to "Eyes are above the body", and the eyes are in conflict with our real world. So we use this parameter to move the scene node up 50 units from the center of the elliptical. Okay. Now collision detection can work.
Code ...............................
Because Collision Detection in irrlicht is not a huge one (the engine has to do a lot of things, ). The next section describes how to use two different types of pickup. But before that, I will make some preparations for the scenario. Three roles need to be picked up: genie, a dynamic light source to illuminate them, a whiteboard to point out the intersection, and, oh, I have to remove the cursor. :)
Code .............................
In order not to be too complex, I will pick up in the rendering loop. We first define two pointers to save the current and last scene nodes, and then enter the loop.
Code .....................
After drawing a complete scene with smgr-> drawall (), we start to pick it up for the first time: we want to know which triangle we are looking at in the coordinates of the whole world. In addition, I want to know which point this is in the quake3 map. To this end, we create a three-dimensional RAY: Starting from the camera position, passing through the target point.
Then ask the collision Manager: Is this Ray colliding with a triangle stored in the triangle selector? If so, we draw a three-dimensional triangle and place the whiteboard on the intersection.
Code ...................
Another method supported by the irrlicht engine is to pick up Nodes Based on the box. Each scene node has an enclosed box. As a result, the pick-up speed is very fast, for example, when you pick up the scene points that the camera sees. Once again, we asked the collision manager to get this result. If we get a scene node, we can highlight it by canceling the illumination of the Material. Of course, if it is not a whiteboard or quake3 map.
Code ...................
In this case, all we need to do is complete the painting.
Code .....................
--End--of--file ―――――――――
Note:
1. Although it is a scientific text, it should be translated for ease of understanding.
2. For the original article, see irrlicht manual.

Related Article

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.