A Graphics/Math Puzzle

Given, A grid that where the Cells are W x W in size, this grid is static as in does NOT move
Given, A Tile “A” of the same size W x W, that DOES move either Horizontal or Vertical (never diaganol)

The Amount the tile “A” moves varies, but is a fractional value of W

There is an event that fires when ever tile “A” moves, so I can get its current postion relative to the grid

I need to know when Tile “A” is aligned wth any cell in the Grid, however it rarely hits the grid cell EXACTLY

for example here are three points in time

 (1240.0, 1478.5092773437500) : 155.0,184.81 
 (1240.0, 1479.7102050781250) : 155.0,184.96 
 (1240.0, 1480.9090576171875) : 155.0,185.10 

the first two values are the tile “A” actual postion. the 2nd pair is the cell postion it is relative too
notice that none line up exactly (it does on X, but not Y)… the cell size here is 80

The purpose is to start moving Tile “A”, and when the user commands it to stop, it continues to move until it hits the next cell boundary. As in Tile “A” must alway start and stop aligned with a cell regardless of the increment it was told to move

Note : this test started with Tile “A” like this

(1240.0, 1400.0) : 155.0,175.0  

this seems to work… if anyone has a better idea, let me know :slight_smile:

override func update(_ currentTime: TimeInterval) {
        scene?.camera?.position = theACTOR!.position
        
        // check if actor is on even tile position
        let x = adjust(theACTOR!.position.x)
        let y = adjust(theACTOR!.position.y)
        if x==true && y==true  {
            print("good\(currentTime) \(theACTOR!.position) : \(x),\(y) :\(tileSize)")
        } else {
            print("bad \(currentTime) \(theACTOR!.position) : \(x),\(y) :\(tileSize)")
        }
        func adjust(_ n: CGFloat) -> Bool {
            let threshold : CGFloat = 4.0
            let offset = abs((n-(tileSize/2)).truncatingRemainder(dividingBy:tileSize))
            if offset <= threshold || offset>=tileSize-threshold { return true }
            return false
        }
    }