1

I have a processing script for a 2-dimensional Minecraft clone. I am currently stuck at placing blocks, as I need them to be snapped to a grid of size block_size. Here is the code:

final float block_size = 8;
final float ground_level = 8;
final float sky_level = 256;
final float x_min = 0;
final float x_max = 256;

class Player {

  float x;
  float y;
  boolean facing;

  Player(float spawn_x, float spawn_y) {
    x = spawn_x;
    y = spawn_y;
  }

  void move() {
    if (keyPressed) {
      if (key == CODED) {
        switch(key) {
          case 'a':
            x--;
            facing = false;
            break;
          case 'd':
            x++;
            facing = true;
            break;
          case ' ':
            y--;
        }
      } else {
        switch(keyCode) {
          case LEFT:
            x--;
            facing = false;
            break;
          case RIGHT:
            x++;
            facing = true;
            break;
          case UP:
            y--;
        }
      }
    }
  }

  void physics() {
    if (y < ground_level) {
      y++;
    }
    x = constrain(x, x_min, x_max);
    y = constrain(y, ground_level, sky_level);
  }

  void place_block() {

  }

  void break_block() {

  }

}

class Block {

  Block(float x_, float y_) {

  }

  void display() {

  }

}

Player player = new Player(2, (x_max - x_min) / 2);

ArrayList<Block> blocks = new ArrayList<Block>();

The problem is in the Block(float x_, float y_) constructor. I need to make sure that x is never above x_, as close as possible to x_, and also a multiple of block_size. Same for y.

I tried x = floor(x_ / block_size) * block_size;, but it didn't work. Does anyone have a line or two of code that I could use?

Jankapunkt
  • 8,128
  • 4
  • 30
  • 59
W. Mays
  • 19
  • 1
  • 6
  • Please tag a language. In general you'll want `Minimum(Round(number/blockSize,0) * blockSize, ceiling)` – John Wu Apr 03 '18 at 00:46
  • @JohnWu Notice the [tag:processing] tag. Processing is a language. – Kevin Workman Apr 03 '18 at 01:44
  • Can you please be more specific than saying "it didn't work"? What exactly happened? Since you're only asking about a single line of code, can you please post a [mcve] that only shows that line of code in action? Can you please provide some sample input and output? What are the various values of `x_` and `block_size`? What are the corresponding values of `x` that you're trying to generate? – Kevin Workman Apr 03 '18 at 01:45

1 Answers1

0

As you mentioned, in general, floor(x_ / block_size) * block_size will return a value that is a multiple of block_size and will always be less or equal to the original x_. However, you have to make sure that when dividing you don't run into a "loss of precision error", because Java doesn't allow implicitly casting a value into another form when information may be lost (like float to int, but this rule doesn't always apply, see this answer). When dividing the float x_ by the int block_size you need to first convert _x into a float:

int x = floor(_x / (float)block_size) * block_size

For y you can simply swap the parameter names.

douira
  • 506
  • 1
  • 6
  • 22