grid snapping

Im having trouble achieving this type of thing for my game where you have these squares and i need surtin objects that you can drag around to snap to the center of each square when the object is relitivetly near the center and you let go of the mouse. and also i need a way of knowing when the object is in the square for it to recognize that and to recognize when several objects arranged in a surtan pattern next to each other and react.

is this possible? thanks here is a blend to help you understand what im talking about the little triangles are the objects you can click on them and drag to move them to each square and the squares are just one big texture i made in Photoshop and applied to the plane.

Attachments

mousefollow.blend (545 KB)

One of the ray functions can give You texture coordinates (u, v) - from there You should be able to calculate what square Your in and how close to the center. With the help of the orientation and scale of the grid You should be able to calculate where the center is to - Or You should be able to with the help of a few ray samples. From scale, orientation and position of the grid You should be able to pull it of with pure math to. No way is really trivial - all is doable.

To recognize patterns I suggest storing the position in a python matrix (two dimensional list / array). Whenever You move one object it updates what cell its in in the matrix and then check neighbor cells for pattern.

This is all pretty advanced python coding. You could simplify the first one by making the matrix the ‘main’ data and move in the matrix and then calculate the 3d position from that - will probably end up easier but it’s not sure it fits Your gameplay.

You could also place empties in the center of each square and use a near sensor to find the closest one. Many ways to skin this cat.

thanks for the help but i have no idea how to program python.