0

我目前正在制作一个程序来构建和拆除块。这很简单但很有趣。我已经走了很远,但遇到了障碍。

到目前为止,我有它,所以当我点击时,它会在我正在查看的位置创建一个块,无论是在另一个块上还是在地面上。这是通过使用来自相机的光线追踪并在由光线与其他对象之间的碰撞创建的 Vector3F 处创建一个块来实现的。我不能做的是对 Vector3F 进行四舍五入,以便数字都是个位数整数。有谁知道如何实现这一目标?

4

1 回答 1

3

您可以使用 Math.round(): http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round(float)

示例:
1:

// It will round the floats in Vector3F and leave the results as floats
Vector3F vect = new Vector3F(2.3f, 1.114124f, 0f);
vect.x = Math.round(vect.x); // 2f
vect.y = Math.round(vect.x); // 1f
vect.z = Math.round(vect.x); // 0f

2:

// It will round the floats in Vector3F and leave the results as ints
Vector3F vect = new Vector3F(2.3f, 1.114124f, 0f);
int x = Math.round(vect.x);
int y = Math.round(vect.x);
int z = Math.round(vect.x);

还有更多信息:How to convert float to int with Java

于 2012-09-18T14:20:42.910 回答