Re: Grid based block placement

For questions about using Classic.

Post » Mon Apr 04, 2011 11:18 am

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.
B
3
G
1
Posts: 20
Reputation: 540

Post » Mon Apr 04, 2011 10:38 pm

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.
B
2
G
2
Posts: 48
Reputation: 746

Post » Tue Apr 05, 2011 5:48 am

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)?
B
3
G
1
Posts: 20
Reputation: 540

Post » Tue Apr 05, 2011 8:43 am

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.
B
2
S
2
G
3
Posts: 406
Reputation: 2,062

Post » Tue Apr 05, 2011 11:41 am

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?
B
3
G
1
Posts: 20
Reputation: 540

Post » Tue Apr 05, 2011 2:48 pm

This should help. I think.

[url:3isi5k8q]http://db.tt/w0uK33D[/url:3isi5k8q]
B
46
S
23
G
7
Posts: 459
Reputation: 6,763

Post » Tue Apr 05, 2011 9:41 pm

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!
B
3
G
1
Posts: 20
Reputation: 540

Post » Wed Apr 06, 2011 9:34 am

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
B
3
G
1
Posts: 20
Reputation: 540


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 4 guests