Re: Grid based block placement

This forum is currently in read-only mode.
From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • I am making a 2d platformer that allows for the creation and destruction of blocks, but unfortunately i cant seem to make a grid (32x32) that limits the area that the blocks are placed at like the one featured here: http://www.indiedb.com/games/survivors- ... a#imagebox

    any help or suggestions to do this would be appreciated.

  • Do you mean the placement at a certain position?

    If so:

    System->create object at (x + MouseX - MouseX % 32,

    y + MouseY - MouseY % 32)

    where (x,y) is equal to the placement of your image's hotspot

    If you meant limiting the placement area then just add a condition to your event that ignores input from the mouse when it is within a certain area.

    System->compare (MouseX > 100)

    System->compare (MouseX < 500)

    System->compare (MouseY > 100)

    System->compare (MouseY < 500)

    You could also "cheat" and place a sprite in the area you want to limit construction to. Then use the condition:

    System->Mouse is over sprite

    I apologize if anything's wrong. My excuse - I started working with construct for the first time in several months yesterday.

  • I set it up but how do i find the image hot spot?

    Also how can i set it so that my mouse must follow these same guidelines as i move it across the screen (So it must not stray from the 32x32 grid)?

  • Keeping an object that follows a mouse snapping to a grid is very simple, and you'll find lost of examples of it on the forums.

    But here's how to do it, along with an explanation of the logic behind the maths.

    To snap an object to the mouse for a 32x32 grid (for example), you update the objects position like this:

    (Pseudo Code)

    ObjectXPosition = Int(ObjectXPosition/32)*32

    ObjectYPosition = Int(ObjectYPosition/32)*32

    ...changing the 32s to whatever grid size you want.

    Now, it looks like it's just dividing by 32 and then multiplying by 32, which would seem to get you back to the same figure, but the important part is the INT, and the brackets.

    By dividing it's position by 32 and then turning it into an INT, you effectively cut off anything that's not an exact division of 32.

    Then you multiply by the second 32, which multiplies the new Integer value, and you have your exact, on-grid position.

    For instance, Int(35/32)*32=32 [the value 35 becomes 32, as would values like 49 or 57]

    Int(73/32)*32=64 [the value 73 becomes 64, as would any value from 65 to 95]

    Krush.

  • Thanks for the reply

    But i had set this up ( thanks to random ) now i am trying to make my mouse ( wich will soon be a block symbol ) move on a 32/32 grid ...check the picture at the start.

    Edit : also in order to place a block correctly i need to aim for the bottom left of were i want it to be or it jumps into the wrong grid...any fix for this?

  • This should help. I think.

    http://db.tt/w0uK33D

  • Thank you so much Minor, this is exactly what i what.

    When i get rich and famous ill be sure to follow you on twitter!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Oh i found i problem that after about 30 minutes i cant figure out how to fix it... when i move the screen down the cursor(red box in your example) loses its place and eventually placed blocks totally miss were they are aimed at. I figured it out that it was the camera movement doing this, any ideas?

    EDIT: nevermind i had the wrong scroll rate

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)