宇宙船がステーションのドックに入るとき、宇宙船が移動するパスとしてベジエ曲線を使用しています。3 次ベジエ曲線に沿って、船が時間 t にあるべき場所を計算する簡単なアルゴリズムがあります。
public class BezierMovement{
public BezierMovement(){
// start docking straight away in this test version
initDocking();
}
private Vector3 p0;
private Vector3 p1;
private Vector3 p2;
private Vector3 p3;
private double tInc = 0.001d;
private double t = tInc;
protected void initDocking(){
// get current location
Vector3 location = getCurrentLocation();
// get docking point
Vector3 dockingPoint = getDockingPoint();
// ship's normalised direction vector
Vector3 direction = getDirection();
// docking point's normalised direction vector
Vector3 dockingDirection = getDockingDirection();
// scalars to multiply normalised vectors by
// The higher the number, the "curvier" the curve
float curveFactorShip = 10000.0f;
float curveFactorDock = 2000.0f;
p0 = new Vector3(location.x,location.y,location.z);
p1 = new Vector3(location.x + (direction.x * curveFactorShip),
location.y + (direction.y * curveFactorShip),
location.z + (direction.z * curveFactorShip));
p2 = new Vector3(dockingPoint.x + (dockingDirection.x * curveFactorDock),
dockingPoint.y + (dockingDirection.y * curveFactorDock),
dockingPoint.z + (dockingDirection.z * curveFactorDock));
p3 = new Vector3(dockingPoint.x, dockingPoint.y, dockingPoint.z);
}
public void incrementPosition() {
bezier(p0, p1, p2, p3, t, getCurrentLocation());
// make ship go back and forth along curve for testing
t += tInc;
if(t>=1){
tInc = 0-tInc;
} else if(t<0){
tInc = 0-tInc;
}
}
protected void bezier(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, double t, Vector3 outputVector){
double a = (1-t)*(1-t)*(1-t);
double b = 3*((1-t)*(1-t))*t;
double c = 3*(1-t)*(t*t);
double d = t*t*t;
outputVector.x = a*p0.x + b*p1.x + c*p2.x + d*p3.x;
outputVector.y = a*p0.y + b*p1.y + c*p2.y + d*p3.y;
outputVector.z = a*p0.z + b*p1.z + c*p2.z + d*p3.z;
}
}
曲線の始点は宇宙船の位置で、終点はドッキング ベイの入り口です (図の赤い点)。宇宙船にはその方向の正規化されたベクトルがあり、ドッキング ベイには別の正規化されたベクトルがあり、船が到着したときにドッキング ベイにまっすぐに整列するために、船が進むべき方向を示します (図の黄色の線)。
緑色の線は宇宙船の可能な経路であり、紫色の円は宇宙船の半径です。最後に、ブラック ボックスはステーションのバウンディング ボックスです。
2 つの問題があります。
- 宇宙船は毎秒 r ラジアンでしか回転できないはずです
- 宇宙船はステーションを通過できません
これは次のように変換されると思います。
a)。船がきつく曲がる必要のない経路を与える「曲線係数」(制御点の長さ) を見つける
b)。ステーションとの衝突を避けられない宇宙船の位置/方向を見つける (そして、その状態からそれを導くためのパスを作成して、パート a) に乗ることができるようにする)
ただし、これらの両方で、解決策を見つけることができませんでした。ベクトル、ボックス、点、球の間の交点を検出するコードは既にありますが、ベジェ曲線はまだありません。2 点間の距離を求める関数もあります。
どんな助けでも大歓迎です
ありがとう、ジェームズ