Drawing order
in
Programming Questions
•
3 years ago
Hi,
The code for this is fairly lengthy and it's hard to isolate just the bit that's relevant here, so I'll hopefully be able to explain it clear enough without needing to post a page of code to wade through:
Basically, I'm drawing a train in 2d with several carriages. The train follows a path and this all works fine and the carriages do everything they're meant to. The only problem I'm having (and I've spent most of today getting a headache over trying variations to make it work) is that when the carriages are drawn one by one from a loop, they overlap the correct way in some directions but overlap wrong in other directions.
Each carriage has an integer ( 0 - 4) associated and each line on the path has a slope calculated at one of 8 angles.
My first way of tackling this problem was making a condition that if the carriages reached a line that I'd named as one where the carriages needed to be drawn in reverse order, I would draw them in reverse order. The simple formula for this was along the lines of:
- for (int z=0; z< 5; z++) {
- if (currentPathSlope == "reverse") {
- i = abs(z-4);
- } else {
- i = z;
- }
- }
Which meant that instead of drawing the carriages in the order of 0, 1, 2.... , when it reached a flagged path it would draw them in the order of 4, 3, 2 .... etc.
This works perfect....
Except....
Imagine a carriage moving along a line flagged as reverse. Say it's the first carriage and so it's numbered 4 and it gets to the end of the line before moving onto the next that isn't flagged. It then gets numbered as carriage 0 so that it's drawn in the opposite order. It works fine except the last carriage in the train - still on the line flagged as reverse - has been numbered as 0 and so it disappears the moment carriage 5 moves onto the next line and becomes 0.
So I'm stuck!
My first line of thought was to keep each carriage numbered consistently throughout the whole path. I can't figure out how to get the loop to draw them in reverse order on command by doing this.
Thanks in advance.
1