Daniel Tian’s Professional Blog

Icon

Just another WordPress.com weblog

Bouncing triangle problem fixed

I managed to fix the bouncing triangle problem. I have a bouncing triangle on the lower right hand corner of my game. Normally it works fine, but when I drag the game window around, the triangle doesn’t reverse its direction and continues to travel in the last direction it was going in until I release the window. If I’ve been dragging the window for a while, the triangle will end up somewhere in the middle of the screen.

This is my code after the fix:

public void Update(GameTime gameTime)
{
    arrowMoveDistance = arrowDirection * new Vector2(0, (float)(gameTime.ElapsedGameTime.TotalMilliseconds / arrowBounceSpeedInMilliseconds));
    arrowTotalDistanceMoved += Math.Abs(arrowMoveDistance.Y);

    if (arrowTotalDistanceMoved >= arrowBounceDistanceInPixels)
    {
        arrowDirection = -arrowDirection;
        arrowTotalDistanceMoved = 0;
    }

    arrowCurrentPosition += arrowMoveDistance;
}

All I had to do was change gameTime.ElapsedRealTime.TotalMilliseconds to gameTime.ElapsedGameTime.TotalMilliseconds. It turns out that when you drag the window around, ElapsedGameTime stops updating but ElapsedRealTime still does, which makes sense because real time keeps on ticking when you move a window around. This is another case of one of those things where you don’t really think about it until it becomes a problem.

The reason why I originally decided to use ElapsedRealTime is because ElapsedGameTime will always return the same value, even if the code runs slower than it should. So for example, if ElapsedGameTime should take 16 ms to run (1/60 of a second) but actually takes 32 ms, ElapsedGameTime will still return 16 ms, but ElapsedRealTime will return the correct value, 32 ms. If the code takes less than 16 ms, both will return 16 ms because that is the time interval between update calls (assuming 60 FPS for the game). This isn’t so much of a problem when framerate is limited but can cause issues when Game.IsFixedTimeStep and GraphicsDeviceManager.SynchronizeWithVerticalRetrace are false, which removes the framerate limit. When the framerate is pushing up into the thousands, time-critical code can exhibit strange behavior if ElapsedGameTime is used because it doesn’t accurately reflect the amount of time that has passed.

Filed under: Uncategorized, , , , , ,

Programming in XNA

First Post?
I find that the first post of any blog is always the hardest, because what you write in the first post usually sets the tone for the rest of the blog. My purpose for creating this blog is to keep a history of the programming activities that I work on and the struggles I’ve come across, and hopefully it can be a fun memory to look back on when I’m old and wizened, and laugh at the ‘good ol’ days’ when I was stuck on such a simple problem.

Advantages of XNA
With that said, let’s get to the real meat of this post. I’ve recently started to learn my way around XNA. From my understanding, XNA is a SDK provided by Microsoft for programming games in C#. The focus of XNA is to eliminate much of the initial setup that other programming languages have when it comes to programming games, such as interfacing with the graphics card and dealing with frame buffers. This way the programmer can jump right in and get results almost instantly, leaving all the back end stuff for XNA’s classes to handle. To that end, I think they did a pretty good job. It literally takes about a minute to import a sprite and display it on the screen, and even usually complex functions such as sprite alpha are greatly simplified. You can also use XNA to write Xbox 360 games, and Microsoft has set up a system where you can submit a game to be peer-reviewed, and if it passes all the checks it can be sold on Xbox Live Arcade. This is a great opportunity for independent developers to market their games to a wide audience without having to drop large amounts of money for marketing or having to find a publisher. It’s also beneficial to Microsoft because they can increase their user base and revenues from games that other people write that they didn’t have to put out any money for.

Disadvantages of XNA
There are some caveats though. XNA uses DirectX, so obviously porting the games to other operating systems and architectures is out of the question. Also, if you write a PC game, anybody who wants to play the game has to install the C# and XNA Redistributable. This can be a hassle for some people, especially if the games don’t come packaged with them. Another issue is that within the past year Microsoft went from XNA 1.0 to the now-current XNA 3.0 beta. I’m not one to normally criticize updates and changes since it usually means additional features and bug fixes, but when a library is updated this often, it’s a hassle for everyone from the developers who have to update their game code to the end user who has to download a new redistributable every change. Lastly, XNA is poll-based, meaning it polls the game a certain number of time per second. This is opposed to event-based, where listener services wait for an event to happen before reacting. This can create issues where event-based programming is desirable over a poll-based one, such as getting keyboard input where someone types faster than the polling rate. There are probably more issues out there that people have with XNA, but these are the ones that I’ve noticed. All in all though, XNA makes it so much easier to program that these problems are minor in comparison to the benefits it brings.

How XNA Works
XNA uses 3 main methods (4 if you count UnloadContent(), but C# has automatic garbage collection so it’s normally not required). LoadContent() will load all the game data before the game is started, Update() updates the game logic on every poll, and Draw() will draw objects to the screen on every poll. By default, the game is set to 60 FPS, and so Update() and Draw() will be called 60 times per second. This makes it very easy to update object properties and draw them onto the screen.

Visual Novel
Since I’m just starting off with XNA and game programming in general, I decided to work on a simple visual novel style game. If you don’t know what a visual novel is, imagine one of those old ‘choose your own adventure’ books where you’re offered a choice at the bottom of every page, such as “If you want to go through the door, turn to page 60. If you want to go down the hallway, turn to page 71.” A visual novel is essentially the same thing except it has graphics, and usually the decision choices you can make are limited to only a few throughout the game. This type of game is very popular in Japan but they’re virtually unheard of here in the United States. The characters are always drawn in the anime style and the storylines are almost always about romance. The reason why it’s a good beginning project for me is because these games usually only have static graphics that fade in and out only, and the bulk of the game is text. This way I don’t have to deal with things such as collision detection and real-time movement until I’m more familiar with XNA programming.

Screenshot of my current progress

Screenshot of my current progress

So far I’ve managed to get the basics up and running. There are different layers for the background, character, textbox background, and textbox, and I can change the sprites they display at will. Text display and word wrapping works, and the text will display letter by letter with an adjustable speed. After the text is displayed, a bouncing triangle will show up to let the user know that they can click the mouse to advance the text, and the mouse click detection is working correctly and even handles the case of the user holding the button down rather than just clicking. The text speed and arrow bounce speed are based on real-time and not the framerate, so it’ll run at the same speed whether the framerate is 60 or 6000. I also wrote a class that can read in the game data (written in plaintext) and parse it into the game, and the game will run the commands in the text file. Here’s an example of the text file:

[back](classroom)3
[char](MakoHappy)2
[text](Mako)"Hello, this is just a test."

The commands follow a command-parameter-value pattern. For background and character commands, the value in the parenthesis is the name of the sprite the game should display, and the number is the time it takes to fade in. For text, the name will be displayed above the main text box to show who is speaking, and the text will scroll in. If there are quotes around the text, they will be displayed as well, which is a good way for users to tell if it’s internal monologue or someone actually speaking.

Right now I’m trying to fix an issue with the bouncing triangle that only occurs when the window for the game is dragged around. For some reason it doesn’t like to obey the conditional statement that tells the triangle to reverse directions once it reaches a certain point. As a result, the arrow will continue to move in whatever direction it was moving in last until I release the mouse button. Then it will slowly move back to its intended position and perform the correct action. I asked about it on the XNA forums but so far no responses yet. The next thing I’ll be working on after I fix this problem is sprite fading.

Well, that about sums it up for this post. I’ll keep updating it as I get more done with the game.

Filed under: Uncategorized, , , ,