Combine Physics Core 2D API shapes
To combine physics shapes into a compound shape, create a composer using a PhysicsComposer object.
Read time 3 minutesLast updated 13 days ago
To combine physics shapes into a compound shape, create a composer using a object. For example, create a car shape by combining a car body and two wheels.
PhysicsComposerFollow these steps:
-
At the top of your script, import thelibrary that contains the allocator APIs the composer uses:
Unity.Collectionsusing Unity.Collections; -
Create theshapes you want to combine. For more information, refer to Create a physics object.
Geometry -
Create a newobject. Pass in an allocator, which creates temporary memory for building up the combined object. For example:
PhysicsComposerPhysicsComposer physicsComposer = PhysicsComposer.Create(Allocator.Temp);For more information about allocators, refer to Allocator. -
Add each object to the composer as a layer, specifying its geometry, its position, and how to combine the shape with the existing layers. For example:physicsComposer.AddLayer(circleGeometry, PhysicsTransform.identity, PhysicsComposer.Operation.OR);For more information about, refer to the Layer operations section.
Operation -
Create the combined shape using. Pass in a scale, and an allocator that creates the memory to store the combined shape. For example:
CreatePolygonGeometryusing NativeArray<PolygonGeometry> combinedShape = composer.CreatePolygonGeometry(new Vector2(1f, 1f), Allocator.Temp); -
Create a body with the combined shape using themethod. For example:
CreateShapeBatchbody.CreateShapeBatch(combinedShape, PhysicsShapeDefinition.defaultDefinition);
The API combines , , , , or any closed region defined by a set of points.
PhysicsComposerCircleGeometryCapsuleGeometryPolygonGeometryPhysicsShapeVector2Layer operations
The following operations are available:
PhysicsComposer.Operation- : Adds the shape.
OR - : Keeps only the overlapping areas between the new shape and the existing shape.
AND - : Subtracts the new shape from the existing shapes.
NOT - : Removes overlapping areas, but keeps non-overlapping areas.
XOR
Example
The following example combines a circle and a capsule shape. Attach this script to a GameObject in a scene, then enter Play mode to check the combined shape.
using UnityEngine;using Unity.U2D.Physics;using Unity.Collections;public class CombineShapes : MonoBehaviour{ // Create the definition for a circle object public CircleGeometry MyCircleGeometry = CircleGeometry.defaultGeometry; public PhysicsTransform CircleTransform = new Vector2(0f, 0f); // Create the definition for a capsule object public CapsuleGeometry MyCapsuleGeometry = CapsuleGeometry.defaultGeometry; public PhysicsTransform CapsuleTransform = new Vector2(0.75f, 0f); private void Awake() { PhysicsWorld m_PhysicsWorld = PhysicsWorld.defaultWorld; // Create a composer to combine shapes PhysicsComposer composer = PhysicsComposer.Create(Allocator.Temp); // Add both shapes to the composer composer.AddLayer(MyCircleGeometry, CircleTransform); composer.AddLayer(MyCapsuleGeometry, CapsuleTransform, PhysicsComposer.Operation.OR); // Combine the shapes using NativeArray<PolygonGeometry> combinedShape = composer.CreatePolygonGeometry(new Vector2(1f, 1f), Allocator.Temp); // Create a body with the combined shapes PhysicsBody body = m_PhysicsWorld.CreateBody(); body.CreateShapeBatch(combinedShape, PhysicsShapeDefinition.defaultDefinition); // Destroy the composer to also destroy the temporary allocator. composer.Destroy(); } }