.NET PhysX 5 binding to all platforms(win, osx, linux) for 3D engine, deep learning, dedicated server of gaming.

Overview

MagicPhysX

NuGet GitHub Actions Releases

.NET PhysX 5 binding to all platforms(win-x64, osx-x64, osx-arm64, linux-x64, linux-arm64) for 3D engine, deep learning, dedicated server of gaming. This library is built on top of NVIDIA PhysX 5 and physx-rs.

Use case:

  • 3D View for MAUI, WPF, Avalonia
  • Physics for Your Own Game Engine
  • Simulate robotics for deep learning
  • Server side physics for dedicated server of gaming

Getting Started

PhysX Binding provides all of PhysX feature through C API. This library is distributed via NuGet.

PM> Install-Package MagicPhysX

C API is provided in NativeMethods in MagicPhysX namespace. Methods are almostly prefixed phys_Px or Px. In addition, extension methods are defined for contexts, just like object-oriented methods. For example, for PxPhysics*:

PxPhysics* physics = physx_create_physics(foundation);

// C API
PxScene* scene1 = PxPhysics_createScene_mut(physics, &sceneDesc);

// Extension methods
PxScene* scene2 = physics->CreateSceneMut(&sceneDesc);

Extension methods API is simpler and similar as original C++ PhysX API.

Here is the simple bound sphere on plane sample.

using MagicPhysX; // for enable Extension Methods.
using static MagicPhysX.NativeMethods; // recommend to use C API.

// create foundation(allocator, logging, etc...)
var foundation = physx_create_foundation();

// create physics system
var physics = physx_create_physics(foundation);

// create physics scene settings
var sceneDesc = PxSceneDesc_new(PxPhysics_getTolerancesScale(physics));

// you can create PhysX primitive(PxVec3, etc...) by C# struct
sceneDesc.gravity = new PxVec3 { x = 0.0f, y = -9.81f, z = 0.0f };

var dispatcher = phys_PxDefaultCpuDispatcherCreate(1, null, PxDefaultCpuDispatcherWaitForWorkMode.WaitForWork, 0);
sceneDesc.cpuDispatcher = (PxCpuDispatcher*)dispatcher;
sceneDesc.filterShader = get_default_simulation_filter_shader();

// create physics scene
var scene = physics->CreateSceneMut(&sceneDesc);

var material = physics->CreateMaterialMut(0.5f, 0.5f, 0.6f);

// create plane and add to scene
var plane = PxPlane_new_1(0.0f, 1.0f, 0.0f, 0.0f);
var groundPlane = physics->PhysPxCreatePlane(&plane, material);
scene->AddActorMut((PxActor*)groundPlane, null);

// create sphere and add to scene
var sphereGeo = PxSphereGeometry_new(10.0f);
var vec3 = new PxVec3 { x = 0.0f, y = 40.0f, z = 100.0f };
var transform = PxTransform_new_1(&vec3);
var identity = PxTransform_new_2(PxIDENTITY.PxIdentity);
var sphere = physics->PhysPxCreateDynamic(&transform, (PxGeometry*)&sphereGeo, material, 10.0f, &identity);
PxRigidBody_setAngularDamping_mut((PxRigidBody*)sphere, 0.5f);
scene->AddActorMut((PxActor*)sphere, null);

// simulate scene
for (int i = 0; i < 200; i++)
{
    // 30fps update
    scene->SimulateMut(1.0f / 30.0f, null, null, 0, true);
    uint error = 0;
    scene->FetchResultsMut(true, &error);

    // output to console(frame-count: position-y)
    var pose = PxRigidActor_getGlobalPose((PxRigidActor*)sphere);
    Console.WriteLine($"{i:000}: {pose.p.y}");
}

// release resources
PxScene_release_mut(scene);
PxDefaultCpuDispatcher_release_mut(dispatcher);
PxPhysics_release_mut(physics);

Other samples(FixedJoint, DistanceJoint, SphericalJoint, RevoluteJoint,RaycastSingle, BricksDoubleDomino, Geometries) are exist in ConsoleSandbox.

Document

MagicPhysX uses physx-rs C binding(physx-sys). You can refer these document.

PhysX Visual Debugger

MagicPhysX can enable PhysX Visual Debugger to debug physcs scene.

image

To use pvd, add this instruction on scene init.

var foundation = physx_create_foundation();

