我想在屏幕上生成随机样条线。 这是我到目前为止所拥有的:
public class CurvedPath {
Random rn;
CatmullRomSpline<Vector2> curve;
float[] xPts;
float[] yPts;
Vector2[] points;
public CurvedPath(){
points = new Vector2[10];
rn = new Random();
curve = new CatmullRomSpline<Vector2>(points,false);
for(int i = 0 ; i < 10; i++){
xPts[i] = rn.nextFloat()*SampleGame.WIDTH;
yPts[i] = SampleGame.HEIGHT*i/10;
}
}
}
我对有关如何使用 CatmullRomSpline
对象 ( https://github.com/libgdx/libgdx/wiki/Path-interface-&-Splines ) 提供的文档感到非常困惑
基本上,我在这里尝试做的是生成 10 个随机点,均匀分布在屏幕的高度上,并沿屏幕的宽度随机放置,以创建随机的弯曲路径。
因此,在构造函数的 for 循环中,您可以看到我生成了样条线每个控制点的 x 和 y 值。
如何将这些点输入到样条对象中并将其渲染在屏幕上?
-谢谢
更新 让我将我的问题改写得更具体一点..
我的控制点由 xPts
和 yPts 表示。
现在我想获取沿着样条线落在的点,如何使用这两个 vector 来做到这一点? CatmullRomSpline 的构造函数采用一个 Vector2
,而不是两个 float[]
最佳答案
你做了什么。填充点:
curve = new CatmullRomSpline<Vector2>(points,false);
要获取曲线上的点:
Vector2 point = new Vector2();
curve.valueAt(point, 0.5f);
valueAt()参数解释:
1(点)您要查找的点存储在 Vector2 对象中。
- 在0和1之间 float ,0是第一个点,1是最后一个点。 0.5f是中间。该 float 表示从第一个点到最后一个点的孔距离。
获取并渲染 100 点可能如下所示:
Vector2 point = new Vector2();
for (int i = 0; i <= 100; i++) {
curve.valueAt(point, i * 0.01f);
// draw using point.x and point.y
}
回答您编辑过的问题:
for(int i = 0 ; i < 10; i++){
points[i].x = rn.nextFloat()*SampleGame.WIDTH;
points[i].y = SampleGame.HEIGHT*i/10;
}
curve = new CatmullRomSpline<Vector2>(points,false);
关于java - Catmull Rom 样条实现 (LibGDX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32161619/