[SOLVED] How to have an object detect an object?

Hello

I have been trying to figure how would I was a square detect if there another square is above it below, left, or right. So if there no square on lets say above it you can swipe up. I sort had it figured out but the method I had does work as it requires so many if statements. What would be a simple way I can make sure that any square detects an available empty spot to slide towards to and a square that indicates that the square that is trying to slide cannot slide to that certain side.

Well I’m a fan of doing things myself for simple apps rather than using physics… which is more… “alive” and not suitable for smooth accuracy.

I think we need more information. Is this kind of like those sliding picture puzzles with the missing squares? Or something similar? There are many ways to test how squares overlap… could check every frame if their dimensions are overlapping (don’t do too many if statements… you need loops instead). Or you could set up a system where you KNOW which squares on a grid are currently occupied. (second option is robust but your squares would have to be stuck on ‘tracks’ so to speak - like those picture puzzles)

I guess you could build a table structure of rows and columns within that row. 

local rows = {}

rows[1] = [1, 1, 1, 1]

rows[2] = [1, 0, 1, 1]

rows[3] = [1, 1, 1, 1]

rows[4] = [1, 1, 1, 1]

Where 0 is free slot.

If you drag rows[3][2] upwards, you check if rows[2][2] is available. Or have a variable that hold the location of 0 and update as necessary. 

@craig yes is a sliding puzzle game but is not for missing squares. I will describe it visually ok so you are given this in the game

https://www.dropbox.com/s/3qp87qulwuzh12k/Screen%20Shot%202013-09-02%20at%206.29.00%20PM.png

and you must have it like this

https://www.dropbox.com/s/8wo63dkvbk07lyl/Screen%20Shot%202013-09-02%20at%206.29.17%20PM.png

and the only way to solve it is you have to slide the squares.

Now how would I prevent overlap on the square and make sure there is an available space to slide the puzzle

I would go with jonjonsson, you will probably need to go with a 2d array and check before render.

I have many ideas how to do that. Jonjonssons solution is good IF you are happy for your square to kind of move incrementally into the ‘empty’ square space. Or… do you want it to move totally freely in both the X & Y directions? I may be able to help either way but you need to describe in greater detail how these squares are to move.

For instance if you hold and drag one downwards… will it move and ‘snap’ in the next space or do you want it to ‘hug’ on to the finger wherever it goes - that second option may prove more difficult for you.

I want it move freely both X & Y direction. Ok for the square to move you tap any square you want to move. Once a square is picked you swipe whatever side you want the square to slide.

Yes, the second option could possibly be handled with something like so:

If your blocks are 100, 100 then you could capture the x and y and then use that to check a table grid:

local row = math.ceil( event.y / 100 )

local col = math.ceil( event.x / 100 )

–check the grid

local spot = grid[row][col]

You would probably need to run the check on the “moved” event.

Ok after many hours of using @jon method I was able to prevent the squares from overlapping when I swiped. I sort of modified his method and used this instead

local grid = {  {1,2,3,4},    {5,6,7,8},  {9,10,11,12} }

Personally this is better for me as I can keep track what square hit what grid setting it to 0. And if they moved out of the grid it resets backs to the original number.

Thanks a lot everyone for helping. I guess I learn something from this.

Well I’m a fan of doing things myself for simple apps rather than using physics… which is more… “alive” and not suitable for smooth accuracy.

I think we need more information. Is this kind of like those sliding picture puzzles with the missing squares? Or something similar? There are many ways to test how squares overlap… could check every frame if their dimensions are overlapping (don’t do too many if statements… you need loops instead). Or you could set up a system where you KNOW which squares on a grid are currently occupied. (second option is robust but your squares would have to be stuck on ‘tracks’ so to speak - like those picture puzzles)

I guess you could build a table structure of rows and columns within that row. 

local rows = {}

rows[1] = [1, 1, 1, 1]

rows[2] = [1, 0, 1, 1]

rows[3] = [1, 1, 1, 1]

rows[4] = [1, 1, 1, 1]

Where 0 is free slot.

If you drag rows[3][2] upwards, you check if rows[2][2] is available. Or have a variable that hold the location of 0 and update as necessary. 

@craig yes is a sliding puzzle game but is not for missing squares. I will describe it visually ok so you are given this in the game

https://www.dropbox.com/s/3qp87qulwuzh12k/Screen%20Shot%202013-09-02%20at%206.29.00%20PM.png

and you must have it like this

https://www.dropbox.com/s/8wo63dkvbk07lyl/Screen%20Shot%202013-09-02%20at%206.29.17%20PM.png

and the only way to solve it is you have to slide the squares.

Now how would I prevent overlap on the square and make sure there is an available space to slide the puzzle

I would go with jonjonsson, you will probably need to go with a 2d array and check before render.

I have many ideas how to do that. Jonjonssons solution is good IF you are happy for your square to kind of move incrementally into the ‘empty’ square space. Or… do you want it to move totally freely in both the X & Y directions? I may be able to help either way but you need to describe in greater detail how these squares are to move.

For instance if you hold and drag one downwards… will it move and ‘snap’ in the next space or do you want it to ‘hug’ on to the finger wherever it goes - that second option may prove more difficult for you.

I want it move freely both X & Y direction. Ok for the square to move you tap any square you want to move. Once a square is picked you swipe whatever side you want the square to slide.

Yes, the second option could possibly be handled with something like so:

If your blocks are 100, 100 then you could capture the x and y and then use that to check a table grid:

local row = math.ceil( event.y / 100 )

local col = math.ceil( event.x / 100 )

–check the grid

local spot = grid[row][col]

You would probably need to run the check on the “moved” event.

Ok after many hours of using @jon method I was able to prevent the squares from overlapping when I swiped. I sort of modified his method and used this instead

local grid = {  {1,2,3,4},    {5,6,7,8},  {9,10,11,12} }

Personally this is better for me as I can keep track what square hit what grid setting it to 0. And if they moved out of the grid it resets backs to the original number.

Thanks a lot everyone for helping. I guess I learn something from this.