37
Physics and features in 2D and 3D Morten Skaaning @ Physics Team

Unite Boston 2015 Physics

Embed Size (px)

Citation preview

PowerPoint Presentation

Physics and features in 2D and 3DMorten Skaaning@ Physics Team

Performance tipsFeatures

2D Physics

2

Colliders are mostly immutable.Prefer to setup once (or be aware of cost)

Static CollidersOne shared bodyTransform each vertex of a modified ColliderRigidbody2D.isKinematic if moved2D Physics - Performance

3

Modifying Transform on Rigidbody2D

Changing PositionRebuilds contact points if RB is on GO.Recreates all Colliders if GO is child of RB.

Changing Rotation or Scale (or Size or Radius)Recreates Colliders!

2D Physics - Performance

4

Use NonAlloc queries to avoid garbage collection.

User-prepared fixed-size results container

LinecastNonAlloc, RaycastNonAlloc, GetRayIntersectionNonAlloc, CircleCastNonAlloc, BoxCastNonAlloc, OverlapPointNonAlloc, OverlapCircleNonAlloc, OverlapAreaNonAlloc

2D Physics - Performance

5

From 5.0 fast knowledge of touching Colliders.

IsTouchingIsTouchingLayers

Both are available on Physics2D, Collider2D & Rigidbody2D2D Physics - Performance

6

From 5.1Freeze PositionFreeze RotationAvoid locking all DoFs2D Physics - Constraints

7

FixedJoint2DFixes two Rigidbody2D together w. configurable spring2D Physics - New in 5.3

8

RelativeJoint2DKeeps two Rigidbody2D at their relative orientation using configurable maximum linear/angular forcesMore like motor than spring2D Physics - New in 5.3

9

TargetJoint2DSingle Rigidbody2DMoves the Rigidbody2D to Target position using configurable spring2D Physics - New in 5.3

Will move a rigid-body to a target position using a configurable maximum force and spring. It is the only joint which operates on a single rigid-body10

FrictionJoint2DApplies force and torque to reduce linear and angular velocities to zero2D Physics - New in 5.3

11

BuoyancyEffector2DApplies buoyancy, fluid-flow and drag forces to simulate fluid behavior.2D Physics - New in 5.3

12

Demo time

2D Physics - New in 5.3

13

3D PhysicsFixing joint issuesFeaturesFuture

14

Static Colliders (without Rigidbody)Does not simulate movement properlyUse Rigidbody w. isKinematic=true

Moving kinematic RigidbodiesSet Transform directly = teleportUse MovePosition(), MoveRotation(), Move() to impart velocity3D Physics General Rigidbody stuff

15

Fast moving RigidbodiesRigidbodies using Freeze RotationRagdolls3D Physics Joint issues

16

Fast moving Rigidbodies (1)

PhysX: One FixedUpdate delay: Contact discovered vs resolvedEnable Continuous Collision Detection (CCD)Increase contact offsetglobally: Physics.contactOffsetper-collider: Collider.contactOffset=> increases radius of ColliderCan change collision behavior slightly3D Physics Joint issues

17

Fast moving Rigidbodies (2)

Ragdoll? Convert to ragdoll earlierContinuous Collision Detection (CCD) enabled Rigidbodies ignore joints on high-speed impacts3D Physics Joint issues

18

Rigidbodies using Freeze Rotation (1)

Implemented as infinite inertiaWorks in local space!Can cause explosions:Switch off preprocessingJoint.enablePreprocessing = false

3D Physics Joint issues

19

Rigidbodies using Freeze Rotation (2)

Sliding when stackedIncrease Solver Iteration CountTry around 20 iterationsVery small perf impact

3D Physics Joint issues

20

Rigidbodies using Freeze Rotation (3)

Combining 2+ Freeze Rotation and HingeJoint currently makes PhysX crashPlease avoid Probably unneeded anyways

3D Physics Joint issues

21

Ragdolls (1)

All body parts should have about equal massAvoid small joint limit spans. Min 5 to 15 degree span.In extreme conditions, enable projectionhelps to keep limbs tightlinear & rotational projectionJoint.enableProjection = truebe careful with kinematics Rigidbodies

3D Physics Joint issues

22

Ragdolls (2)

In tight aiming sub-machine gun posesTry reducing Collider radius/sizeTry making the Joint limits widerTry lowering Rigidbody.maxDepenetrationVelocity

3D Physics Joint issues

23

Ragdolls (3)

Jitter: Try increasing Solver Iteration CountTry around 20Avoid setting body part TransformUse MovePosition, MoveRotation, Move insteadAvoid exotic scaling on body/jointNegative = badAll ones for debug3D Physics Joint issues

24

DocsJoint and ragdoll stabilityhttp://docs.unity3d.com/Manual/RagdollStability.htmlUpgrade guide from Unity4http://docs.unity3d.com/Manual/UpgradeGuide5-Physics.html

3D Physics Online resources

25

Expose Collision.impulseSum of impulse in first 16 contacts in Collider-Collider pair

public class Foo : MonoBehaviour {public void OnCollisionEnter(Collision collision) {Debug.Log(Collision impulse: + collision.impulse);}}

Collision callbacks needs more work to expose each contact without GC

3D Physics New in 5.2

26

Per call control of trigger / raycast interaction

enum Physics.QueryTriggerInteractionUseGlobal IgnoreCollide3D Physics New in 5.2

27

Oriented box queries!

Physics.CheckBoxPhysics.OverlapBoxPhysics.BoxCastPhysics.BoxCastAll3D Physics New in 5.3 (Alpha 4)

28

Non garbage allocating queries

RaycastNonAllocBoxCastNonAllocSphereCastNonAllocCapsuleCastNonAllocOverlapSphereNonAllocOverlapBoxNonAlloc3D Physics New in 5.3 (Alpha 4)

29

Mass distribution control

ExplicitRigidbody.centerOfMassRigidbody.inertiaTensorImplicitRigidbody.ResetCenterOfMass()Rigidbody.ResetInertiaTensor()3D Physics New in 5.3 (Alpha 4)

30

WheelCollider scaling support improved

Particles prioritize TerrainColliders over other Colliders

3D Physics New in 5.3 (Alpha 4)

31

HingeJoint orientationSame as Unity4Limits, TargetPosition

Rigidbody w. GO(pink)Clockwise/left-hand

Connected Body (grey)Counter-clockwise/right-hand

3D Physics New in 5.3 (Alpha 4)

The Rigidbody that share the GameObject with the HingeJoint will view the Joint anchor as a left-hand coordinate system. This means the Rigidbody will view positive angles as going clockwise around the Joint anchor.

The HingeJoint "Connected Body" will view the Joint anchor as a right-hand coordinate system. This means the Connected Body will view positive angles as going counter-clockwise around the Joint anchor.

32

Bugs (most of our time)CharacterController climb crazinessWheelColliderOnTriggerStay issuesSphereCast on scaled meshesPhysX on Android devicesRegressions

FeaturesShort term vs long term3D Physics roadmap

33

Collision geometry visualization

Overlay on scene view

Support large terrains

Feature set can grow

Plan to release public test this fall3D Physics roadmap features

34

HACD Solve Concave MeshCollider problemConvex decompositionInitially at import time3D Physics roadmap features

35

PhysX 3.3.3 (from 3.3.1)Collision detection, threading fixes

Further reduce garbage generationChange Collision.contacts to user-supplied array

Enable PhysX checked errorsEditor/Development only3D Physics roadmap features

36

Questions?

[email protected]

Twitter:morten_skaaningThank you for listening

37