Go语言调用-Physx物理引擎

作者: Golang语言社区 | 来源:发表于2018-12-12 16:59 被阅读28次

链接地址:
https://github.com/Golangltd/go-physx

实例:
package gophysx

/*

cgo LDFLAGS: -L./ -lPhysxWrapGo

include "PhysxWrapGo.h"

include "stdlib.h"

*/
import "C"
import (
"errors"
"unsafe"
)

var bInitSDK int

func init() {
bInitSDK = int(C.InitPhysxSDK())
if bInitSDK == 0 {
panic("[FATAL] CANT INIT PHYSX SDK!")
}
}

func ReleasePhysxSDK() {
C.ReleasePhysxSDK()
}

var ErrNeedInitSDK = errors.New("physx sdk is not init.")
var ErrCreateSceneFail = errors.New("create scene fail.")

type PxScene struct {
c unsafe.Pointer
}

func NewScene(path string) (*PxScene, error) {
spath := C.CString(path)
defer C.free(unsafe.Pointer(spath))
if bInitSDK == 0 {
return nil, ErrNeedInitSDK
}
this := &PxScene{}
this.c = C.CreateScene(spath)
if this.c == nil {
return nil, ErrCreateSceneFail
}
return this, nil
}

func (this *PxScene) Release() {
if this.c != nil {
C.DestroyScene(this.c)
this.c = nil
}
}

func (this *PxScene) Update(elapsedTime float32) {
if this.c != nil {
C.UpdateScene(this.c, C.float(elapsedTime))
}
}

func (this *PxScene) CreatePlane(yAxis float32) (id uint64) {
if this.c != nil {
id = uint64(C.CreatePlane(this.c, C.float(yAxis)))
}
return
}

func (this *PxScene) CreateBoxDynamic(pos Vector3, halfExtents Vector3) (id uint64) {
if this.c != nil {
id = uint64(C.CreateBoxDynamic(this.c, C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(halfExtents.X), C.float(halfExtents.Y), C.float(halfExtents.Z)))
}
return
}

func (this *PxScene) CreateBoxKinematic(pos Vector3, halfExtents Vector3) (id uint64) {
if this.c != nil {
id = uint64(C.CreateBoxKinematic(this.c, C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(halfExtents.X), C.float(halfExtents.Y), C.float(halfExtents.Z)))
}
return
}

func (this *PxScene) CreateBoxStatic(pos Vector3, halfExtents Vector3) (id uint64) {
if this.c != nil {
id = uint64(C.CreateBoxStatic(this.c, C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(halfExtents.X), C.float(halfExtents.Y), C.float(halfExtents.Z)))
}
return
}

func (this *PxScene) CreateSphereDynamic(pos Vector3, radius float32) (id uint64) {
if this.c != nil {
id = uint64(C.CreateSphereDynamic(this.c, C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(radius)))
}
return
}

func (this *PxScene) CreateSphereKinematic(pos Vector3, radius float32) (id uint64) {
if this.c != nil {
id = uint64(C.CreateSphereKinematic(this.c, C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(radius)))
}
return
}

func (this *PxScene) CreateSphereStatic(pos Vector3, radius float32) (id uint64) {
if this.c != nil {
id = uint64(C.CreateSphereStatic(this.c, C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(radius)))
}
return
}

func (this *PxScene) CreateCapsuleDynamic(pos Vector3, radius, halfHeight float32) (id uint64) {
if this.c != nil {
id = uint64(C.CreateCapsuleDynamic(this.c, C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(radius), C.float(halfHeight)))
}
return
}

func (this *PxScene) CreateCapsuleKinematic(pos Vector3, radius, halfHeight float32) (id uint64) {
if this.c != nil {
id = uint64(C.CreateCapsuleKinematic(this.c, C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(radius), C.float(halfHeight)))
}
return
}

func (this *PxScene) CreateCapsuleStatic(pos Vector3, radius, halfHeight float32) (id uint64) {
if this.c != nil {
id = uint64(C.CreateCapsuleStatic(this.c, C.float(pos.X), C.float(pos.Y), C.float(pos.Z), C.float(radius), C.float(halfHeight)))
}
return
}

func (this *PxScene) RemoveActor(id uint64) {
if this.c != nil {
C.RemoveActor(this.c, C.ulonglong(id))
}
}

func (this *PxScene) SetLinearVelocity(id uint64, velocity Vector3) {
if this.c != nil {
C.SetLinearVelocity(this.c, C.ulonglong(id), C.float(velocity.X), C.float(velocity.Y), C.float(velocity.Z))
}
}

