- 船只向前行驶的驱动力
假设在水中没有摩擦阻力,船只有惯性,船只可以转弯,按下前进键时船只会在力的作用下使得自身的物理运动方向变化到自身的前方方向,从而向前行进。

上图中
V:船当前物理速度
V1,V2:V在两个方向上的分速度
Vn:船要达到的目标速度
假设
船的最大前进推进力为pushForce,船的最大速率只能是maxSpeed。
具体思想为:将V分解为V1和V2,利用V1,V2和Vn的关系,得出当前的船需要添加的恒力,下面总结出三种方案。
第一种:
制动力和推进力分离
1.先得出在船正方向的最大力Fn,这里可以抽象的认为这个力是一直添加在船的正方向的,就像火箭的推进引擎。
2.制动力,这是另外计算的,根据当前的船的速度,以及Fn,得出,然后和已经添加的Fn相加得出当前需要添加到恒力组件上的力(由于概念问题,这里Fn做了一次不必要的计算)
第二种:
驱动力即是制动力,也就是一次性计算
用最大正方向速度向量减去当前速度向量,得到和船的偏移向量刚好相反但是模相等的抵消向量。为了抵消偏移向量,让pushForce乘以 该抵消向量的单位向量,从而得到需要添加到恒力组件上的力
改良:为了防止抖动,求出需要的力向量后,让当前组件上的力渐变到求出的力。
第三种:
将当前速度方向分解为V1和V2,然后计算两个垂直方向上需要添加的力,最后又这两个力相加得到需要的力向量
可以看出,其实上面三种方案最后的结果都是相同的,只是为了功能上的需求,以及防止物理演算失真的原因,从而得到的三种不同的计算方案
方案2的unity代码如下
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Player01Test : MonoBehaviour 6{ 7 public float rotateSpeed;//旋转速度 8 ConstantForce2D constantForce2D;//恒力组件 9 public float maxSpeed;//最大速度 10 Rigidbody2D rigid2D; 11 12 //推力参数 13 public float pushForce;//推力大小 14 //[HideInInspector] 15 //public float currentForce;//当前推力 16 17 // Start is called before the first frame update 18 void Start() 19 { 20 constantForce2D = GetComponent<ConstantForce2D>(); 21 rigid2D = GetComponent<Rigidbody2D>(); 22 } 23 24 private void FixedUpdate() 25 { 26 //旋转 27 //为利用扭矩力使得船往某个方向旋转 28 if (Input.GetKey(KeyCode.LeftArrow))//按下左方向键,船往顺时针旋转 29 { 30 //transform.Rotate(new Vector3(0, 0, rotateSpeed * Time.fixedDeltaTime)); 31 if (rigid2D.angularVelocity >= 200)//如果旋转速度已经达到最大,那么撤销扭矩力 32 constantForce2D.torque = 0; 33 else //否则添加扭矩力 34 constantForce2D.torque = 600; 35 rigid2D.angularDrag = 0; 36 } 37 else if (Input.GetKey(KeyCode.RightArrow))//按下右方向键,船往逆时针旋转 38 { 39 //transform.Rotate(new Vector3(0, 0, -rotateSpeed * Time.fixedDeltaTime)); 40 if (rigid2D.angularVelocity <= -200) 41 constantForce2D.torque = 0; 42 else 43 constantForce2D.torque = -600; 44 rigid2D.angularDrag = 0; 45 } 46 else 47 { 48 constantForce2D.torque = 0; 49 rigid2D.angularDrag = 50; 50 } 51 52 //推进 53 if (Input.GetKey(KeyCode.UpArrow)) 54 { 55 Vector3 Vn= transform.up * maxSpeed;//船需要达到的目标速度 56 Vector3 V = rigid2D.velocity;//当前的速度(方向不一定是当前的船正方向) 57 Vector3 forceDir = (Vn - V).normalized;//获得力方向的单元向量 58 59 //添加力到船的恒力组件上 60 constantForce2D.force = forceDir * pushForce; 61 /*//这里为改进代码方式0,用来代替上面这段代码,即允许微小误差存在,主要是防止物理演算的失真抖动 62 if (forceDir.magnitude>Time.fixedDeltaTime*pushForce) 63 constantForce2D.force = forceDir * pushForce; 64 else 65 constantForce2D.force = Vector3.zero; 66 */ 67 } 68 else//如果没有添加任何力,那么让恒力组件为0 69 { 70 constantForce2D.force = Vector3.zero; 71 } 72 } 73}
组件

- 船只转弯的驱动
为了方便理解这里进行类似例子引申:
以2d为例,一个小球在一个横杆上,小球唯一的移动方式是设置constant force组件的方向力,小球有惯性,没有摩擦力,如何让小球移动到杆上的任意一个点,并且在该点的速度刚好为0?
很显然,在物理上这个问题很难解决,因此解决的方案是如何让小球无限的接近目标点,并且速度为0。
问题的关键:
如何在力的 作用下到达目标点
如何在力的作用下变化到目标速度
(这里就可想象为到达目标点时,速度为0)
方案一:(太复杂,以后考虑)
设置减速半径,当运动到里目标点距离为减速半径的长度时进行减速操作。减速力为,减速力为偏移距离剧偏移半径的百分比乘以最大推力,方向为速度的反方向。偏移半径为,最大速度在最大力的反作用下减速到0,期间运动的距离刚好为减速半径(改进,在变力的情况下速度减到0,运动的距离为偏移半径)
方案二:(太简单太low)
进入减速半径后直接使用rigidbody上的迟滞力,将迟滞力放到最大
方案三:(明确简单,优先选择)
1.如果大于减速距离,那么用力驱使速度朝向目标方向移动,目标速度为最大速度
2.如果小于减速距离,那么用力驱使速度朝向目标方向,目标速度为迟滞速度(迟滞速度比最大速度小得多,便于操作调节运动的位置)
3.如果小于迟滞距离:如果当前速度大于迟滞速度,那么设置目标速度为0,此时如果速度小于迟滞速度,那么设置线性阻尼为最大(改进:如果速度方向与目标方向相反,把目标速度改为朝向目标点的迟滞速度);如果速度小于迟滞速度,那么设置线性阻尼为最大(改进:如果速度方向与目标方向相反,把目标速度改为朝向目标点的迟滞速度)。
从而使得角色最大程度的靠近目标点,并且速度为0.
//=============================================================================================================
//更新版,以unity2D飞行游戏为例
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5//飞船的最大推进力,扭矩力,推进转弯速率比来自于引擎 6public class Ship : MonoBehaviour 7{ 8 9 public float maxSpeed; 10 public float pushForce; 11 12 public float maxRotateSpeed; 13 public float torque; 14 15 public float rotateSpeedPercent;//转弯速率比,即当飞船不推进时最大转弯速率是maxRotateSpeed,如果正在开启引擎推进,那么速率是百分率乘以最大速率 16 public float currentRotatePercent; 17 18 public Transform weaponAnchor;//武器锚点,生成的武器成为锚点的子物体 19 //public Weapon weapon; 20 public Transform engineAnchor; 21 //public Engine engine; 22 23 Rigidbody2D rigid2D; 24 ConstantForce2D constantForce2D; 25 26 public Transform model; 27 28 // Start is called before the first frame update 29 void Start() 30 { 31 rigid2D = GetComponent<Rigidbody2D>(); 32 constantForce2D = GetComponent<ConstantForce2D>(); 33 } 34 35 private void Update() 36 { 37 //Vector3 rotate = new Vector3(0, transform.rotation.eulerAngles.z, 0); 38 int n = (int)(transform.rotation.eulerAngles.z / 90); 39 if (n == 0 || n == 2) model.transform.rotation = transform.rotation * Quaternion.Euler(0, transform.rotation.eulerAngles.z % 90*(n==0?-1:1), 0); 40 else model.transform.rotation = transform.rotation * Quaternion.Euler(0, (90-transform.rotation.eulerAngles.z % 90) * (n == 1 ? -1 : 1), 0); 41 Debug.Log(model.rotation.eulerAngles.z); 42 } 43 44 // Update is called once per frame 45 void FixedUpdate() 46 { 47 //引擎推进 48 if (Input.GetKey(KeyCode.UpArrow)) 49 { 50 PushRigid(); 51 //engine.EngineActive(); 52 currentRotatePercent = 0.5f;// rotateSpeedPercent;//如果正在推进引擎,那么当前转弯速率比为默认转弯速率比 53 } 54 else 55 { 56 constantForce2D.force = Vector3.zero; 57 //engine.EngineUnActive(); 58 currentRotatePercent = 1;//如果没有推进引擎,那么当前转弯速率比为1,即用最大转弯速率为目标速率旋转 59 } 60 61 //转弯 62 if (Input.GetKey(KeyCode.LeftArrow)) { rigid2D.angularDrag = 0; RotateRigid(1); } 63 else if (Input.GetKey(KeyCode.RightArrow)) { rigid2D.angularDrag = 0; RotateRigid(-1); } 64 else { RotateRigid(0); rigid2D.angularDrag = 20; } 65 66 //开火 67 //if (Input.GetKey(KeyCode.LeftControl) && weapon != null) weapon.Fire(); 68 } 69 70 //施加推力 71 void PushRigid() 72 { 73 Vector3 velocityReal = transform.up * maxSpeed; 74 Vector3 velocityRigid = rigid2D.velocity; 75 Vector3 forceDir = (velocityReal - velocityRigid).normalized; 76 if ((velocityReal - velocityRigid).magnitude > Time.fixedDeltaTime * pushForce) { 77 constantForce2D.force = forceDir * pushForce; 78 } 79 else 80 constantForce2D.force = Vector3.zero; 81 } 82 83 //旋转,用最大速率与百分比相乘获得目标旋转速率 84 void RotateRigid(int dir) 85 { 86 RotateRigid(dir, currentRotatePercent * maxRotateSpeed); 87 } 88 89 //旋转,带有目标旋转速率 90 void RotateRigid(int dir,float rotateSpeed)//0停止旋转,1正转,-1反转,第二个参数为转弯速率 91 { 92 93 float tarAngularVelocity = dir * rotateSpeed;//最大目标速度current永远大于0 94 float dirAngular = tarAngularVelocity - rigid2D.angularVelocity;//获得速度差 95 float proportion = dirAngular / rotateSpeed; 96 proportion = Mathf.Clamp(proportion, -1, 1); 97 constantForce2D.torque = proportion * torque; 98 } 99 100 //这里是数据使得飞船部件的属性数据计算生效 101 /* 102 public void InitDatas() 103 { 104 if (engine) 105 { 106 pushForce = engine.pushForce; 107 torque = engine.torque; 108 rotateSpeedPercent = engine.rotatePercent; 109 } 110 }*/ 111 112 private void OnDrawGizmos() 113 { 114 Gizmos.color = Color.red; 115 116 } 117}