【发布时间】:2014-10-09 07:55:38
【问题描述】:
我想在我的游戏引擎中实现平滑的地形碰撞,当我说平滑时,我的意思是玩家的高度不是由一个顶点决定的。我相信重心坐标是要走的路。我花了 7 个小时研究这个问题,但我看到的代码都没有真正起作用,也没有用简单的英语解释。
这就是我目前所拥有的一切。 :(
public float getHeightAt(float xPos, float zPos) {
Vector3f one = new Vector3f((float)xPos, ((float)new Color(heightMap.getRGB((int)xPos, (int)zPos)).getRed())/255f*exaggeration*scale, (float)zPos);
Vector3f two = new Vector3f((float)xPos+1, ((float)new Color(heightMap.getRGB((int)xPos+1, (int)zPos)).getRed())/255f*exaggeration*scale, (float)zPos);
Vector3f three = new Vector3f((float)xPos, ((float)new Color(heightMap.getRGB((int)xPos, (int)zPos+1)).getRed())/255f*exaggeration*scale, (float)zPos+1);
float height = mid(one, two, three, new Vector3f(xPos, 0f, zPos));
System.out.println(height);
return height + 0.25f;
}
private float mid(Vector3f a, Vector3f b, Vector3f c, Vector3f p) {
Vector3f AB = a.mul(b);
Vector3f BC = b.mul(c);
Vector3f norm = AB.cross(BC);
float n0 = norm.getX();
float n1 = norm.getY();
float n2 = norm.getZ();
return (n0*a.getX() + n1*a.getY() + n2*a.getZ() - n0*p.getX() - n2*p.getZ()) / n1;
}
它可以工作,但它不平滑,我什至不知道它是否是重心的。
【问题讨论】:
-
您可能想尝试双线性插值。
标签: java opengl collision smooth terrain