我试图找出如何制作4个单位宽度的赛道,我想我需要找到一个与另一个点平行的点,我已经读过这个:http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
但我无法理解它,这就是我目前的观点(方法的一部分):
private List<VectorData> calculateTrack(int trackNr) {
Vector trackHeight = new Vector(0d, 0d, 2d);
//Generate all points
List<VectorData> points = new ArrayList<>();
for (double t = 0d; t < 1.00d; t += (1d / SEGMENTS)) {
Vector pIn, pOut, p0, p1, p2, p3;
switch (trackNr) {
case 0:
pIn = getPoint(t);
pOut = getOuterPoint(t);
p0 = pIn.subtract(trackHeight); //bottom point inside of track
p1 = pIn; //top point inside of track
// p2 = p.add(p.cross(p.add(Vector.Z)).normalized().scale(4d));
// p3 = p.add(p.cross(p.add(Vector.Z)).normalized().scale(4d)).subtract(trackHeight);
p2 = pOut; //top point outside of track
p3 = pOut.subtract(trackHeight); //bottom point outside of track
break;
case 1:
pIn = getCubicBezierPnt(t, controlPointsOTrack[0], controlPointsOTrack[1], controlPointsOTrack[2], controlPointsOTrack[3]); //bottom point inside
pOut = getCubicBezierPnt(t, controlPointsOTrackOutside[0], controlPointsOTrackOutside[1], controlPointsOTrackOutside[2], controlPointsOTrackOutside[3]);
p0 = pIn.subtract(trackHeight); //top point inside
p1 = pIn; //top point inside
// p2 = p.add(p.cross(Vector.Z).normalized().scale(4d)); //top point outside
// p3 = p.add(p.cross(Vector.Z).normalized().scale(4d)).subtract(trackHeight); //bottom point outside
p2 = pOut;
p3 = pOut.subtract(trackHeight);
break;
default:
throw new IllegalArgumentException("RaceTrack.calculateTrack: Unknown trackNr: trackNr = " + trackNr);
}
VectorData[] pArray = { new VectorData(p0, null), new VectorData(p1, null), new VectorData(p2, null), new VectorData(p3, null) };
points.addAll(Arrays.asList(pArray));
}
你可以看到我现在使用getPoint(t)
和getOuterPoint(t)
作弊',但首先我不想这样做,其次这不再适用于贝塞尔曲线。
那么有人可以指导我如何得到一个与另一个点垂直的点吗?
有效替换以下代码:
/**
* Returns the position of the curve at 0 <= {@code t} <= 1.
*/
public Vector getPoint(double t) {
return new Vector(
10 * Math.cos(2 * Math.PI * t),
14 * Math.sin(2 * Math.PI * t),
1d
);
}
public Vector getOuterPoint(double t) {
return new Vector(
(10 + 4) * Math.cos(2 * Math.PI * t),
(14 + 4) * Math.sin(2 * Math.PI * t),
1d
);
}
我建议的解决方案:
Vector vDelta = getPointPerTrackNr(trackNr, (t + (1d / SEGMENTS)) % 1.00d).subtract(pIn);
Vector vWidth = new Vector(
(vDelta.x() * Math.cos(Math.toRadians(270))) - (vDelta.y() * Math.sin(Math.toRadians(270))),
(vDelta.x() * Math.sin(Math.toRadians(270))) + (vDelta.y() * Math.cos(Math.toRadians(270))),
vDelta.z()
).normalized().scale(RobotRace.NUM_ROBOTS * 1d);
首先,我计算vDelta
,即t
的当前曲目段与下一曲目段之间的距离,例如t + 0.01d
。
然后我通过取vWidth
计算vDelta
,将其旋转270度,然后对其进行标准化,然后使用宽度进行缩放,在本例中为RobotRace.NUM_ROBOTS * 1d
。