0

I am currently making a program to build up and knock down blocks. It's simple but fun. I have gotten pretty far but have hit a snag.

So far, I have it so that when I click, it creates a block at the position I am looking at, either on another block or the ground. This is achieved through the use of ray tracing from the camera and creating a block at a Vector3F created from the collision between the ray and the other object. What I can't do is round the Vector3F so that the numbers are all single digit integers. Does anyone know how to achieve this?

John Conde
  • 217,595
  • 99
  • 455
  • 496
user1610541
  • 111
  • 1
  • 11

1 Answers1

3

You can use Math.round(): http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round(float)

Examples:
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);

There is also more info: How to convert float to int with Java

Community
  • 1
  • 1
m4tx
  • 4,139
  • 5
  • 37
  • 61