Skip to content

Instantly share code, notes, and snippets.

@JamWils
Last active September 13, 2023 09:27
Show Gist options
  • Save JamWils/08d67b9b5e1b3eab0a8f0f03d30b9f00 to your computer and use it in GitHub Desktop.
Save JamWils/08d67b9b5e1b3eab0a8f0f03d30b9f00 to your computer and use it in GitHub Desktop.
This is some sample code on how to test Unity Physics properly, this uses version 1.0.14. This sets up a sphere collider and draw a ray through the center of it along the y-axis. Please note the `LocalTransform` component, this needs to be included for the collider to work properly in a unit test.
// Original credit: https://forum.unity.com/threads/testing-systems-performing-raycasts.1031335/
public class BaseSystemTest : ECSTestsFixture {
protected EntityManager entityManager;
protected SystemHandle physicsWorldSystem;
protected SystemHandle collisionWorldSystem;
[SetUp]
public override void Setup() {
base.Setup();
TestWorld.GetOrCreateSystem<BuildPhysicsWorld>();
var systems = new List<Type>() {
typeof(BeginInitializationEntityCommandBufferSystem),
typeof(EndInitializationEntityCommandBufferSystem),
typeof(BeginSimulationEntityCommandBufferSystem),
typeof(EndSimulationEntityCommandBufferSystem),
typeof(FixedStepSimulationSystemGroup),
typeof(BeginFixedStepSimulationEntityCommandBufferSystem),
typeof(EndFixedStepSimulationEntityCommandBufferSystem),
typeof(LateSimulationSystemGroup),
typeof(BeginPresentationEntityCommandBufferSystem),
typeof(PhysicsInitializeGroup),
typeof(PhysicsSimulationGroup),
typeof(PhysicsSystemGroup),
typeof(ExportPhysicsWorld),
};
entityManager = TestWorld.EntityManager;
DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(TestWorld, systems);
physicsWorldSystem = TestWorld.GetExistingSystem<BuildPhysicsWorld>();
collisionWorldSystem = TestWorld.GetExistingSystem<ExportPhysicsWorld>();
}
[Test]
public void RaycastTestSimplePasses() {
var entity = entityManager.CreateEntity(
typeof(PhysicsCollider),
typeof(PhysicsWorldIndex),
typeof(LocalTransform)
);
// Set the PhysicsCollider component to a sphere shape
entityManager.SetComponentData(entity, new PhysicsCollider {
Value = SphereCollider.Create(
new SphereGeometry {
Center = float3.zero,
Radius = 1f,
}
)
});
entityManager.SetComponentData(entity, LocalTransform.Identity);
entityManager.SetSharedComponent(entity, new PhysicsWorldIndex {
Value = 0,
});
physicsWorldSystem.Update(TestWorld.Unmanaged);
collisionWorldSystem.Update(TestWorld.Unmanaged);
entityManager.CompleteAllTrackedJobs();
var raycastInput = new RaycastInput {
Start = new float3(0f, -4f, 0f),
End = new float3(0f, 4f, 0f),
Filter = CollisionFilter.Default
};
var query = entityManager.CreateEntityQuery(typeof(PhysicsWorldSingleton));
var physicsWorldSingleton = query.GetSingleton<PhysicsWorldSingleton>();
bool didHit = physicsWorldSingleton.CastRay(raycastInput, out RaycastHit hit);
Assert.IsTrue(didHit);
Assert.AreEqual(entity, hit.Entity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment