r/howdidtheycodeit • u/detroitmatt • Aug 18 '21
Question How did Cities Skylines code the road placement tool?
I can imagine something like this:
class StraightRoadPlacementTool {
private RoadNode LastNode;
public float Height;
void onClick() {
var newNodePosition = GetClickLocation() + new Vector3(0, 0, Height);
var newNode = new Node(newNodePosition);
if(LastNode == null) {
LastNode = newNode;
} else {
ConnectNodes(LastNode, newNode);
}
}
}But I don't know what ConnectNodes would look like, and I don't know what a CurvedRoadTool would look like. I know there'd be a bezier curve and a control point in there somewhere, but I can't put it into words.
Maybe a CurvedRoadTool wouldn't even have a bezier curve, you'd click once to start the road, click again to set the control point, click a third time to complete the "triangle", and the "angles" of the road are just the lines of the triangle, but then I still don't know how ConnectNodes is visually smoothing the curve out.
Anyway, what do you think?
Edit: And actually, now that I think about it, even this wouldn't work because there's a preview while you move the mouse around before you click. How did they do that?
17
u/dex3r Aug 18 '21
I'm not gonna tell you how they did it, since I have no idea, but I can tell you this: C:S was written in Unity, in C#, and can be decompiled using tools like Just Decompile. There you can look at the source code yourself. I've done it a few years ago, for similar reasons like yours, and it went quite well.
It's possible they upgraded to IL2CPP compilation since I did it, so you might want to find an old build to do that, since you cannot decompile IL2CPP code to C#.
7
u/Archawn Aug 19 '21
See this excellent explanation by red blob games: https://www.redblobgames.com/articles/curved-paths/
11
u/_stice_ Aug 19 '21
Not a game developer but i am a software programmer and i have taken computational geometry in college and have played C:S a lot.
Just like you'd have an onClick event handler, you would have an onMouseMove event handler with very much the same method like getCursorPosition(), and the rest of the logic would look very similar to how you wrote it, so don't let that be a source of uncertainty for you, it's exactly how you said it.
As you might already know since you mentioned the word "bezier", the idea is that if you represent a curve using something like y = ax2 + bx + c on screen or when designing a car (yes this is in 2D for now, it's just a throwaway example), it's really unintuitive to see what values of a, b, c would result in what curve. You can't "draw" with this, it has no clear relation to positions of the cursor or anything. A bezier curve is just a way the above equation (quadratic in this case) is split and re-expressed in terms of other things which are available: the positions of clicks or cursors. To be precise, it would still only ever result in a quadratic equation and would need 3 inputs (each some combination of a, b, c) because maths, but the 3 inputs map to something you know, like the starting point of the curve A, the end point of the curve B, and maybe a 3rd point C such that the curve is tangent to AB and BC. In fact, you can plug these values into y = ax2 + bx + c and come up with the quadratic bezier equation yourself right now, if you spend some time on it.
(Moreover, they did it in such a way that you could have a single parameter t such that when t = 0 you plug it into the equation and it gives you the starting point, for t = 1 it gives you the end point, and t = any value in between gives you a linearly proportional point on the curve.)
Think ms paint curve tool: that's just a cubic bezier (the above one was quadratic), you are specifying starting point A, end point D, some point B in between (note the curve doesn't pas through B but the curve will be tangential to the invisible line AB), and some point C in between (same thing as B, but with the invisible line CD).
Now you might guess how this fits into the scenario you laid out. IF in fact they're using beziers, notice that you'd need 3 points (from clicks or mouse move events) to make a curved road in cities skylines, which fits in with what i explained earlier. The resultant drawn curve needs to just draw a bezier using the lastnode and TWO newnodes (not 1 . . . there's an infinite number of curves you can draw using just 2 points).
Some caveats:
For stuff like this, in games as well as in design fields, they use B-splines or some other tools, which are almost exactly the same concept (of re-expressing a polynomial equation), but they'd have additional things like multiple points the curve passes through, weights which express "how curved" the road is at that point, etc. which makes it even more intuitive to draw using. It's by no means inaccessible to figure out.
Hope all of this helps.
3
u/create_a_new-account Aug 19 '21
you might find this a little interesting
https://www.youtube.com/watch?v=Njosg4Sz0tw
0
u/[deleted] Aug 18 '21
[deleted] — view deleted comment