找回密码
 立即注册
查看: 243|回复: 0

[笔记] Unity 3D定点数物理引擎实战系列(五)重构物理Entity架构 ...

[复制链接]
发表于 2022-9-22 07:10 | 显示全部楼层 |阅读模式

前言

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


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


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


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实战源码。
附:视频教程

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 06:40 , Processed in 0.648212 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表