ImmediatePhysics.GenerateContacts
Declaration
public static int GenerateContacts(ReadOnly<GeometryHolder> geom1, ReadOnly<GeometryHolder> geom2, ReadOnly<ImmediateTransform> xform1, ReadOnly<ImmediateTransform> xform2, int pairCount, NativeArray<ImmediateContact> outContacts, NativeArray<int> outContactCounts, float contactDistance);Параметры
| Параметр | Описание |
|---|---|
| geom1 | Массив, содержащий первый член каждой пары объектов GeometryHolder с назначенными формами. |
| geom2 | Массив, содержащий второй член каждой пары объектов GeometryHolder с назначенными формами. |
| xform1 | Массив, содержащий первый член каждой пары ImmediateTransforms. |
| xform2 | Массив, содержащий второй член каждой пары ImmediateTransforms. |
| pairCount | Количество пар, предоставленных в массивах ,GeometryHolder, и ,ImmediateTransform,. |
| outContacts | Выходный массив созданных контактов. |
| outContactCounts | Выходный массив, содержащий информацию о количестве контактов, созданных для каждой пары. |
| contactDistance | Расстояние, на котором контакты начинают генерироваться между парами. |
Возвращаемое значение
инт Возвращает общее число созданных точек контакта.
Описание
Создает точки контакта для всех заданных пар фигур. Хранит полученные контакты в массиве ImmediateContact, а количество контактов для каждой пары в массиве `outContactCounts`.
Этот пример кода определяет, сколько пар объектов PhysX должен ожидать в сцене. Вы можете установить определенное число для небольшой проверки контакта, или, если вы собираетесь запустить более сложную симуляцию, возможно, вы захотите реализовать широкофазную фильтрацию, чтобы выяснить, сколько пар объектов будут взаимодействовать. Самый простой и наименее эффективный вариант заключается в том, чтобы поместить все возможные пары тел в функцию GenerateContacts, которая существует в сцене. Этот пример показывает базовую настройку ручного добавления двух пар тел и проверки контактов между ними.
using System.Collections.Generic; using UnityEngine; using UnityEngine.LowLevelPhysics; using Unity.Collections;
public class ImmediatePhysics_GenerateContacts_Example : MonoBehaviour { private int m_NumberOfPairs = 2;
// Pull in a reference from the Editor to the Mesh Collider. [SerializeField] private MeshCollider m_Mesh;
List<Vector3> contactPoints = new List<Vector3>();
// There is no way to know how many contacts the shapes will generate, // so set a maximum value that should confidently fit all of them. int s_MaxContactCount = 100;
void Start() { // Create and set up geometry and transform arrays. NativeArray<GeometryHolder> geom1 = new NativeArray<GeometryHolder>(m_NumberOfPairs, Allocator.Temp); NativeArray<GeometryHolder> geom2 = new NativeArray<GeometryHolder>(m_NumberOfPairs, Allocator.Temp);
// Create a box shape that is 1 meter wide on each axis, and use that to construct a GeometryHolder object. BoxGeometry boxShape = new BoxGeometry(new Vector3(0.5f, 0.5f, 0.5f)); GeometryHolder box = GeometryHolder.Create(boxShape);
// Get the geometry of an existing collider. GeometryHolder convexMesh = m_Mesh.GeometryHolder;
NativeArray<ImmediateTransform> xForms1 = new NativeArray<ImmediateTransform>(m_NumberOfPairs, Allocator.Temp); NativeArray<ImmediateTransform> xForms2 = new NativeArray<ImmediateTransform>(m_NumberOfPairs, Allocator.Temp);
ImmediateTransform body1Transform = new ImmediateTransform { Position = new Vector3(0, 0, 0), Rotation = Quaternion.identity };
ImmediateTransform body2Transform = new ImmediateTransform { Position = new Vector3(0, 0.75f, 0), Rotation = Quaternion.Euler(new Vector3(45f, 0, 0)) };
// First pair is two boxes interacting. geom1[0] = box; geom2[0] = box;
xForms1[0] = body1Transform; xForms2[0] = body2Transform;
// Second pair is a box and a convex Mesh Collider. geom1[1] = convexMesh; geom1[2] = box;
xForms1[1] = body1Transform; xForms2[1] = body2Transform;
// This code re-uses the same transforms, which means that if all the bodies // were part of the same simulation, they would all overlap with each other. However, // because we are only generating contacts per pair, one pair of bodies does not affect // the other pairs.
// Create a place to hold the output contacts and their counts per pair. // There is no way to know how many contacts the shapes will generate, // so set a maximum value that should confidently fit all of them. NativeArray<ImmediateContact> contacts = new NativeArray<ImmediateContact>(s_MaxContactCount, Allocator.Temp); // The number of contactCounts is the same as the number of pairs. NativeArray<int> contactCounts = new NativeArray<int>(m_NumberOfPairs, Allocator.Temp);
// Finally, call the GenerateContacts function and put all the arrays in. // Keep note of the ReadOnly status of the input arrays.
var totalContacts = ImmediatePhysics.GenerateContacts(geom1.AsReadOnly(), geom2.AsReadOnly(), xForms1.AsReadOnly(), xForms2.AsReadOnly(), m_NumberOfPairs, contacts, contactCounts);
// It is now possible to check whether there were any contacts for the pairs. // First, iterate over the number of input pairs defined in m_NumberOfPairs. int idx = 0; for (int i = 0; i < m_NumberOfPairs; i++) { // Iterate over the number of contacts that were generated per each pair. for (int j = 0; j < contactCounts[i]; j++) { // Add the contact points to a list for visualization later. contactPoints.Add(contacts[idx].Point);
// The number of contact points per each pair can be variable, so // use a separate index to keep track of which contact to retrieve. idx++; } } } }