How do I make automatic toggling of the roads?

  • Hi,

    I am making a city builder game of some sort and I have one big problem with roads. I personally do not place the roads on the map, because the player does that so I can't toggle them in the editor. I have only 4-directional roads, but I don't want to code every possibility of road placement, so I need some algorithm that will compare where the nearest roads are and toggle the road to connect to them. Any help from you guys is appreciated. Thanks!

  • Can you elaborate on 4-directional roads? Are your roads constrained to a grid? How many different types of intersections can you have? Are you using tilemaps or sprites?

    Overlap at offset might be of use. Test each direction for existing roads, and set animation frame with the correct intersection.

    An array storing the data of what is where could also be very useful in a grid based system, or something similar can be done with a tilemap as well.

  • Yes, I can. Yes, thery are in a grid. I have 4 types of roads (straight, straight with one connected road, 4 directional intersection, curved road).

    I will look at what you said. Seems pretty useful. Thanks.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The key information is for each direction around each road, does it contain a road or not?

    I would use four boolean instance variables on your road sprite: up, down, left, and right. They will be true or false -if there is a road at that position it will be true, otherwise false. You can use 4 overlap at offset events to check each position, and set the variable.

    Once you have that information for each sprite, you'll need to set the correct animation frame based on the instance variables.

    The most straightforward way would be to have a frame for each variation of the curved road or T-intersection, but I'm sure there can be a way to reduce the frames required by rotating the image and using mirror when appropriate.

  • Thanks. This was really helpful. It is now more clear to me with those 4 boolean variables.

    Btw I did that with rotating the road by an angle before.

  • Search for "bitwise" with the forum search. The method is called bitwise tilemaps, and is usually done with terrain, but it would work perfect for roads.

  • Cool, thanks R0J0! I've used a binary sort of approach before but I never knew what it was called or how to explain it coherently. Now I know

    Here is a link I found from another thread -

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)