Some initial work

This commit is contained in:
bloeys
2022-12-07 03:31:35 +04:00
parent 8e9dbee002
commit b219ee830d
4 changed files with 123 additions and 0 deletions

3
go.mod
View File

@ -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

6
go.sum
View File

@ -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=

34
main.go
View File

@ -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() {

80
physics/physx/physx.go Executable file
View File

@ -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
}