Log In  

I have been trying to code in a snapping mechanic in a rhythm maze game that I'm working on, but I can't get it to work. The player always ends up at one of the corners of the map or doesn't snap onto the map tiles. I've been using

flr()

but it's not working in ANY way. I want the player to snap onto a map tile when they're not moving, can someone please help me with this?

P#106625 2022-02-09 22:38

1

Have you tried x=8*flr(x/8)? (using x coordinate as an example to show the formula more clearly)

P#106628 2022-02-09 22:50

@kimiyoribaka I haven't tried that, does it work?

P#106637 2022-02-10 01:38

it does! thanks so much dude!

P#106638 2022-02-10 01:39

Err...it should? You might need to adjust the position first to tweak it, though. To explain, the map tiles are 8x8 pixels, so to snap to them, you just need to set the position in pixels to a multiple of 8. Assuming you are using pixels for the position, dividing by 8, rounding to an integer, then multiplying by 8 does that. Just using flr() on the position would snap to the nearest pixel (which wouldn't matter since the drawing functions do that anyway). It doesn't guarantee that the tile would be the correct one though. If you're using 8x8 sprites for the player, adding 4 to the x and y before dividing would make the map tile that gets snapped to more accurate.

P#106639 2022-02-10 01:49

You can also cut down a bit on code size and token use by using the floor divide operator "\", as in the following:

Newx=x\8*8
P#106643 2022-02-10 04:59 ( Edited 2022-02-10 04:59)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 02:28:46 | 0.021s | Q:18