// create pvd
var pvd = phys_PxCreatePvd(foundation);

fixed (byte* bytePointer = "127.0.0.1"u8.ToArray())
{
    var transport = phys_PxDefaultPvdSocketTransportCreate(bytePointer, 5425, 10);
    pvd->ConnectMut(transport, PxPvdInstrumentationFlags.All);
}

// create physics
uint PX_PHYSICS_VERSION_MAJOR = 5;
uint PX_PHYSICS_VERSION_MINOR = 1;
uint PX_PHYSICS_VERSION_BUGFIX = 3;
uint versionNumber = (PX_PHYSICS_VERSION_MAJOR << 24) + (PX_PHYSICS_VERSION_MINOR << 16) + (PX_PHYSICS_VERSION_BUGFIX << 8);

var tolerancesScale = new PxTolerancesScale { length = 1, speed = 10 };
var physics = phys_PxCreatePhysics(versionNumber, foundation, &tolerancesScale, true, pvd, null);

phys_PxInitExtensions(physics, pvd);

var sceneDesc = PxSceneDesc_new(PxPhysics_getTolerancesScale(physics));
sceneDesc.gravity = new PxVec3 { x = 0.0f, y = -9.81f, z = 0.0f };

var dispatcher = phys_PxDefaultCpuDispatcherCreate(1, null, PxDefaultCpuDispatcherWaitForWorkMode.WaitForWork, 0);
sceneDesc.cpuDispatcher = (PxCpuDispatcher*)dispatcher;
sceneDesc.filterShader = get_default_simulation_filter_shader();

var scene = PxPhysics_createScene_mut(physics, &sceneDesc);

// pvd client
var pvdClient = scene->GetScenePvdClientMut();
if (pvdClient != null)
{
    pvdClient->SetScenePvdFlagMut(PxPvdSceneFlag.TransmitConstraints, true);
    pvdClient->SetScenePvdFlagMut(PxPvdSceneFlag.TransmitContacts, true);
    pvdClient->SetScenePvdFlagMut(PxPvdSceneFlag.TransmitScenequeries, true);
}

Toolkit Sample

C API is slightly complex in C# usage. Here is the sample of high level framework, PhysicsSystem.

using MagicPhysX.Toolkit;
using System.Numerics;

unsafe
{
    using var physics = new PhysicsSystem(enablePvd: false);
    using var scene = physics.CreateScene();

    var material = physics.CreateMaterial(0.5f, 0.5f, 0.6f);

    var plane = scene.AddStaticPlane(0.0f, 1.0f, 0.0f, 0.0f, new Vector3(0, 0, 0), Quaternion.Identity, material);
    var sphere = scene.AddDynamicSphere(1.0f, new Vector3(0.0f, 10.0f, 0.0f), Quaternion.Identity, 10.0f, material);

    for (var i = 0; i < 200; i++)
    {
        scene.Update(1.0f / 30.0f);

        var position = sphere.transform.position;
        Console.WriteLine($"{i:D2} : x={position.X:F6}, y={position.Y:F6}, z={position.Z:F6}");
    }
}

Code sample is available in MagicPhysX.Toolkit.

Native Build Instruction

require Rust.

Open directory src\libmagicphysx. Run cargo build.

Native binaries in package is built on GitHub Actions build-physx.yml. If we need to update physx-sys(PhysX) version, run this GitHub Actions to input physxversion.

License

This library is licensed under the MIT License.

You might also like...
A Rust curses library, supports Unix platforms and Windows

