|
|
@@ -3,66 +3,42 @@
|
|
|
<span class="badge text-bg-primary">Beginner</span>
|
|
|
<span class="badge text-bg-success">Designer</span>
|
|
|
|
|
|
-> [!WARNING]
|
|
|
-TODO
|
|
|
+A trigger is a collision shape which detects when colliders enter it, it can be used to run an event when a player character enters a room for example.
|
|
|
+
|
|
|
+Triggers in Stride's Bepu implementation fall into what is known as a special kind of Contact Event Handler.
|
|
|
|
|
|
-If you set a collider to be a **trigger**, other colliders no longer bump into it. Instead, they pass through.
|
|
|
+The contact event handler is a class that receives collision data whenever the object it is associated with collides with the world.
|
|
|
|
|
|
-The trigger detects when colliders enter it, which you can use to script events. For example, you can detect when a player character enters a room, and use this in your script to trigger an event. For more information, see [Events](../scripts/events.md).
|
|
|
-
|
|
|
->[!Note]
|
|
|
->[Character colliders](characters.md) can't be used as triggers.
|
|
|
-
|
|
|
-## Create a trigger
|
|
|
-
|
|
|
-1. Create a [collider](colliders.md).
|
|
|
-
|
|
|
-2. In the **Property Grid**, under the collider component properties, select **Is Trigger**.
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-## Detect trigger collisions
|
|
|
-
|
|
|
-You can see when something enters the trigger using the following code:
|
|
|
+If the contact event handler you bind to an object is set to `NoContactResponse`, it is a Trigger, the object will never prevent anything from passing through it, it will only receive collision events.
|
|
|
|
|
|
+Here's a basic example of a component which acts as a trigger to display a message in the console:
|
|
|
```cs
|
|
|
-// Wait for an entity to collide with the trigger
|
|
|
-var firstCollision = await trigger.NewCollision();
|
|
|
-
|
|
|
-var otherCollider = trigger == firstCollision.ColliderA
|
|
|
- ? firstCollision.ColliderB
|
|
|
- : firstCollision.ColliderA;
|
|
|
-```
|
|
|
+using Stride.BepuPhysics;
|
|
|
+using Stride.BepuPhysics.Definitions.Contacts;
|
|
|
+using Stride.Engine;
|
|
|
|
|
|
-Alternatively, directly access the `TrackingHashSet`:
|
|
|
-
|
|
|
-```cs
|
|
|
-var trigger = Entity.Get<PhysicsComponent>();
|
|
|
-foreach (var collision in trigger.Collisions)
|
|
|
+public class Test : StartupScript, IContactEventHandler
|
|
|
{
|
|
|
- //do something with the collision
|
|
|
-}
|
|
|
-```
|
|
|
-
|
|
|
-Or use the `TrackingHashSet` events:
|
|
|
+ public bool NoContactResponse => true;
|
|
|
|
|
|
-```cs
|
|
|
-var trigger = Entity.Get<PhysicsComponent>();
|
|
|
-trigger.Collisions.CollectionChanged += (sender, args) =>
|
|
|
-{
|
|
|
- if (args.Action == NotifyCollectionChangedAction.Add)
|
|
|
+ void IContactEventHandler.OnStartedTouching<TManifold>(CollidableComponent eventSource, CollidableComponent other,
|
|
|
+ ref TManifold contactManifold,
|
|
|
+ bool flippedManifold,
|
|
|
+ int workerIndex,
|
|
|
+ BepuSimulation bepuSimulation)
|
|
|
{
|
|
|
- //new collision
|
|
|
- var collision = (Collision) args.Item;
|
|
|
- //do something
|
|
|
+ Log.Warning("Entered!");
|
|
|
}
|
|
|
- else if (args.Action == NotifyCollectionChangedAction.Remove)
|
|
|
+
|
|
|
+ void IContactEventHandler.OnStoppedTouching<TManifold>(CollidableComponent eventSource, CollidableComponent other,
|
|
|
+ ref TManifold contactManifold,
|
|
|
+ bool flippedManifold,
|
|
|
+ int workerIndex,
|
|
|
+ BepuSimulation bepuSimulation)
|
|
|
{
|
|
|
- //old collision
|
|
|
- var collision = (Collision)args.Item;
|
|
|
- //do something
|
|
|
+ Log.Warning("Exited!");
|
|
|
}
|
|
|
-};
|
|
|
+}
|
|
|
```
|
|
|
|
|
|
For an example of how to use triggers, see the [Script a trigger](script-a-trigger.md) tutorial.
|
|
|
@@ -70,6 +46,5 @@ For an example of how to use triggers, see the [Script a trigger](script-a-trigg
|
|
|
## See also
|
|
|
|
|
|
* [Tutorial: Script a trigger](script-a-trigger.md)
|
|
|
-* [Colliders](colliders.md)
|
|
|
-* [Collider shapes](collider-shapes.md)
|
|
|
-* [Events](../scripts/events.md)
|
|
|
+* [Collidables](colliders.md)
|
|
|
+* [Collider shapes](collider-shapes.md)
|