PDA

View Full Version : Snapping pieces of "track" together



jdarling
09-01-2008, 03:46 PM
I've been playing with an idea for a few days now in my "spare time" and have run into some problems. The basic concept is to let users build "tracks" from a part library. This is quite simple to do when you have straights and 90 angles, but when I start throwing in curves that are say 45 deg rounded it becomes more difficult.

Has anyone worked on a project where you would snap parts together in an arbitrary fashion, and if so do you mind sharing some of your wisdom (or source code :))?

jdarling
09-01-2008, 05:00 PM
A bit more information that I think I have found. I think I would be best to store vectors for connection edges associated with "tracks" and utilize those along with relative positioning. Does this seem to make sense, or is there a better way?

Traveler
09-01-2008, 05:18 PM
Is there some kind of tiled map involved, where you place these parts onto or I am thinking in the wrong direction?

jdarling
09-01-2008, 07:16 PM
I can't actually utilize a tile map of any kind in this case. Think of it this way; you have a piece of land and have to build roads and train tracks around the land. You have a library of track pieces (cuz roads are easy) and you must stay inside their contraints. For example you know that a O27-8 curve is 1/8th of a 27" circle, course you could have O27-16 or O27-4 as well as O31-8, etc... So the placement is freeform. But if you place a piece next to another piece they should "snap" together to form a loose group. That way I can grab an entire group and rotate them free form to any angle.

Basically a model railroad track layout software scheme. Problem is all model railroad layout software is closed source and pay-ware :(. This design isn't my final goal, but its my first goal as it seems the difficult part to get out of the way.

Traveler
09-01-2008, 10:10 PM
Ok, so it doesn't matter what kind of track (size, angle) you use, as long as they are of the same type, they should connect. And not only with a single connection, but also to make a set complete.

jdarling
10-01-2008, 01:29 PM
Your on the right track (all puns intended). Though you may have different sized track types on the same screen, they would never connect with one another.

A picture might be worth 1000 words in this case:
http://www.postimage.org/aV55RDpi.jpg (http://www.postimage.org/image.php?v=aV55RDpi)

As you can see, the magenta track is the same as the black only rotated 17 degrees. But, when you go to add another piece of track to the layout you now have much more complicated math. I'm thinking I need to keep a set of connection vectors for each track piece, call them connections, each connection would know of two points and how they should be placed in relation to the root connection. This way I can rotate the root connection and calculate the child connections. Unfortunately, the math is a bit above my head, especially when I start getting into strange curves.

I did figure out that to "Snap" the pieces together I had to iterate my known list of connections and see what the closest one was, then check that distance against my Snap distance to see if I should snap the piece to any other pieces.

Traveler
10-01-2008, 05:17 PM
Do you have fixed sized and angled pieces? If so, then isn't it so that it doesn't matter which angle the entire set is to match a single piece?

Ok, here's an idea

http://www.gameprogrammer.net/pics/outside/railroadtrack.jpg

Consider the image above. I first select a piece (1/3 of a 90 degree piece) which I want to use and then select the piece I want it to connect to. The game calculates the correct angle (30 I believe) and connects the piece.
In case the entire set is rotated with a given degree, just internally reset the entire set to 0 degrees, and make the match, then revert the set back to the rotation value it was before the match and you're good.

jdarling
10-01-2008, 07:56 PM
Thats pretty much what I want to do (btw: Do you have the source to that) except that I want it to be done with drag and drop and not such pretty graphics in the end :).

PS: I have managed to get most of this working quite well, in fact I'm only a few short steps away from having something to show off. The hardest part now is drawing an arc from a known 3 points (Center, Start, and End) knowing that the Arc might not be a true circular piece.

Traveler
11-01-2008, 09:06 AM
So I suppose it's all a matter of getting the math right, now.


(btw: Do you have the source to that).

Sry, that was just something I made up in Photoshop. There's no sourcecode I'm afraid.

Looking forward to the endresult :wink:

NecroDOME
13-01-2008, 12:50 PM
What you also can try is to generate tracks from a line. you know the direction of a line, than you can just generate some... This way it's totally freeform and not to hard to make I thing as you have your "connection points"

NecroDOME
13-01-2008, 05:07 PM
Just came across this site: http://www.unibw.de/inf2/DiaGen/examples.html

Maybe it's somehow useful? didn't take a look at it, but it has a "Rail editor".

jdarling
14-01-2008, 01:34 PM
Thats a good looking package. I'll take a look at it and see if there is anything I can use from it.

Thanks Necro.

Traveler, yep its all a matter of math right now. Actually I've got almost everything working now (good weekend) except I can't figure out how to read the PPI information for the screen in a cross platform manor. Thus I can't guarntee that at 1:1 viewing 1 inch is actually 1 inch. Screen resolution and monitor size/PPI greatly affect the results :(. For example on my desktop at work with the 19" monitor its about 24 PPI, while on my wifes laptop with the 14" LCD at home its more around 36 PPI :(. Guess I'll have to play more or give the user a way to select it.