This page has been translated automatically.
Видеоуроки
Интерфейс
Основы
Продвинутый уровень
Подсказки и советы
Основы
Программирование на C#
Рендеринг
Профессиональный уровень (SIM)
Принципы работы
Свойства (properties)
Компонентная Система
Рендер
Физика
Редактор UnigineEditor
Обзор интерфейса
Работа с ассетами
Контроль версий
Настройки и предпочтения
Работа с проектами
Настройка параметров ноды
Setting Up Materials
Настройка свойств
Освещение
Sandworm
Использование инструментов редактора для конкретных задач
Расширение функционала редактора
Встроенные объекты
Ноды (Nodes)
Объекты (Objects)
Эффекты
Декали
Источники света
Geodetics
World-ноды
Звуковые объекты
Объекты поиска пути
Player-ноды
Программирование
Основы
Настройка среды разработки
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Плагины
Форматы файлов
Материалы и шейдеры
Rebuilding the Engine Tools
Интерфейс пользователя (GUI)
Двойная точность координат
API
Animations-Related Classes
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
VR-Related Classes
Работа с контентом
Оптимизация контента
Материалы
Визуальный редактор материалов
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Учебные материалы

Обработка контактов при столкновении

Sometimes you may need to implement your own contact handler for colliding physical bodies. In this example, you will learn how to visualize contact points, display debug information, and add a hit effect (sparks) at the point of impact. Иногда вам может потребоваться реализовать собственный обработчик контактов для сталкивающихся физических тел. В этом примере вы узнаете, как визуализировать точки контакта, отображать отладочную информацию и добавлять эффект удара (искры) в точке столкновения.

Preparing the contentПодготовка контента#

