2
\$\begingroup\$

The title may not make sense, nor may the question - I'll try nevertheless. I'm attempting to make a game similar to The Powder Toy.
Essentially, this is a physics simulation game, where the users create various structures using the provided materials.
As of now, I've created the grid, and given the user the ability to draw with grey cells (or, Stone).
Of course, this may seem like what I would want - however, all the cells are stored in a two dimensional array. When the user clicks on the screen, the program round the x and y of the cursor to the grid, and adds to the array like that.
What I want to happen is for the cell to change in the place of the cursor, instead of rounding it to the nearest cell.
I've attempted to explain it in this image:
Example
In this image, the green large dots are the mouse position, and the place at which the arrows are pointing are examples of where the cell could be.
Green represents what I want to happen but in most cases doesn't, and red is what does happen, but what I don't want.


Here's part of my function to set the cells:

public void handleInput () {
    if (Gdx.input.isButtonPressed(Buttons.LEFT)) {
        mouse_x = Gdx.input.getX();
        mouse_y = Gdx.input.getY();

        rou_mouse = new Vector2(round2px(mouse_x), round2px(mouse_y));

        set_px (rou_mouse, new Element_stone());
}

Where set_px sets a certain item, this is used to draw to the screen.
I hope this is enough information, but if you would need anymore to solve it, I'd be happy to update the post to include more code.

Oh, and I should probable mention I'm using LibGDX.

\$\endgroup\$
  • 1
    \$\begingroup\$ What is round2px? Is it a LibGDX function or your own? If it's your own, could you post it? \$\endgroup\$ – Christer Jan 27 '16 at 17:43
  • \$\begingroup\$ Let me understand, you want to find the nearest grid center whenever a click is triggered so you could somehow fill that cell with your gray color ? \$\endgroup\$ – Ion Farima Jan 28 '16 at 21:08
  • \$\begingroup\$ @Ion Farima Basically, yeah. The grid is stored as a 2d array, and if you want, I'll update my post to include the draw function. \$\endgroup\$ – Jacob_ Jan 28 '16 at 21:35
1
\$\begingroup\$

What you really want to do is "floor" instead of round. Flooring rounds down, which will select the corner of the cell that the is mouse pointer is in, instead of plain rounding, which rounds to the corner that is closest to the mouse pointer.

int mouseX = Gdx.input.getX();
int mouseY = Gdx.input.getY();

/*
 * cellWidth and cellHeight must be int's; division between two ints chops off the 
 * remainder and returns the whole number (effectually flooring the quotient)
 */
int gridX = mouseX / cellWidth;
int gridY = mouseY / cellHeight;

I would modify your set_px method to take in arguments of type int, instead of a Vector2 (which uses floats and can get confusing).

set_px(gridX, gridY, new Element_stone());

I would also take a look at Java Naming Conventions!

\$\endgroup\$
0
\$\begingroup\$

Ok this is how i would do it.

 public void handleInput () {
                mouse_x = Gdx.input.getX();
                mouse_y = Gdx.input.getY();

                rou_mouse = new Vector2(mouse_x/cellWidth, mouse_y/cellHeight); 

                set_px (rou_mouse, new Element_stone());

        }

The new vector2 arguments should give what cell number your clicking on in relation to your array.

grid[rou_mouse.x][rou_mouse.y]

Just like that

\$\endgroup\$
  • \$\begingroup\$ when I try to do this using my set_px, it doesn't give me an error, yet it only draws to the top left of the screen, wherever I click. And, when I use grid[rou_mouse.x][rou_mouse.y] it gives an error saying it can't convert float to int \$\endgroup\$ – Jacob_ Jan 28 '16 at 20:07
  • \$\begingroup\$ rou_mouse is a Vector2 so obviously it returns float values for x and y and the array index needs int values. You could divide the mouse position by cell dimentions if the grid would be situated on 0,0 so you need to add an offset that should be subtracted from mouse_x and mouse_y in order to get the current clicked cell. \$\endgroup\$ – Ion Farima Jan 28 '16 at 21:13
0
\$\begingroup\$

So if I understood, you want to get the cell that is clicked so you can color it.

private void fillTouchedCell() {
  if (Gdx.input.isTouched()) {
    Cell c = getCellAtTouch();
    if (c != null) {
      //cell clicked, we cant fill it with color
      c.fill(Color.RED);
    }
  }
}

private Cell getCellAtTouch() {

  unpojectedTouch.set(Gdx.input.getX(), Gdx.input.getY(), 0); //insert input into a Vector3
  cam.unproject(unpojectedTouch); //convert to your camera viewport size

  touch.set(unpojectedTouch.x, unpojectedTouch.y);

  for (int x = 0; x < GRID_COLUMNS; x++) {
    for (int y = 0; y < GRID_ROWS; y++) {

      Cell cell = cells[x][y]; // get cell from your 2-side array

      if (touch.x > cell.getX() && touch.x < cell.getX() + cell.getWidth()) {
        if (touch.y > cell.getY() && touch.y < cell.getY() + cell.getHeight()) {
          return cell;
        }
      }
    }
  }

  return null;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.