### Creating a Space Mapping system

Hello, as I go through various books and practicing I'm writing my own little space simulator app for fun and to reinforce the stuff i'm learning. Problem is now I've done something wrong. My space map is backwards. I'm using x and y coordinates and using nested loops to draw the map based on the user's current location. currently when using the charts command in flight it spits out a nice map, but when you set a course of new x and y, throttle up and movement begins, moving towards the new course reveals that the map is backwards. x and y are reversed as well as moving toward positive course goes in the down or back direction. Not sure what I messed up. Please help!

Its a bit of code so i'm linking to pastebin, hope thats okay.

http://pastebin.com/8Ttve2YR
*BUMP* no one has any input? Should it be in another section maybe?
If you have the following code - where m_x is the X-coordinate, and m_y represent the Y-cordinates:
 ``123456789101112131415161718`` ``````void SpaceShip::Charts() { char space[150][150]; for(int i = (m_x -10);i < (m_x + 10);i++) //row by 80. { for(int j = (m_y - 20);j < (m_y + 20);j++) //columns by 100. { space[i][j]='.'; space[m_x][m_y]='^'; space[10][20]='*'; space[12][24]='O'; cout << space[i][j]; } cout << endl; } //return 0; //This is an error Error - void function should NOT return a value }``````

Then the way the you have the two loops at the moment (with m_y loop inside the m_x loop) means that X goes up/down and Y goes left/right across the page.

BUT People expect the the X-cordinate to go left/right and Y-cordinates up/down - so you need to do the loop the other way around (put m_x loop inside the m_y loop)

its probaly a little advanced...you seen david brabens elite? i loved that
Thanks GuestGulkan. I'll try to fiddle with that and see if it fixes the issue. Hopefully it will also fix the reverse direction as well as the x and y being swapped. I'll report back.

DevonRevenge I've not seen Elite. Just looked it up. Pretty cool. I was mostly inspired to start messing around, learning more C++ and developing thanks to another pioneer, Chris Roberts (Wing Commander) and his new game Star Citizen. Day job is Network engineer, and I do cloud computing architecture and such as well.. so while C++ isn't part of my day to day, it wouldn't hurt long run.. and would open up more doors.
GuestGulkan: Thanks a bunch, You were right on the money, flipping the loop worked great for getting X and Y reversed, then i just had to modify the location referenced map stuff to draw the correct amount of lines. I had tried flipping the values, but not the entire loop. So now its:

 ``1234567891011121314151617`` ``````void SpaceShip::Charts() { char space[150][150]; for(int j = (m_y - 10);j < (m_y + 10);j++) //columns by 100. { for(int i = (m_x -20);i < (m_x + 20);i++) //row by 80. { space[i][j]='.'; space[m_x][m_y]='^'; space[10][20]='*'; space[12][24]='O'; cout << space[i][j]; } cout << endl; } }``````

Perfect. x and y orientation

Movement along X is also working great, movement towards negative goes to the left, positive goes towards the right. Y however is flipped.. and I understand why, its incrementing from top to bottom. so positive goes down as it advances from where it starts drawing. Understanding it is one thing, but I'm not sure how to fix it. It needs to be more like the standard 4 quadrant algebra graph.

Just an additional 1 cent worth:

I like to use Row & Col rather i & j. It is easy to get the right way around because you remember that Rows are Y, and Cols are X.

Also, it will save you from doing this one day:

`space[i][i]='.';`

Using Row & Col, this won't happen because it would rather unlikely for you to do this:

`space[Row][Row]='.';`

I have seen exactly this happen before, someone had 200 lines of code full of i's & j's - no one could see the problem. I suggested doing a find replace - changing them to Row & Col. The error became much easier to spot visually. Even better, one is unlikely to make the error in the first place.

Hope all goes well.
TheIdeasMan: Very good point. I could see how a larger program could get very confusing using abstract variables. Thanks for the tip.
After some tinkering I figured out the backwards Y coords and rather than positively incrementing it rewrote it to do the opposite... works :) I also added the y and x just to show the proper axis label to confirm it was working properly.

 ``123456789101112131415161718192021`` ``````void SpaceShip::Charts() { char space[150][150]; for(int j = (m_y + 10);j > (m_y - 10);j--) //columns { for(int i = (m_x - 20);i < (m_x + 20);i++) //row { space[i][j]=' '; space[m_x][m_y]='^'; space[10][20]='*'; space[12][24]='O'; space[(m_x - 20)][j]= 'y'; space[i][(m_y + 10)]= 'x'; cout << space[i][j]; } cout << endl; } } ``````
Last edited on
Topic archived. No new replies allowed.