Difference between revisions of "W4 Collision System"

From Ciliz|W4
Line 21: Line 21:
  
 
== Collider management ==
 
== Collider management ==
 +
Any node has access to the collider management interface, which allows it to add, remove and find colliders by name.
  
 +
The number of colliders is not limited.
 +
 +
Note. Colliders must have unique names. When adding a collider with the same name as an existing one, a warning will be displayed in the console, and the existing collider will be destroyed.
 
=== Adding a collider ===
 
=== Adding a collider ===
 +
1) Adding a collider by passing arguments to the Collider class constructor:
 +
<syntaxhighlight lang="c++">
 +
template<typename... Args>
 +
cref<Collider> addCollider(Args&&... args);
 +
template<typename String, typename... Args>
 +
cref<Collider> addCollider(const String& name, Args&&... args);
 +
</syntaxhighlight>
  
 +
Code example:
 +
<syntaxhighlight lang="c++">
 +
// automatic generation of collider name
 +
cube->addCollider(make::sptr<core::AABB>(1.f));
 +
// collider name set manually
 +
cube->addCollider("Intersect", make::sptr<core::AABB>(1.f));
 +
</syntaxhighlight>
 +
2) Adding a collider by specifying the BoundingVolume type and passing arguments to the constructor of the given type T:
 +
<syntaxhighlight lang="c++">
 +
template<typename T, typename... Args>
 +
cref<Collider> addCollider(Args&&... args);
 +
template<typename T, typename String, typename... Args>
 +
cref<Collider> addCollider(const String& name, Args&&... args);
 +
</syntaxhighlight>
 +
Code example:
 +
<syntaxhighlight lang="c++">
 +
// automatic generation of collider name
 +
cube->addCollider<core::AABB>(1.f);
 +
// collider name set manually
 +
cube->addCollider<core::AABB>("Intersect", 1.f);
 +
</syntaxhighlight>
 +
All options give identical results:
 +
* collider is an Axis-aligned Bounding Box with a side size of 1;
 +
* collider with an automatically generated name added in the first case, or "Intersect" in the second.
 
=== Getting a collider ===
 
=== Getting a collider ===
  

Revision as of 13:25, 29 July 2020

Scope

The collision system provides the handling of intersections between objects. For one object, you can set several different volumes (bounding boxes, spheres) at once and subscribe to intersections.

The W4 Engine implements the following types of interactions with colliders:

  • Intersect - intersection of two colliders.
  • Raycast - collider and ray intersection.
  • Screencast - intersection of the collider and the ray emitted from the given screen coordinates.

The following examples of the working code of the collision system can be found in the W4 Engine:

  • apps/internal/gist-colliding
  • apps/internal/test-screencast-blocking
  • apps/samples/gist-raycast

Let's take a closer look at the Collision system. Consider the main elements of the Collision system:

  • BoundingVolume - interface. This is a visitor whose descendants implement methods for calculating intersections. Also BoundingVolume contains local Transform.
  • CollisionInfo - structure containing an intersection data. The data depends on the type of interaction and the types of interacting volumes.
  • Collider - class containing BoundingVolume and callback management mechanisms.
  • ColliderEventDispatcher - utility static class containing arrays of colliders, distributed by type of interaction. This class also contains general onUpdate event handling methods for calculating Intersect and onTouch (Screencast calculation).
  • Node (see W4_Coordinate_system_and_Node_structure) contains the collider management interface.

Collider management

Any node has access to the collider management interface, which allows it to add, remove and find colliders by name.

The number of colliders is not limited.

Note. Colliders must have unique names. When adding a collider with the same name as an existing one, a warning will be displayed in the console, and the existing collider will be destroyed.

Adding a collider

1) Adding a collider by passing arguments to the Collider class constructor:

template<typename... Args>
cref<Collider> addCollider(Args&&... args);
template<typename String, typename... Args>
cref<Collider> addCollider(const String& name, Args&&... args);

Code example:

// automatic generation of collider name
cube->addCollider(make::sptr<core::AABB>(1.f));
// collider name set manually 
cube->addCollider("Intersect", make::sptr<core::AABB>(1.f));

2) Adding a collider by specifying the BoundingVolume type and passing arguments to the constructor of the given type T:

template<typename T, typename... Args>
cref<Collider> addCollider(Args&&... args);
template<typename T, typename String, typename... Args>
cref<Collider> addCollider(const String& name, Args&&... args);

Code example:

// automatic generation of collider name
cube->addCollider<core::AABB>(1.f);
// collider name set manually 
cube->addCollider<core::AABB>("Intersect", 1.f);

All options give identical results:

  • collider is an Axis-aligned Bounding Box with a side size of 1;
  • collider with an automatically generated name added in the first case, or "Intersect" in the second.

Getting a collider

Removing a collider

The Collider class

This class implements control of collider interactions

Bounding Volume

Local Transform

Intersect

Screencast

Reaction to TouchEvent

Raycast