func (this *PxScene) AddForce(id uint64, force Vector3) {
if this.c != nil {
C.AddForce(this.c, C.ulonglong(id), C.float(force.X), C.float(force.Y), C.float(force.Z))
}
}

func (this *PxScene) ClearForce(id uint64) {
if this.c != nil {
C.ClearForce(this.c, C.ulonglong(id))
}
}

func (this *PxScene) GetGlobalPostion(id uint64) (outPostionX, outPostionY, outPostionZ float32) {
if this.c != nil {
C.GetGlobalPostion(this.c, C.ulonglong(id), unsafe.Pointer(&outPostionX), unsafe.Pointer(&outPostionY), unsafe.Pointer(&outPostionZ))
}
return
}

func (this *PxScene) GetGlobalRotate(id uint64) (outRotateX, outRotateY, outRotateZ, outRotateW float32) {
if this.c != nil {
C.GetGlobalRotate(this.c, C.ulonglong(id), unsafe.Pointer(&outRotateX), unsafe.Pointer(&outRotateY), unsafe.Pointer(&outRotateZ), unsafe.Pointer(&outRotateW))
}
return
}

func (this *PxScene) SetGlobalPostion(id uint64, pos Vector3) {
if this.c != nil {
C.SetGlobalPostion(this.c, C.ulonglong(id), C.float(pos.X), C.float(pos.Y), C.float(pos.Z))
}
}

func (this *PxScene) SetGlobalRotate(id uint64, rotate Quat) {
if this.c != nil {
C.SetGlobalRotate(this.c, C.ulonglong(id), C.float(rotate.X), C.float(rotate.Y), C.float(rotate.Z), C.float(rotate.W))
}
}

func (this *PxScene) IsStaticObj(id uint64) (ok bool) {
if this.c != nil {
ok = (C.IsStaticObj(this.c, C.ulonglong(id)) != 0)
}
return
}

func (this *PxScene) IsDynamicObj(id uint64) (ok bool) {
if this.c != nil {
ok = (C.IsDynamicObj(this.c, C.ulonglong(id)) != 0)
}
return
}

func (this *PxScene) SetCurrentMaterial(staticFriction, dynamicFriction, restitution float32) {
if this.c != nil {
C.SetCurrentMaterial(this.c, C.float(staticFriction), C.float(dynamicFriction), C.float(restitution))
}
}

func (this *PxScene) SetCurrentAngularDamping(value float32) {
if this.c != nil {
C.SetCurrentAngularDamping(this.c, C.float(value))
}
}


社区交流群:221273219
Golang语言社区论坛 :
www.Golang.Ltd
LollipopGo游戏服务器地址:
https://github.com/Golangltd/LollipopGo
社区视频课程课件GIT地址:
https://github.com/Golangltd/codeclass


Golang语言社区

相关文章

  • Go语言调用-Physx物理引擎

    链接地址:https://github.com/Golangltd/go-physx 实例:package gop...

  • Unity基础-物理系统

    Unity内置了NVIDIA的Physx物理引擎,Physx是目前使用最为广泛的物理引擎,被很多游戏大作所采用,开...

  • Unity基础(12)-物理系统

    1.什么是Unity物理系统 Unity是一款3D引擎软件,内置NVIDIA PhysX物理引擎,使3D物体具备物...

  • PhysX 3.3 基础

    概述 PhysX目前是由 NVIDIA 开源的一个3D物理引擎,由于其功能强大,目前已经继承在了各大商业游戏引擎中...

  • cgo

    cgo cgo是用来在Go语言中调用C语言的工具 Go语言调用C语言 简单C语言函数 在Go语言中需要通过impo...

  • 01初识Go

    语言介绍 go语言的全称是:go programming language。但是因为go 这个词太通用了,搜索引擎...

  • [docker]基于Centos7安装Docker

    Docker 是一个开源的应用容器引擎,基于 Go 语言[https://www.runoob.com/go/go...

  • golang读取stdin

    go语言读取stdin内容代码例子 go程序代码 调用脚本1 运行结果 调用脚本2 运行结果

  • go语言与c语言的相互调用

    由于工作原因,需要实现go语言与c语言的相互调用。由于go语言与c语言有着千丝万缕的暧昧关系,两者之间的调用可以通...

  • 18-Go语言和C语言交叉访问

    Go语言中调用C语言函数 在Go语言开篇中我们已经知道, Go语言与C语言之间有着千丝万缕的关系, 甚至被称之为2...

网友评论

    本文标题:Go语言调用-Physx物理引擎

    本文链接:https://www.haomeiwen.com/subject/pfsphqtx.html