To create sparks, we are going to use the VFX add-on (you can download it on https://store.unigine.com).Для создания искр будет использоваться дополнение VFX (его можно скачать на https://store.unigine.com).

Create a new project in UNIGINE SDK Browser and import the VFX add-on to it.Создайте новый проект в браузере UNIGINE SDK и импортируйте в него дополнение VFX.

Open the project in the UnigineEditor and add some primitive objects to the default scene.Откройте проект в UnigineEditor и добавьте несколько примитивных объектов в сцену по умолчанию.

Let's get some information about the contacts the blue box will have with other objects.Давайте получим некоторую информацию о контактах, которые синий кубик будет иметь с другими объектами.

Making the Objects CollidableСоздание объектов, с которыми возможно столкновение#

The objects in the scene are dynamic ones, so to be able to collide they need a body and a collision shape. Add a Rigid body and a shape to each object via the Physics tab of the Parameters window.Объекты в сцене являются динамическими, поэтому для того чтобы они могли с чем-нибудь столкнуться, им нужно тело и коллизионная форма. Добавьте Rigid body и форму к каждому объекту на вкладке Physics окна Parameters.

Collisions are available for static objects as well (like buildings or ground) — simply enable the Collision option for the corresponding surface.Коллизии доступны и для статических объектов (например, зданий или земли) — просто включите опцию Collision для соответствующей поверхности.

Enabling High Priority ContactsВключение контактов с высоким приоритетом#

On collision, contacts are distributed randomly between the interacting bodies to optimize performance: some are handled by the first body, others by the second. For a body, contacts that it handles itself are internal (access to them is fast), and contacts handled by other bodies are external.При столкновении контакты случайным образом распределяются между взаимодействующими телами для оптимизации производительности: некоторые обрабатываются первым телом, другие — вторым. Для тела контакты, которые оно обрабатывает само, являются внутренними (доступ к ним быстрый), а контакты, обрабатываемые другими телами, являются внешними.

Примечание
You can iterate through all contacts for a body, but for better performance it is recommended to process only internal ones.Вы можете перебирать все контакты для тела, но для повышения производительности рекомендуется обрабатывать только внутренние.

The box is a high-priority body for us and we want to track its collisions with maximum efficiency. We can make the box handle most of its contacts itself (so that most of them are internal). To do so, select the box and check High Priority Contacts in the Physics tab (or do it via code).Кубик является для нас высокоприоритетным телом, и мы хотим отслеживать его столкновения с максимальной эффективностью. Мы можем сделать так, чтобы кубик самостоятельно обрабатывал большинство своих контактов (так, чтобы большинство из них были внутренними). Для этого выберите кубик и включите опцию High Priority Contacts на вкладке Physics (или сделайте это через код).

Creating a Hit Effect NodeСоздание ноды эффекта попадания#

We will use a Particle System to create a node that simulates sparks.Мы будем использовать систему частиц для создания ноды, имитирующей искры.

  1. In the UnigineEditor, click Create -> Particle System -> Particles. Place the object somewhere in the world, rename it to sparks, and adjust its parameters:В UnigineEditor нажмите Create -> Particle System -> Particles. Поместите объект где-нибудь в мире, переименуйте его в sparks и измените его параметры:

    • Number Per Spawn = 10
    • Radius = 0.02
    • Life Time = 0.2
    • Period = inf
    • Duration = 0
  2. In the Surface Material section, assign library_spark1.mat material (it is located in the data -> vfx -> materials -> library_vfx folder of your project).В разделе Surface Material назначьте материал library_spark1.mat (он находится в папке data -> vfx -> materials -> library_vfx вашего проекта).
  3. Switch to the data folder in the Asset Browser. Right-click the sparks node and select Create a Node Reference. A sparks.node file will be generated in the data folder.Перейдите в папку data в браузере ассетов. Щелкните правой кнопкой мыши на ноде sparks и выберите Create a Node Reference. В папке data будет создан файл sparks.node.
  4. Delete the sparks object from the scene, we no longer need it since it will be loaded via code.Удалите объект sparks из сцены, он нам больше не нужен, так как он будет загружен с помощью кода.

Algorithm DescriptionОписание алгоритма#

The algorithm we are going to use can be described as follows:Алгоритм, который мы собираемся использовать, можно описать следующим образом:

  1. We create three variables (lastContactTime, lastContactPoint, lastContactInfo) to keep the time and position of the last occurred contact and some info about it.Мы создаем три переменные (lastContactTime, lastContactPoint, lastContactInfo), чтобы сохранить время и местоположение последнего произошедшего контакта и некоторую информацию о нем.
  2. We subscribe for an event to perform some actions when each contact emerges. The handler function (OnContactEnter()) takes the body and the index of the contact.Мы подписываемся на событие, чтобы выполнять некоторые действия при возникновении каждого контакта. Функция обработчика (OnContactEnter()) принимает тело и индекс контакта.
  3. If the contact is internal, we save its time and position.Если контакт внутренний, мы сохраняем его время и местоположение.
  4. We get both physical bodies participating in this contact (body0, body1). We check if our box has hit another physical object.Мы получаем оба физических тела, участвующих в этом контакте (body0, body1). Мы проверяем, ударил ли наш кубик другой физический объект.

    • If any of body0 and body1 exists (and it is not the body of our box), then we have found this object. We get details about this body and render it in the viewport using Visualizer.Если какой-либо из body0 и body1 существует (и это не тело нашего кубика), то мы нашли этот объект. Мы получаем подробную информацию об этом теле и отображаем его в окне просмотра, используя Визуализатор.
    • Otherwise, the box has hit some static object (a surface with the Collision option enabled). We make this surface highlighted in the viewport as well.В противном случае поле попало на какой-либо статический объект (поверхность с включенной опцией Collision). Мы также выделяем эту поверхность в окне просмотра.
  5. We add details we are interested in (e.g., the contact impulse).Мы добавляем интересующие нас детали (например, импульс контакта).
  6. We spawn a hit effect if the impulse is strong enough. Physics event handlers are called in the main thread, so it is safe to create nodes inside the OnContactEnter() function.Мы создаем эффект удара, если импульс достаточно силен. Обработчики физических событий вызываются в основном потоке, поэтому безопасно создавать ноды внутри функции OnContactEnter().
  7. Finally, in the Update() method we display the info and create a slow motion effect for one second.Наконец, в методе Update() мы отображаем информацию и создаем эффект замедленной съемки на одну секунду.

Component CodeКод компонента#

Let's use the C++ Component System to implement this logic. We will create a C++ component and assign it to the box node.Давайте воспользуемся Компонентной системой C++ для реализации этой логики. Мы создадим компонент C++ и назначим его ноде кубика.

  1. Open your project in an IDE.Откройте свой проект в среде IDE.

    Примечание
    It is recommended to use Visual Studio as a default IDE.Рекомендуется использовать Visual Studio в качестве IDE по умолчанию.
  2. In the IDE create a new C++ class (*.h and *.cpp files) and call it ContactsHandler. Make sure it inherits from the Unigine::ComponentBase class.В среде IDE создайте новый класс C++ (файлы *.h и *.cpp) и назовите его ContactsHandler. Он должен быть унаследован от класса Unigine::ComponentBase.
  3. Copy the code below, paste it to the corresponding files in your project and save them in your IDE.Скопируйте приведенный ниже код, вставьте его в соответствующие файлы в вашем проекте и сохраните их в вашей среде IDE.

    ContactsHandler.h (C++)
    #pragma once
    #include <UnigineComponentSystem.h>
    #include <UnigineGame.h>
    #include <time.h>
    
    class ContactsHandler :
    	public  Unigine::ComponentBase
    {
    public:
    	// declare constructor and destructor for our class and define a property name. 
    	COMPONENT_DEFINE(ContactsHandler, ComponentBase)
    		// declare methods to be called at the corresponding stages of the execution sequence
    		COMPONENT_INIT(init);
    	COMPONENT_UPDATE(update);
    
    	// A node that will be loaded on contact (a hit effect)
    	PROP_PARAM(File, hitEffect);
    
    	bool debug = true;
    
    protected:
    	void init();
    	void update();
    	void OnContactEnter(const Unigine::BodyPtr &, int);
    
    private:
    	// Time, position and some info of the last occurred contact
    	time_t lastContactTime;
    	Unigine::Math::vec3 lastContactPoint;
    	Unigine::String lastContactInfo;
    };
    ContactsHandler.cpp (C++)
    #include "ContactsHandler.h"
    #include <UnigineNode.h>
    #include <UnigineVisualizer.h>
    
    using namespace Unigine;
    using namespace Math;
    
    REGISTER_COMPONENT(ContactsHandler);
    
    void ContactsHandler::init()
    {
    	BodyPtr body = node->getObjectBody();
    	if (body)
    	{
    		// For debug purposes, we can render certain contacts depending on their type    
    		body->getEventContacts().connect([](const BodyPtr &b) {b->renderInternalContacts(); });
    		// subscription for contact event (when each contact emerges)
    		body->getEventContactEnter().connect(this, &ContactsHandler::OnContactEnter);
    	}
    }
    
    // This function takes the body and the index of the contact
    void ContactsHandler::OnContactEnter(const Unigine::BodyPtr &body, int num)
    {
    	// Enable Visualizer to see the rendered contact points
    	Visualizer::setEnabled(true);
    
    	if (body->isContactInternal(num))
    	{
    		if (debug)
    		{
    			// The time of the contact
    			lastContactTime = time(NULL);
    			// The position of the contact
    			lastContactPoint = body->getContactPoint(num);
    
    			// We get both physical bodies participating in this contact
    			BodyPtr body0 = body->getContactBody0(num);
    			BodyPtr body1 = body->getContactBody1(num);
    
    			BodyPtr touchedBody = NULL;
    
    			// We check if our object has hit another physical object.
    			// If any of the bodies exists and it's not the body of our object
    			// then we have found another physics-driven object that hit it
    			if (body0 && body0 != body) touchedBody = body0;
    			if (body1 && body1 != body) touchedBody = body1;
    
    			if (touchedBody)
    			{
    				// Our object has touched a physics-driven object.
    				// We save the info about the body
    				lastContactInfo = String::format("body %s of object %s ", touchedBody->getName(), touchedBody->getObject()->getName());
    				// Render it in the viewport
    				Visualizer::renderObject(touchedBody->getObject(), Math::vec4_blue, 0.5f);
    			}
    			else
    			{
    				// It has touched a surface with Collision enabled
    				lastContactInfo = String::format("surface #%d of object %s", body->getContactSurface(num), body->getContactObject(num)->getName());
    				// Highlighting the surface
    				Visualizer::renderObjectSurface(body->getContactObject(num), body->getContactSurface(num), Math::vec4_blue, 0.5f);
    			}
    
    			// You can add details you are interested in (e.g., the contact impulse)
    			lastContactInfo += String::format("\nimpulse: %f", body->getContactImpulse(num));
    		}
    
    		// We spawn a hit effect if the impulse is strong enough
    		if (body->getContactImpulse(num) > 0.3f && hitEffect)
    		{
    			NodePtr Effect = World::loadNode(hitEffect);
    			Effect->setPosition(Vec3(body->getContactPoint(num)));
    		}
    	}
    
    }
    
    void ContactsHandler::update()
    {
    	// Here we can display the info and create a slow motion effect for one second
    	if (debug)
    	{
    		if (int(time(NULL) - lastContactTime) < 1.0f)
    		{
    			// Slow motion effect
    			Game::setScale(0.3f);
    
    			Visualizer::renderMessage3D(lastContactPoint, Math::vec3_one, ("last contact: \n" + lastContactInfo).get(), Math::vec4_green, 2, 24);
    		}
    		else  // All the other time the speed will be normal
    		{
    			Game::setScale(1.0f);
    		}
    	}
    }
  4. Before you can use C++ components, you should initialize the Component System. Modify the init() method of the AppSystemLogic class as shown below (AppSystemLogic.cpp file).Прежде чем использовать компоненты C++, необходимо инициализировать систему компонентов. Измените метод init() класса AppSystemLogic, как показано ниже (файл AppSystemLogic.cpp).

    AppSystemLogic.cpp (C++)
    #include <UnigineComponentSystem.h>
    
    int AppSystemLogic::init()
    {
    	// initialize ComponentSystem and register all components
    	ComponentSystem::get()->initialize();
    
    	return 1;
    }
  5. Build and run the project via your IDE (press Ctrl + F5 in Visual Studio). The Component System will generate a property file (ContactsHandler.prop) in the data -> ComponentSystem folder of your project.Создайте и запустите проект через вашу среду IDE (нажмите Ctrl + F5 в Visual Studio). Система компонентов сгенерирует файл свойств (ContactsHandler.prop) в папке data -> ComponentSystem вашего проекта.

Applying Logic to the ObjectПрименение логики к объекту#

Switch back to the UnigineEditor and select the box node. Click Add New Property in the Parameters window and assign the newly generated ContactsHandler property. Specify the sparks.node asset to be loaded and spawned on collision.Переключитесь обратно в UnigineEditor и выберите ноду box. Щелкните Add New Property в окне Parameters и назначьте вновь созданному свойству ContactsHandler. Укажите ассет sparks.node, который будет загружен и создан при столкновении.

Примечание
You might need to increase the physics FPS and the number of iterations for better quality during slowdown. You can do this in the Settings tab of the UnigineEditor.Возможно, вам потребуется увеличить FPS физики и количество итераций для улучшения качества во время замедления. Вы можете сделать это на вкладке Settings в UnigineEditor.

Click Run to see the result. As the box is rolling around, the physics-driven objects and collidable surfaces it hits along the way are highlighted, and contact details are displayed near the contact point. Whenever a collision occurs, it triggers an asset spawning and a slow motion effect.Нажмите Run, чтобы увидеть результат. Когда кубик вращается, объекты, управляемые физикой, и поверхности, с которыми он сталкивается по пути, подсвечиваются, а информация о контактах отображается рядом с точкой контакта. Всякий раз, когда происходит столкновение, это запускает запуск ассета и эффект замедленной съемки.

Примечание
All manipulations with physical bodies should belong to the UpdatePhysics() method.Все манипуляции с физическими телами должны принадлежать методу UpdatePhysics().

This is how you can easily track collisions and run necessary logic on time: spawn particles, play sounds, and destroy fracture objects.Таким образом, вы можете легко отслеживать столкновения и вовремя запускать необходимую логику: создавать частицы, воспроизводить звуки и убирать разрушаемые объекты.

Последнее обновление: 27.02.2024
Build: ()