Snap to grid?

Get help using Construct 2

Post » Sun Jan 01, 2012 10:21 pm

Is there a easy way to snap to grid? With a drag and drop mechanics?

Or should i use this example? http://www.scirra.com/forum/grid-generation-does-not-work_topic44812.html and place it on the nearest grid to the mouse? But the how do i get the nearest to the mouse, Distance(Mouse.X,Mouse.y,?,?)
B
6
S
1
G
1
Posts: 42
Reputation: 810

Post » Sun Jan 01, 2012 10:34 pm

"Pick nearest" is a common condition.

You select the object type ("Grid" here I guess) and input the position you want to test (here mouse.x and mouse.y).
This will pick the nearest instance of the Grid object to your mouse cursor.

For drag/drop mechanics I suggest you to check the how do I FAQ, I think there are a few examples of drag/drop listed in there.

The "snap to grid" effect is achieved when you drop an object by spawning it in the middle of the current cell instead of the mouse position where it was droped.
It depends a great deal on how you setup your grid.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Sun Jan 01, 2012 10:47 pm

@Kyatric

I looked at the manual entry, But didnt really understand how to use the expression, I made the grid part, Can you help me place the objects onto the grid please?

Here is my capx, http://dl.dropbox.com/u/51270524/Tower%20Defense/Tower%20Defense.capxDiggyDog2012-01-01 22:48:15
B
6
S
1
G
1
Posts: 42
Reputation: 810

Post » Sun Jan 01, 2012 10:59 pm

nah it's far simpler than that.
Snapping is a kind of value rounding. For instance if your round off float value, you obtain a snapping to exactly 1 px.
To achieve the same kind of effect with wider cells you just have to scale down values, round them up and scale them back up.
That's the idea.
Mathematically it looks like that :

Code: Select all
System: Every Tick
   -> Sprite: set X to round(mouse.X/cellsize)*cellsize
   -> Sprite: set Y to round(mouse.Y/cellsize)*cellsize

now you might end up with some offset depending on either:
1. the position of the pivot point of your sprite
Or
2. the starting position or origin of your grid


for the first one you just have to add a position offset to the result
Code: Select all
System: Every Tick
   -> Sprite: set X to round(mouse.X/cellsize)*cellsize+Sprite.pivotOffsetX
   -> Sprite: set Y to round(mouse.Y/cellsize)*cellsize+Sprite.pivotOffsetY

for the second you have to offset the position before calculation and then offset it back (its like a change of origin)
Code: Select all
System: Every Tick
   -> Sprite: set X to round((mouse.X-gridOffsetX)/cellsize)*cellsize+gridOffsetX
   -> Sprite: set Y to round((mouse.Y-gridOffsetY)/cellsize)*cellsize+gridOffsetY


I hope I'm not too wrong :D
Last edited by Yann on Sun Jun 22, 2014 10:36 am, edited 4 times in total.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Mar 26, 2012 1:12 pm

Great post, Yann! Glad I found it.
B
20
S
9
G
2
Posts: 15
Reputation: 2,594

Post » Thu Jul 12, 2012 4:18 am

Yann, Thank you. This is exactly what I needed. Very elegant solution.
B
16
S
1
G
3
Posts: 43
Reputation: 2,597

Post » Thu Jul 12, 2012 4:44 pm

Yuup Yann nailed it as usual. Couldnt have said it better mself
B
13
S
5
G
2
Posts: 306
Reputation: 3,262

Post » Sat Jul 14, 2012 12:12 am

Good stuff
B
22
S
5
G
3
Posts: 194
Reputation: 5,855

Post » Tue Jan 01, 2013 4:06 pm

Hi guys,

I just started using construct and I wanted to give a try at making a game operate similar to PvZ. So far I am trying to get the snap to grid to work but I'm having some slight issues.

Here is my project file so that you can see what I have so far:
https://dl.dropbox.com/u/74662263/TowerDefense_Test.capx
B
3
Posts: 7
Reputation: 389

Post » Tue Jan 01, 2013 4:18 pm

@goldboyj
First of all, welcome to the community!

It's great you've added your .capx to the post, but it would be even better if you could also point out exactly what the problem is. It all depends on what you want to happen, so it's really just a guessing game for me when looking at your .capx at this point.

PS: You could also have made a new thread instead of resurrecting this old one. That way you get notifications when others post in the thread, instead of the guy who started this thread a year ago.
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Next

Return to How do I....?

Who is online

Users browsing this forum: marekmk and 3 guests