Skip to content

Instantly share code, notes, and snippets.

@andrew-raphael-lukasik
Last active January 14, 2024 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrew-raphael-lukasik/aa88cf6f32634126849dfe176f11a955 to your computer and use it in GitHub Desktop.
Save andrew-raphael-lukasik/aa88cf6f32634126849dfe176f11a955 to your computer and use it in GitHub Desktop.
System to inspect `PhysicsJoint` and `PhysicsConstrainedBodyPair` data

System to visually inspect PhysicsJoint and PhysicsConstrainedBodyPair data

Written for entities 1.0.16, physics 1.0.16

Screenshot 2023-11-29 165444

// src* https://gist.github.com/andrew-raphael-lukasik/aa88cf6f32634126849dfe176f11a955
using UnityEngine;
using Unity.Entities;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Physics;
using Unity.Physics.Extensions;
using Unity.Physics.Systems;
using Unity.Jobs;
namespace EditorOnly.Authoring
{
[Unity.Burst.BurstCompile]
[RequireMatchingQueriesForUpdate]
[WorldSystemFilter( WorldSystemFilterFlags.Default | WorldSystemFilterFlags.Editor )]
[UpdateInGroup( typeof(Unity.Physics.Authoring.PhysicsDisplayDebugGroup) )]
public partial struct PhysicsJointDebugSystem : ISystem
{
ComponentLookup<LocalToWorld> _lookupLTW;
[Unity.Burst.BurstCompile]
public void OnCreate ( ref SystemState state )
{
_lookupLTW = state.GetComponentLookup<LocalToWorld>( isReadOnly:true );
state.RequireForUpdate<SystemStartRequest>();
}
[Unity.Burst.BurstCompile]
public void OnDestroy ( ref SystemState state ) {}
[Unity.Burst.BurstCompile]
public void OnUpdate ( ref SystemState state )
{
_lookupLTW.Update( ref state );
state.EntityManager.CompleteDependencyBeforeRO<LocalToWorld>();
foreach( var( pj , pcb ) in SystemAPI.Query< RefRO<PhysicsJoint> , RefRO<PhysicsConstrainedBodyPair> >() )
{
Entity entityA = pcb.ValueRO.EntityA;
Entity entityB = pcb.ValueRO.EntityB;
var ltwA = _lookupLTW[entityA];
var ltwB = _lookupLTW[entityB];
var bodyAFromJoint = pj.ValueRO.BodyAFromJoint;
var bodyBFromJoint = pj.ValueRO.BodyBFromJoint;
Debug.DrawLine( ltwA.Position , ltwA.Value.TransformPoint(bodyAFromJoint.Position) , Color.cyan );
Debug.DrawRay( ltwA.Position , ltwA.Value.TransformDirection(bodyAFromJoint.Axis) , Color.red );
Debug.DrawRay( ltwA.Position , ltwA.Value.TransformDirection(bodyAFromJoint.PerpendicularAxis) , Color.green );
Debug.DrawLine( ltwB.Position , ltwB.Value.TransformPoint(bodyBFromJoint.Position) , Color.yellow );
Debug.DrawLine( ltwB.Position , ltwB.Value.TransformPoint(bodyBFromJoint.Axis) , Color.red );
Debug.DrawLine( ltwB.Position , ltwB.Value.TransformPoint(bodyBFromJoint.PerpendicularAxis) , Color.green );
}
}
public struct SystemStartRequest : IComponentData {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment