mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 12:38:24 +01:00
samples: add Hello/Demo samples for most BRUTAL packages
Add samples/, a collection of single-file .NET 10 apps run with `dotnet run <file>.cs`. Hello/ has one minimal sample per package that has a public API, many of them headless; Demo/ has fuller programs that combine packages into a more interesting application. samples/README.md indexes every sample and covers the layout, error handling and per-OS requirements.
This commit is contained in:
parent
a25cdf80b2
commit
b0c9fd9cdc
1 changed files with 4599 additions and 1 deletions
98
samples/Hello/HelloJolt.cs
Normal file
98
samples/Hello/HelloJolt.cs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* HelloJolt
|
||||
*
|
||||
* Drops a box onto the ground with Jolt Physics and prints where it is as it falls and
|
||||
* comes to rest. See Demo/DemoJolt.cs for a version that draws a pile of boxes in 3D with OpenGL.
|
||||
*/
|
||||
|
||||
#:package Ahwoo.Brutal.Jolt@2026.10.2
|
||||
#:package Ahwoo.Brutal.Numerics@2026.10.1
|
||||
|
||||
using Brutal.JoltApi;
|
||||
using Brutal.Numerics;
|
||||
|
||||
const ushort STATIC_LAYER = 0;
|
||||
const ushort MOVING_LAYER = 1;
|
||||
|
||||
// Jolt needs an allocator, a factory and its types registered before anything else; Run creates the
|
||||
// job system and collision layers.
|
||||
Jolt.RegisterDefaultAllocator();
|
||||
Factory.Instance = Factory.Create();
|
||||
Jolt.RegisterTypes();
|
||||
|
||||
try
|
||||
{
|
||||
Run();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Jolt.UnregisterTypes();
|
||||
Factory.DestroyInstance();
|
||||
}
|
||||
|
||||
static void Run()
|
||||
{
|
||||
// Declared in dependency order, so they're disposed in reverse when Run returns.
|
||||
using var tempAllocator = TempAllocatorImpl.Create(10 * 1024 * 1024);
|
||||
using var jobSystem = JobSystemThreadPool.Create(2048, 8, -1);
|
||||
|
||||
// One broad phase layer per object layer; moving objects collide with static ones and each other.
|
||||
using var broadPhaseLayers = BroadPhaseLayerInterfaceTable.Create(2, 2);
|
||||
broadPhaseLayers.MapObjectToBroadPhaseLayer(STATIC_LAYER, 0);
|
||||
broadPhaseLayers.MapObjectToBroadPhaseLayer(MOVING_LAYER, 1);
|
||||
|
||||
using var pairFilter = ObjectLayerPairFilterTable.Create(2);
|
||||
pairFilter.EnableCollision(STATIC_LAYER, MOVING_LAYER);
|
||||
pairFilter.EnableCollision(MOVING_LAYER, MOVING_LAYER);
|
||||
|
||||
using var broadPhaseFilter = ObjectVsBroadPhaseLayerFilterTable.Create(broadPhaseLayers, 2, pairFilter, 2);
|
||||
|
||||
using var physicsSystem = PhysicsSystem.Create();
|
||||
physicsSystem.Init(1024, 0, 1024, 1024, broadPhaseLayers, broadPhaseFilter, pairFilter);
|
||||
var bodyInterface = physicsSystem.GetBodyInterface(); // Default gravity is (0, -9.81, 0).
|
||||
|
||||
// A static ground whose top surface is at y = 0.
|
||||
using var groundShape = BoxShapeSettings.Create(new float3(5, 0.5f, 5), 0.05f, default);
|
||||
using var groundSettings = BodyCreationSettings.Create(
|
||||
groundShape,
|
||||
new float3(0, -0.5f, 0),
|
||||
floatQuat.Identity,
|
||||
MotionType.Static,
|
||||
STATIC_LAYER
|
||||
);
|
||||
var ground = bodyInterface.CreateAndAddBody(groundSettings, Activation.DontActivate);
|
||||
|
||||
// A 1x1x1 dynamic box, 4 units above the ground.
|
||||
using var boxShape = BoxShapeSettings.Create(new float3(0.5f), 0.05f, default);
|
||||
using var boxSettings = BodyCreationSettings.Create(
|
||||
boxShape,
|
||||
new float3(0, 4, 0),
|
||||
floatQuat.Identity,
|
||||
MotionType.Dynamic,
|
||||
MOVING_LAYER
|
||||
);
|
||||
var box = bodyInterface.CreateAndAddBody(boxSettings, Activation.Activate);
|
||||
|
||||
// Two seconds at 60 Hz; the box should land and settle with its centre near y = 0.5.
|
||||
// (Jolt allows a couple of centimetres of penetration, so expect about 0.48.)
|
||||
const float TIME_STEP = 1.0f / 60.0f;
|
||||
for (var step = 0; step <= 120; step++)
|
||||
{
|
||||
if (step > 0)
|
||||
physicsSystem.Update(TIME_STEP, 1, tempAllocator, jobSystem);
|
||||
|
||||
if (step % 20 == 0)
|
||||
{
|
||||
var position = bodyInterface.GetPosition(box);
|
||||
Console.WriteLine(
|
||||
$"t = {step * TIME_STEP:F2}s position = ({position.X:F2}, {position.Y:F2}, {position.Z:F2}) active = {bodyInterface.IsActive(box)}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Bodies are IDs, not IDisposable: remove and destroy them while the physics system still exists.
|
||||
bodyInterface.RemoveBody(box);
|
||||
bodyInterface.DestroyBody(box);
|
||||
bodyInterface.RemoveBody(ground);
|
||||
bodyInterface.DestroyBody(ground);
|
||||
}
|
||||
Loading…
Reference in a new issue