pancurses pancurses is a curses library for Rust that supports both Linux and Windows by abstracting away the backend that it uses (ncurses-rs and pdc

Design token framework — adopt a unified design language across platforms, codebases, and teams
Design token framework — adopt a unified design language across platforms, codebases, and teams

Palette Design tokens framework with atomic classes for React and Master CSS. Deliver a consistent visual identity across your apps with design tokens

The auto-managed -sys crate for Apple platforms using bindgen directly from build environment

apple-sys Apple platforms have a rather monotonous programming environment compared to other platforms. On several development machines, we will depen

Use Thunk to build your Rust program that runs on old Windows platforms, support Windows XP and more!

Use Thunk to build your Rust program that runs on old platforms. Thunk uses VC-LTL5 and YY-Thunks to build programs that support old platforms. So, ho

Tool that mirrors questions and resolutions from other forecasting platforms to Manifold.

Tool that mirrors questions and resolutions from other forecasting platforms to Manifold. Managram commands People can interact with the bot by sendin

A mono-repo for the Engineering Practice Domains of Development, Data, Infrastructure, Testing, and Platforms

Engineering Practice Domains Introduction Welcome to the Engineering Practice Domains at Fearless! This repository serves as a monorepo for our collab

TMM is a Linux native game modding tool. it allows to install and depoly mods for Linux native and wine games.

Tux Mod Manager TMM is a Linux native mod manager made with the Tauri toolkit. It can install, load, remove and deploy mods for both Linux native and

An implementation of a Windows Event Collector server running on GNU/Linux.

OpenWEC OpenWEC is a free and open source (GPLv3) implementation of a Windows Event Collector server running on GNU/Linux and written in Rust. OpenWEC

A GSL (the GNU Scientific Library) binding for Rust

rust-GSL A Rust binding for the GSL library (the GNU Scientific Library). The minimum support Rust version is 1.54. Installation This binding requires

Comments
  • Release Task

    Release Task

    • [x] Rust Native binary build
      • [x] x64 build
      • [x] use upload/download artifacts
      • [x] osx-arm, linux-arm
    • [x] samples high-level framework( MagicPhysX.Toolkit )
    • [x] unit test for all platforms
    • [x] GitHub Actions NuGet Publish
    opened by neuecc 0
Releases(1.0.0)
Owner
Cysharp, Inc.
Cysharp is a subsidiary of Cygames, that is focusing C# both .NET and Unity.
Cysharp, Inc.
ruborute is an interactive command-line tool to get asphyxia@sdvx gaming data.

ruborute Are you 暴龍天 ?. The ruborute is an interactive command-line tool to get asphyxia@sdvx gaming data. asphyxia-core/plugins: https://github.com/a

RinChanNOW! 9 Sep 28, 2022
Revolutionize handheld gaming with adaptive game settings. Optimize graphics and gameplay experience based on real-time system metrics. Open-source project empowering developers to enhance games on portable devices

Welcome to the server-side application for the HarmonyLink project. This innovative software is developed with the Rust programming language and is ai

Jordon Brooks 5 Jun 28, 2023
RustRedOps is a repository dedicated to gathering and sharing advanced techniques and malware for Red Team, with a specific focus on the Rust programming language. (In Construction)

RustRedOps In Construction.... The project is still under development Overview RustRedOps is a repository that houses various tools and projects relat

João Victor 17 Dec 14, 2023
Scan the symbols of all ELF binaries in all Arch Linux packages for usage of malloc_usable_size

Scan the symbols of all ELF binaries in all Arch Linux packages for usage of malloc_usable_size (-D_FORTIFY_SOURCE=3 compatibility)

null 3 Sep 9, 2023
A cli utility for playing music mixes for programming & focus from musicforprogramming.net

mfp: music for programming A command-line utility for playing music mixes for programming & focus (from musicforprogramming.net), unlocking the flow s

Rohit Gupta 10 Jul 12, 2023
Convert your .NET dependencies to the Directory.Packages.props format

directory-packages-props-converter Converts your projects to use Central Package Management. <PackageReference> dependencies have their Version remove

Jeroen Vannevel 18 Aug 7, 2023
This rust compiler backend emmits valid CLR IR, enambling you to use Rust in .NET projects

What is rustc_codegen_clr? NOTE: this project is a very early proof-of-concept This is a compiler backend for rustc which targets the .NET platform an

null 252 Sep 7, 2023
Command-line tool designed to simplify the process of managing multiple .NET SDK versions on your system

.NET Version Manager (dver) Overview dver is a command-line tool designed to simplify the process of managing multiple .NET SDK versions on your syste

Sergio Triana Escobedo 5 Aug 23, 2024
.NET assembly loader with patchless AMSI and ETW bypass in Rust

RustPatchlessCLRLoader The RustPatchlessCLRLoader leverages a sophisticated integration of patchless techniques for bypassing both Event Tracing for W

C2Pain 25 Oct 23, 2024
ddi is a wrapper for dd. It takes all the same arguments, and all it really does is call dd in the background

ddi A safer dd Introduction If you ever used dd, the GNU coreutil that lets you copy data from one file to another, then you may have encountered a ty

Tomás Ralph 80 Sep 8, 2022