Skip to content

Commit

Permalink
Add examples project
Browse files Browse the repository at this point in the history
  • Loading branch information
BeanCheeseBurrito committed Jan 18, 2024
1 parent a61a227 commit b5af1b7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Box2D.NET.Examples/Box2D.NET.Examples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup>
<Example>Example</Example>
<StartupObject>$(Example)</StartupObject>
</PropertyGroup>

<Import Project="../Box2D.NET.Native/Box2D.NET.Native.targets"/>

<ItemGroup>
<ProjectReference Include="../Box2D.NET/Box2D.NET.csproj"/>
</ItemGroup>

</Project>
43 changes: 43 additions & 0 deletions Box2D.NET.Examples/CSharp/HelloWorld.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Box2D.NET.Bindings;

public static unsafe class HelloWorld
{
public static void Main()
{
// Create a world
B2.WorldDef worldDef = B2.DefaultWorldDef;
worldDef.gravity = new B2.Vec2 { x = 0, y = -9.81f };

B2.WorldId worldId = B2.CreateWorld(&worldDef);

// Create a body
B2.BodyDef bodyDef = B2.DefaultBodyDef;
bodyDef.type = B2.dynamicBody;

B2.BodyId bodyId = B2.CreateBody(worldId, &bodyDef);

// Create a shape
B2.Polygon box = B2.MakeBox(1, 1);
B2.ShapeDef shapeDef = B2.DefaultShapeDef;
shapeDef.friction = 0.6f;
shapeDef.density = 1.0f;

B2.ShapeId shapeId = B2.CreatePolygonShape(bodyId, &shapeDef, &box);

// Run the simulation
const float timeStep = 1.0f / 60.0f;
const int velocityIterations = 6;
const int positionIterations = 2;

for (int i = 0; i < 60; i++)
{
B2.WorldStep(worldId, timeStep, velocityIterations, positionIterations);
B2.Vec2 position = B2.BodyGetPosition(bodyId);
float angle = B2.BodyGetAngle(bodyId);

Console.WriteLine($"Position: ({position.x}, {position.y})");
Console.WriteLine($"Angle: {angle}");
Console.WriteLine();
}
}
}
8 changes: 8 additions & 0 deletions Box2D.NET.Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public static class Example
{
public static void Main()
{
Console.WriteLine("To run an example, use the following command and provide the full class name of your desired example");
Console.WriteLine("Example: \"dotnet run --project Box2D.NET.Examples --property:Example=HelloWorld\"");
}
}
6 changes: 6 additions & 0 deletions Box2D.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Box2D.NET.Native", "Box2D.N
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Box2D.NET.Tests", "Box2D.NET.Tests\Box2D.NET.Tests.csproj", "{F49CDACC-2515-4DB2-8FB9-64CB2618FA12}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Box2D.NET.Examples", "Box2D.NET.Examples\Box2D.NET.Examples.csproj", "{7E044479-E555-48C9-B0C3-757B0FCB2374}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -36,5 +38,9 @@ Global
{F49CDACC-2515-4DB2-8FB9-64CB2618FA12}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F49CDACC-2515-4DB2-8FB9-64CB2618FA12}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F49CDACC-2515-4DB2-8FB9-64CB2618FA12}.Release|Any CPU.Build.0 = Release|Any CPU
{7E044479-E555-48C9-B0C3-757B0FCB2374}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E044479-E555-48C9-B0C3-757B0FCB2374}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E044479-E555-48C9-B0C3-757B0FCB2374}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E044479-E555-48C9-B0C3-757B0FCB2374}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ For more up-to-date packages, development builds are available on the [GitLab pa
> [!WARNING]
> Development feed packages may be deleted without warning to free up space.
## Running examples
To run any of the example programs, use ``dotnet run``and set the `Example` property's value to the example's full class name.
**Example**:
```console
dotnet run --project Box2D.NET.Examples --property:Example=HelloWorld
```

## Building from source
### Clone the repo
Clone the repo and it's submodules.
Expand Down

0 comments on commit b5af1b7

Please sign in to comment.