Unity 3D定点数物理引擎实战系列

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

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

1: 设计PhyBaseEntity,让所有具体形状的Entity都继承自它;

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

3: 编写其它的形状的物理Entity;

PhyBaseEntity 设计

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

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

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里面。

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

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,就很简单了:

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实战源码。

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值