This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-10-28 14:57:27

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Snap to Grid?

I was wondering how I could make a 15*15 sprite snap to this grid:

http://i49.tinypic.com/15gyvlv.png


Every square is 16*16.

Offline

 

#2 2012-10-28 15:03:59

zubblewu
Scratcher
Registered: 2011-02-17
Posts: 1000+

Re: Snap to Grid?

set x to ((round ((x position) / (16))) * (16))
set y to ((round ((y position) / (16))) * (16))
You may need to also add a +8 before the rounding and a -8 after, it depends.


........................................................................................................................................................................................................................................

Offline

 

#3 2012-10-28 15:08:43

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Snap to Grid?

zubblewu wrote:

set x to ((round ((x position) / (16))) * (16))
set y to ((round ((y position) / (16))) * (16))
You may need to also add a +8 before the rounding and a -8 after, it depends.

I tried that before, but it didn't work. I'll try it with the +8/-8 though.

Offline

 

#4 2012-10-28 15:10:00

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Snap to Grid?

Not working still...

Offline

 

#5 2012-10-28 15:12:45

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Snap to Grid?

ImagineIt wrote:

Not working still...

And how is it not working?

Last edited by ErnieParke (2012-10-28 15:13:33)


http://i46.tinypic.com/35ismmc.png

Offline

 

#6 2012-10-28 15:19:16

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Snap to Grid?

ErnieParke wrote:

ImagineIt wrote:

Not working still...

And how is it not working?

It snaps to the wrong places. ex: In the middle of two.

Offline

 

#7 2012-10-28 15:27:48

Molybdenum
Scratcher
Registered: 2012-06-17
Posts: 1000+

Re: Snap to Grid?

ImagineIt wrote:

zubblewu wrote:

set x to ((round ((x position) / (16))) * (16))
set y to ((round ((y position) / (16))) * (16))
You may need to also add a +8 before the rounding and a -8 after, it depends.

I tried that before, but it didn't work. I'll try it with the +8/-8 though.

To this, but try changing the + whatevers.


"The Enrichment Center is required to remind you that you will be baked, and then there will be cake."
(|Balls and Platforms: Stay on!|) (|NaOS-H: An operating system... Or is it?|)

Offline

 

#8 2012-10-28 15:28:02

zubblewu
Scratcher
Registered: 2011-02-17
Posts: 1000+

Re: Snap to Grid?

Can you upload what you have so far?
I'll fix it. I know what's happening, I've had the same problem.


........................................................................................................................................................................................................................................

Offline

 

#9 2012-10-28 15:28:05

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Snap to Grid?

ImagineIt wrote:

ErnieParke wrote:

ImagineIt wrote:

Not working still...

And how is it not working?

It snaps to the wrong places. ex: In the middle of two.

So could you do something for me? If the sprite was at (0,0) where would it go? How about (8,8) and (16,16)?


http://i46.tinypic.com/35ismmc.png

Offline

 

#10 2012-10-28 15:39:48

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Snap to Grid?

ErnieParke wrote:

ImagineIt wrote:

ErnieParke wrote:

And how is it not working?

It snaps to the wrong places. ex: In the middle of two.

So could you do something for me? If the sprite was at (0,0) where would it go? How about (8,8) and (16,16)?

0,0(Starting at the bottom left) = -171, -172; 8,8 = -52, -51; 16,16 = 69, 68. Thanks for your help guys. I'll try to upload it.

Edit: Wait, I just had an Idea. I'll upload after I try it.

Edit: OK, Now the x and y coordinates will always be equal when snapped to the grid, but they still don't get snapped to it.

Last edited by ImagineIt (2012-10-28 15:46:52)

Offline

 

#11 2012-10-28 15:50:48

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Snap to Grid?

ImagineIt wrote:

ErnieParke wrote:

ImagineIt wrote:


It snaps to the wrong places. ex: In the middle of two.

So could you do something for me? If the sprite was at (0,0) where would it go? How about (8,8) and (16,16)?

0,0(Starting at the bottom left) = -171, -172; 8,8 = -52, -51; 16,16 = 69, 68. Thanks for your help guys. I'll try to upload it.

Edit: Wait, I just had an Idea. I'll upload after I try it.

Edit: OK, Now the x and y coordinates will always be equal when snapped to the grid, but they still don't get snapped to it.

When I said (0,0), I meant in Scratch, not your grid. Also, do you want the square snapped to the middle of squares or at the points where 4 squares touch?


http://i46.tinypic.com/35ismmc.png

Offline

 

#12 2012-10-28 15:53:17

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Snap to Grid?

ErnieParke wrote:

ImagineIt wrote:

ErnieParke wrote:


So could you do something for me? If the sprite was at (0,0) where would it go? How about (8,8) and (16,16)?

0,0(Starting at the bottom left) = -171, -172; 8,8 = -52, -51; 16,16 = 69, 68. Thanks for your help guys. I'll try to upload it.

Edit: Wait, I just had an Idea. I'll upload after I try it.

Edit: OK, Now the x and y coordinates will always be equal when snapped to the grid, but they still don't get snapped to it.

When I said (0,0), I meant in Scratch, not your grid. Also, do you want the square snapped to the middle of squares or at the points where 4 squares touch?

In the middle of squares. I'm uploading it now.

Offline

 

#13 2012-10-28 15:57:17

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Offline

 

#14 2012-10-28 16:02:48

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Snap to Grid?

Snap To Grid Wiki Article.

What you probably want to do is "shift" your sprite to match your grid.

set x to (((16)*(round(((x position)-(shiftx))/(16))))+(shiftx))
set y to (((16)*(round(((y position)-(shifty))/(16))))+(shifty))


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#15 2012-10-28 16:10:22

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Snap to Grid?

Hm... That works at the bottom left, but then once it gets further away, it will not snap in still. How can I automatically set the shift?

Offline

 

#16 2012-10-28 19:13:19

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Snap to Grid?

Bump.

Offline

 

#17 2012-10-28 21:04:19

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Snap to Grid?

ImagineIt wrote:

Hm... That works at the bottom left, but then once it gets further away, it will not snap in still. How can I automatically set the shift?

Your problem may be that, though every square is 16x16, the lines between the squares have a thickness too.  If the lines are 1 pixel thick, use 17 instead of 16.


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#18 2012-10-28 21:08:37

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Snap to Grid?

MoreGamesNow wrote:

ImagineIt wrote:

Hm... That works at the bottom left, but then once it gets further away, it will not snap in still. How can I automatically set the shift?

Your problem may be that, though every square is 16x16, the lines between the squares have a thickness too.  If the lines are 1 pixel thick, use 17 instead of 16.

No, the inside of the square is 15 pixels, the outline is 1 pixel. I just figured out what the problem is though. The pixels are shared instead of being separate. That's what causes it to go out of place. I just don't WANT them to not share pixels. So, we just need to find a way to change the snap. Oh! I think I found out how! Let me test it now!

Offline

 

#19 2012-10-28 21:15:24

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Snap to Grid?

It's working! Thanks for all of your help guys!

Offline

 

#20 2012-10-28 21:27:09

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Snap to Grid?

Wait... Not fully... When I move the mouse one pixel, it can have weird effects.

Last edited by ImagineIt (2012-10-28 21:29:10)

Offline

 

#21 2012-10-28 23:17:35

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Snap to Grid?

I just did a pixel count and it should be 15 (14 on the inside and border of 1 pixel).  Can you update the project in the link above with the latest version?


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

Board footer