Search code examples
c#xna-4.0

Draw travel distance in XNA 4


So basically i am creating an XNA game at college and i need some help with something as i cannot seem to figure it out myself and i'm pretty new to this.

Basically i have a spaceship with a scrolling background of stars. I have falling asteroids and basically the point of my game is to travel as far as possible without being hit by said asteroids.

I'm really looking for some guidance as to how i could measure a theoretical distance travelled by the ship and then draw it on screen?

Any help would be greatly appreciated.

Many thanks.


Solution

  • Solution A

    Somewhere in your code you are defining the offset of the backdrop for each frame. You could just invert* this value and add it to the total amount every frame:

    totalDistance += -backdropOffset;
    

    If the offset is defined in pixels you have to convert it to your game world unit (kilometers, lightyears, ...) before displaying the distance.

    * If the ship moves forward, the backdrop "slides" in the other direction.


    Solution B (more work but less headaches)

    It is actually not the backdrop that is moving; it's the ship. So why not move the ship and follow it with the camera?

    You will be able to do all kinds of motion. Right now you have to invert every movement of the ship and then apply it to the backdrop. Kind of counter-intuitive, don't you think? So going with this solution your code will be much closer to reality => less headaches during debugging, easier maintenance of your application and you will be quicker when adding new features.

    And of course, getting the total distance would be as trivial as

    var totalDistance = myShip.Position.Y;