From b219ee830d402549d11a8bbbfdf4429467ff17f7 Mon Sep 17 00:00:00 2001 From: bloeys Date: Wed, 7 Dec 2022 03:31:35 +0400 Subject: [PATCH] Some initial work --- go.mod | 3 ++ go.sum | 6 ++++ main.go | 34 ++++++++++++++++++ physics/physx/physx.go | 80 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100755 physics/physx/physx.go diff --git a/go.mod b/go.mod index 95bff96..b815302 100755 --- a/go.mod +++ b/go.mod @@ -10,4 +10,7 @@ require ( github.com/bloeys/assimp-go v0.4.4 github.com/bloeys/gglm v0.43.0 github.com/inkyblackness/imgui-go/v4 v4.6.0 + github.com/bloeys/physx-go v0.1.2 ) + +// replace github.com/bloeys/physx-go => ../physx-go \ No newline at end of file diff --git a/go.sum b/go.sum index 5fbd52a..468bb10 100755 --- a/go.sum +++ b/go.sum @@ -2,6 +2,12 @@ github.com/bloeys/assimp-go v0.4.4 h1:Yn5e/RpE0Oes0YMBy8O7KkwAO4R/RpgrZPJCt08dVI github.com/bloeys/assimp-go v0.4.4/go.mod h1:my3yRxT7CfOztmvi+0svmwbaqw0KFrxaHxncoyaEIP0= github.com/bloeys/gglm v0.43.0 h1:ZpOghR3PHfpkigTDh+FqxLsF0gN8CD6s/bWoei6LyxI= github.com/bloeys/gglm v0.43.0/go.mod h1:qwJQ0WzV191wAMwlGicbfbChbKoSedMk7gFFX6GnyOk= +github.com/bloeys/physx-go v0.1.0 h1:/Zon+zt05hPfCo3gFTbFeZ1PJAGc/pFDyLIiO+V9n4M= +github.com/bloeys/physx-go v0.1.0/go.mod h1:NeG0fl0thLNfZNXkPPZF1dpZpB57BT8MUOdtdCxQSBw= +github.com/bloeys/physx-go v0.1.1 h1:gFUfn3PAuqENStxtFfc2W6bVJXEhiQNYOBm+KlffHv8= +github.com/bloeys/physx-go v0.1.1/go.mod h1:NeG0fl0thLNfZNXkPPZF1dpZpB57BT8MUOdtdCxQSBw= +github.com/bloeys/physx-go v0.1.2 h1:an/8R/QHMiysd2IcviAIVmgwzXRl7fSqiTSNPzi0qzk= +github.com/bloeys/physx-go v0.1.2/go.mod h1:NeG0fl0thLNfZNXkPPZF1dpZpB57BT8MUOdtdCxQSBw= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 h1:zDw5v7qm4yH7N8C8uWd+8Ii9rROdgWxQuGoJ9WDXxfk= diff --git a/main.go b/main.go index 6550dd0..8b17dec 100755 --- a/main.go +++ b/main.go @@ -13,9 +13,11 @@ import ( "github.com/bloeys/nmage/logging" "github.com/bloeys/nmage/materials" "github.com/bloeys/nmage/meshes" + "github.com/bloeys/nmage/physics/physx" "github.com/bloeys/nmage/renderer/rend3dgl" "github.com/bloeys/nmage/timing" nmageimgui "github.com/bloeys/nmage/ui/imgui" + "github.com/bloeys/physx-go/pgo" "github.com/go-gl/gl/v4.1-core/gl" "github.com/inkyblackness/imgui-go/v4" "github.com/veandco/go-sdl2/sdl" @@ -65,6 +67,7 @@ var ( type OurGame struct { Win *engine.Window ImGUIInfo nmageimgui.ImguiInfo + Px *physx.PhysX } type TransformComp struct { @@ -215,6 +218,37 @@ func (g *OurGame) Init() { //Lights simpleMat.SetUnifVec3("lightPos1", lightPos1) simpleMat.SetUnifVec3("lightColor1", lightColor1) + + // Setup physx + px, err := physx.NewPhysx(physx.PhysXCreationOptions{ + TypicalObjectLength: 1, + TypicalObjectSpeed: 9.81, + + EnableVisualDebugger: true, + VisualDebuggerHost: "127.0.0.1", + VisualDebuggerPort: 5425, + VisualDebuggerTimeoutMillis: 10_000, + VisualDebuggerTransmitConstraints: true, + VisualDebuggerTransmitContacts: true, + VisualDebuggerTransmitSceneQueries: true, + + SceneGravity: gglm.NewVec3(0, -9.81, 0), + // @TODO: This has to be zero because PhysX is deciding to throw 'Exception 0x406d1388' when creating threads. + // This exception is used to provide debuggers with thread names :) + // I don't know why it thinks we are running a debugger. This doesn't happen in physx-go. + // To fix it either we run in a debugger, which will handle the exception for us, or we set this + // to zero and run everything on the main thread + SceneCPUDispatcherThreads: 0, + SceneContactHandler: g.PhysxContactHandler, + }) + if err != nil { + logging.ErrLog.Fatalln("Failed to create PhysX. Err:", err) + } + g.Px = px +} + +func (g *OurGame) PhysxContactHandler(pgo.ContactPairHeader) { + } func (g *OurGame) Update() { diff --git a/physics/physx/physx.go b/physics/physx/physx.go new file mode 100755 index 0000000..1281ea8 --- /dev/null +++ b/physics/physx/physx.go @@ -0,0 +1,80 @@ +package physx + +import ( + "errors" + + "github.com/bloeys/gglm/gglm" + "github.com/bloeys/physx-go/pgo" +) + +type PhysX struct { + Foundation *pgo.Foundation + Physics *pgo.Physics + Scene *pgo.Scene +} + +type PhysXCreationOptions struct { + + // Good defaults are length=1 (1m sizes), and speed=9.81 (speed of gravity) + TypicalObjectLength float32 + // Good defaults are length=1 (1m sizes), and speed=9.81 (speed of gravity) + TypicalObjectSpeed float32 + + // If EnableVisualDebugger=true then all VisualDebuggerXYZ variables must be set + EnableVisualDebugger bool + VisualDebuggerHost string + // Default port is 5425 + VisualDebuggerPort int + VisualDebuggerTimeoutMillis int + VisualDebuggerTransmitConstraints bool + VisualDebuggerTransmitContacts bool + VisualDebuggerTransmitSceneQueries bool + + SceneGravity *gglm.Vec3 + // Number of internal PhysX threads that do work. + // If this is zero then all work is done on the thread that calls simulate + SceneCPUDispatcherThreads uint32 + // Gets called when two objects collide + SceneContactHandler func(cph pgo.ContactPairHeader) +} + +func NewPhysx(options PhysXCreationOptions) (px *PhysX, err error) { + + // Setup foundation, pvd, and physics + px = &PhysX{} + px.Foundation = pgo.CreateFoundation() + + ts := pgo.NewTolerancesScale(options.TypicalObjectLength, options.TypicalObjectSpeed) + if options.EnableVisualDebugger { + + pvdTr := pgo.DefaultPvdSocketTransportCreate(options.VisualDebuggerHost, options.VisualDebuggerPort, options.VisualDebuggerTimeoutMillis) + pvd := pgo.CreatePvd(px.Foundation) + if !pvd.Connect(pvdTr, pgo.PvdInstrumentationFlag_eALL) { + return nil, errors.New("failed to connect to PhysX Visual Debugger. Is it running? Did you pass correct visual debugger host/port (default port is 5425)?") + } + + px.Physics = pgo.CreatePhysics(px.Foundation, ts, false, pvd) + + } else { + px.Physics = pgo.CreatePhysics(px.Foundation, ts, false, nil) + } + + // Setup scene + sd := pgo.NewSceneDesc(ts) + sd.SetGravity(pgo.NewVec3(options.SceneGravity.X(), options.SceneGravity.Y(), options.SceneGravity.Z())) + sd.SetCpuDispatcher(pgo.DefaultCpuDispatcherCreate(options.SceneCPUDispatcherThreads, nil).ToCpuDispatcher()) + sd.SetOnContactCallback(options.SceneContactHandler) + + px.Scene = px.Physics.CreateScene(sd) + + if options.EnableVisualDebugger { + + scenePvdClient := px.Scene.GetScenePvdClient() + scenePvdClient.SetScenePvdFlag(pgo.PvdSceneFlag_eTRANSMIT_CONSTRAINTS, options.VisualDebuggerTransmitConstraints) + scenePvdClient.SetScenePvdFlag(pgo.PvdSceneFlag_eTRANSMIT_CONTACTS, options.VisualDebuggerTransmitContacts) + scenePvdClient.SetScenePvdFlag(pgo.PvdSceneFlag_eTRANSMIT_SCENEQUERIES, options.VisualDebuggerTransmitSceneQueries) + scenePvdClient.Release() + } + + return px, nil +}