Collision test

Xtreme3D

Lesson 11
Collision test

Level: medium
Version of the Xtreme3D: 3.0.x
Author: Gecko

In the games very often, it is required to define the fact of collision between two objects. They can be, for example, the character and the Platform, shell and purpose, and so on. The collision detection is based logic, the shooters ������������, simulations, role-playing games and some of the Strategies. If this does not always want to find the exact intersection of two polygon ����� - enough to test the intersection of limiting their scope (Bounding Sphere) or ��������������� (Bounding Box). Xtreme3D includes easy-to-use tools that allow you to do this.

Function test collisions in the Xtreme3D begin with "ObjectCheck..." and operate on restricting the spheres and ����������������� objects, which are calculated using the engine automatically, depending on the volume, which is their geometry. Restricting the ��������������� (which in these functions are called the Cube) aligned to the local coordinate axes of the object - that is, can rotate with him. Such ��������������� is often called Oriented Bounding Box, or abbreviated OBB. Functions return true (1) If a crossing, and lie (0) otherwise.

Xtreme3D includes the following function test collisions:
ObjectCheckSphereVsSphere, ObjectCheckSphereVsCube, ObjectCheckCubeVsCube, ObjectCheckCubeVsFace, ObjectCheckFaceVsFace. The last two of them operate on the objects of the type of Freeform - accordingly, can detect the crossing of the parallelepiped bounding one object with the polygon model of the other, as well as the intersection of the two models. This test is rather slow, therefore, it is recommended to optimize its use - for example, to carry out an accurate test between the models only if the collision was detected between their limiting areas:

If ObjectCheckSphereVsSphere(obj1, obj2)
{
  If ObjectCheckFaceVsFace(obj1, obj2)
  {
    // Do something
  }
}

These functions are useful when you need to perform a discrete test - that is, when it can be argued that the objects are moving with small speeds. If the speed of high, and the object in one step of game time flies the distance greater than the size of another object, the discrete checking can easily fail. A universal solution to this problem so far, no, but there are a variety of simplified methods. The easiest method - "throwing rays" (Ray Casting). In the Xtreme3D have enough effective implementation of this method. From the center of the object beam is produced in the direction of the Direction of the object. Then the intersection with this ray checked the target object, one or a few. Thus, it is possible to simulate the movement of the bullets (under the assumption that it is moving with infinite speed) - instantly find the point at which it enters. Using the "throwing rays" can be used to determine the height of the land under the character that is necessary for realization of the jumps. In addition, this method is indispensable for the building of the logic of the interaction of the character with interactive objects and triggers - Imagine, for example, the shooter, RPG or a quest from the first person, where the player can pick up objects and to press on the levers, by clicking on them with the mouse. To do this, you can estimate the distance between the player and the object, and then apply "throwing rays":

If ObjectGetDistance(player item) <= 1.0
{
  If ObjectRaycast(player item)
  {
    Hit x = ObjectGetCollisionPosition(0);
    Hit_y = ObjectGetCollisionPosition(1);
    Hit_z = ObjectGetCollisionPosition(2);
  }
}

What is the most pleasant, "throwing rays" in the Xtreme3D is fully compatible with the objects of Freeform and gives correct results in any transformation of the objects.