Snap to Grid

Hi I need to implement a grid on the zx plane which i can place objects on (for an environment editor).

Looking for a few suggestions on how to go about drawing the grid and also what square of the grid the mouse of hovering over with the pick ray (all i can come up with is using a square with a colored outline for each square of the grid, i’m sure theres a more efficient way;)) is there common way to go about this?

Thanks

for the grid just create (or import) a plane and make it wireframe using PolygonAttributes


Appearance temp =  grid.getAppearance();
PolygonAttributes pa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE,PolygonAttributes.CULL_NONE, 0f );
temp.setPolygonAttributes(pa);

i’m not sure about snapping … will let you know shortly

Excellent thanks LaLiLuLeLo, just need a starting point on this one, will give it a try

snapping is a bitch.

Think VERY carefully about what you want your algo to do, and think of all the special cases, before you even begin to design it.

Otherwise, you’ll do it, then decide some aspect is unpleasant GUI, and then find youhave to re-write the entire algo from scratch just to make that one change :frowning:

</voice of bitter experience>

Thanks for the reply, My app needs an environment editor (making a simulation of an ant colony, want to be able to build simple table top ant farm to be used in simulation from simple primatives, nothing fancy) so want a simple snap to grid system where simple primatives, planes, wedges, cubes are used to build the environment.

Do you know of any good tutorials?? just want to get it done properly the first time, as i have plenty other things to develop!!

Thanks :slight_smile:

Eh? Snapping on a flat xy plane is dead easy, just something like:

snapTo(float x, int scale)
{
int temp = Math.round(x / scale);
return temp * scale;
}

You divide by your scale, use integer rounding to snap to the nearest division, then scale back up again. Things get more tricky if you want it snapping to an arbitrary plane, but in that case you just need to extract your tangent vectors and do all your calculations in tangent space (and convert them afterwards, natch).

[quote]Eh? Snapping on a flat xy plane is dead easy, just something like:
[/quote]
Yes, drawing a snap of the current mouse position is easy.

But doing an interactive snapping GUI is not so easy :(. It’s easy to get something “that works” but it tends to be annoying to use.

For instance, when moving objects on a coarse grid, you do NOT want to “just render-snap” because then objects appear not to move for 100 pixels, then suddenly jump a massive distance.

Then the can of worms opens for how you’re going to mix snapping with smooth movement into one cohesive GUI. And it’s easy to get lost in the border-conditions for the snapping behaviour.

It’s even worse if you’re doing drag-n-drop, and to have to be careful about whether you’re measuring movement relative to the cursor start postiion or the object’s last partial snap etc. Funnily enough, if you reset the objects position to wherever you drop it then the GUI becomes VERY unresponsive; instead, you want to cache the last offset of the object when you dropped it, so that if e.g. you move object one pixel to left, causing it to jump left, you can let go and then pick it up and move it one pixel to the right instead of 100 pixels in order to make it jump to the right again.

Or…you just take into account mouse direction and guess where to preset the smooth co-ords to.

See? Gets complex…

Yeah, a good system/gui is tough. There’s also other elements like object-to-object collisions – snapping around other objects. I guess it depends on how well you want the system to be…

If you ever do make it could you post the code please?

I just love starting a good discussion ;D

I’ll be doing the code for my snapping stuff over the christmas, but its my first time considering it in a project so it’ll probably be very specfic to my problem. but i’ll post it if you want. Im doing an ant colony simulation if anyones interrested, its an interesting project from an ai point of view