重构物理Entity架构,支持更多的形状

发布时间 2023-11-01 10:03:41作者: 游戏开发阿博

上一节实战如何编译BEPUphysicsint源码到自己的项目, 如何整合物理引擎与Unity图形渲染。本节来从新调整设计,让物理的Entity基本操作与物理的形状分离,支持更多的物理形状,支持物理entity与Unity物体位移与旋转同步。主要分成以下3个部分:

PhyBaseEntity 设计

上一节课我们设计的PhyBoxEntity主要包含了两个部分,一个是Entity的物理形状的创建(这个与具体的形状有关),一个是物理Entity的基本功能,如位置,旋转同步,給Entity线性速度等。而且其它的形状的物理Entity,除了形状创建会不一样,其它基本一样,所以我们调整一下设计,把PhyBoxEntity与物理形状相关的代码放到PhyBoxEntity里面,与形状无关的物理Entity的基本功能,我们放到PhyBaseEntity里面,然后让具体的形状在扩展的时候继承PhyBaseEntity即可。合计架构如图所示:

 

接下来我们把上一节课的PhyBoxEntity与形状无关的数据与功能提取到PhyBaseEntity,PhyBoxEntity只保留形状相关的代码。与形状无关的数据部分:

public class PhyBaseEntity : MonoBehaviour {
    protected BEPUphysics.Entities.Entity phyEntity = null;
    protected Vector3 center = Vector3.zero;
    [SerializeField]
    protected float mass = 1;

    protected bool isTrigger = false;
    protected PhysicMaterial phyMat = null;
    
    [SerializeField]
    protected bool isStatic = false;
}

phyEntity: 用的是所有Entity形状的基类,这样可以指向不同子类的具体实例,其它数据都是物理Entity通用的,都写道PhyBaseEntity里面。

同步物理Entity与Unity节点的位移与旋转

同时PhyBaseEntity实现了物理Entity与Unity节点同步的功能增加了位置+旋转。如代码所示:

public void AddSelfToPhyWorld() { 
        if(this.phyEntity == null) {
            return;
        }

        BEPUPhyMgr.Instance.space.Add(this.phyEntity);
    }

    public void SyncPhyTransformWithUnityTransform() {
        if (this.phyEntity == null) {
            return;
        }


        // 位置
        Vector3 unityPos = this.transform.position;
        unityPos += this.center;
        this.phyEntity.position = ConversionHelper.MathConverter.Convert(unityPos);
        // end

        // 旋转
        Quaternion rot = this.transform.rotation;
        this.phyEntity.orientation = ConversionHelper.MathConverter.Convert(rot);
        // end
    }

    public void SyncUnityTransformWithPhyTransform() {
        if (this.phyEntity == null) {
            return;
        }

        // 位置
        BEPUutilities.Vector3 pos = this.phyEntity.position;
        Vector3 unityPosition = ConversionHelper.MathConverter.Convert(pos);
        unityPosition -= this.center;
        this.transform.position = unityPosition;
        // end 

        // 旋转
        BEPUutilities.Quaternion rot = this.phyEntity.orientation;
        Quaternion r = ConversionHelper.MathConverter.Convert(rot);
        this.transform.rotation = r;
        // end
    }

    // 同步物理entity的位置到transform;
    void LateUpdate() {
        if (this.phyEntity == null || this.isStatic) {
            return;
        }

        this.SyncUnityTransformWithPhyTransform();
    }

接口SyncUnityTransformWithPhyTransform: 从物理Entity同步到Unity节点

接口SyncPhyTransformWithUnityTransform: 从Unity节点同步到物理Entity

 

再来看调整后的PhyBoxEntity,就很简单了:

[RequireComponent(typeof(BoxCollider))]
public class PhyBoxEntity : PhyBaseEntity {
    
    void Start() {
        BoxCollider box = this.gameObject.GetComponent<BoxCollider>();
        float width = box.size.x;
        float height = box.size.y;
        float length = box.size.z;
        
        this.center = box.center;
        this.phyMat = box.material;
        this.isTrigger = box.isTrigger;


        if (this.isStatic) {
            this.phyEntity = new BEPUphysics.Entities.Prefabs.Box(BEPUutilities.Vector3.Zero, (FixMath.NET.Fix64)width, (FixMath.NET.Fix64)height, (FixMath.NET.Fix64)length);
        }
        else {
            this.phyEntity = new BEPUphysics.Entities.Prefabs.Box(BEPUutilities.Vector3.Zero, (FixMath.NET.Fix64)width, (FixMath.NET.Fix64)height, (FixMath.NET.Fix64)length, (FixMath.NET.Fix64)this.mass);
        }

        this.AddSelfToPhyWorld();
        this.SyncPhyTransformWithUnityTransform();
    }

   
}

根据BoxCollider的数据创建Box的物理Entity, 同步到Unity节点的位置与旋转,并加入到物理世界。

 

编写其它形状的物理Entity

经过上面的调整以后,我们就很快的编写其它的形状,比如实现PhySphereEntity,

[RequireComponent(typeof(SphereCollider))]
public class PhySphereEntity : PhyBaseEntity
{
    // Start is called before the first frame update
    void Start() {
        SphereCollider sphere = this.gameObject.GetComponent<SphereCollider>();

        float radius = sphere.radius;

        this.center = sphere.center;
        this.phyMat = sphere.material;
        this.isTrigger = sphere.isTrigger;

        if (this.isStatic) {
            this.phyEntity = new BEPUphysics.Entities.Prefabs.Sphere(BEPUutilities.Vector3.Zero, (FixMath.NET.Fix64)radius);
        }
        else {
            this.phyEntity = new BEPUphysics.Entities.Prefabs.Sphere(BEPUutilities.Vector3.Zero, (FixMath.NET.Fix64)radius, (FixMath.NET.Fix64)this.mass);
        }

        this.AddSelfToPhyWorld();
        this.SyncPhyTransformWithUnityTransform();
    }
}

继承自PhyBaseEntity,同时根据SphereCollider组件来创建形状。

后续我们要給物理Entity线性速度,角速度都把接口写到PhyBaseEntity里面。

 

今天的物理Entity的设计调整就到这里了,关注我们,可以获取Unity BEPUphysint3D实战源码。