我使用bezier曲线作为我的太空飞船沿着它们进入码头的路径。我有一个简单的算法来计算,由船舶应在时间t沿三次Bezier曲线:立方贝塞尔曲线:最大梯度和避免碰撞?
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;
}
}
曲线起点是飞船的位置,终点是入口处停靠站(红点在图上)。飞船的方向有一个归一化的矢量,而对接舱有另一个归一化的矢量,用于指示船只必须驶入的方向,以便当它到达时直接对准坞站(图中的黄线)
绿线是宇宙飞船的可能路径,紫色圆圈是宇宙飞船的半径。最后,黑匣子是该站的边框。
我有两个问题:
- 的飞船应该只能够把以每秒
- 飞船不能通过站飞[R弧度
我认为这可以翻译为:
a)。找到将给出船舶不必转得太紧的路径的“曲线因素”(控制点长度)
b)。找到无法避免与车站碰撞的太空船位置/方向(并创建一条路径指引它离开该状态,因此它可以与a部分相连))
但是,对于这两者,我没有多少运气找到解决方案。我已经有了检测向量,方框,点和球体之间交点的代码,但是还没有贝塞尔曲线。我也有功能让我找到两点之间的距离。
任何帮助将非常感激
谢谢, 詹姆斯
谢谢!并抱歉花了这么久回到你身边。我将问题标记为正确的,尽管最终我采用了不同的方法,在继续进入停靠点之前,我使用了一个'侧点',如果车站在路上,那么它将首先进入船舶。 – 2012-07-19 12:47:54