How do I draw lines using XNA?

When working with XNA, everything (even 2d primitives) have to be expressed in a way that a 3d card can understand, which means that a line is just a set of vertices. MSDN has a pretty good walkthrough here: http://msdn.microsoft.com/en-us/library/bb196414.aspx#ID2EEF You’ll find that it takes more code to render a primitive line than it would … Read more

XNA: get screen’s width and height

Empirically I’ve found that in XNA 4.0 I need to use GraphicsDevice.Viewport.Width GraphicsDevice.Viewport.Height when running windowed mode, as I find GraphicsDevice.DisplayMode.Width GraphicsDevice.DisplayMode.Height gives me the resolution of the entire screen. Hopefully this helps someone else out.

When transforming textures (drawn as flat 3D objects) to mimic depth, black lines appear randomly

Look at the rock of the bottom of that last image – it’s got sandy-colored lines going through it. Presumably, you are drawing the sand first, then the rock on top. This tells me it’s not “black lines being drawn” through the textures, but that parts of the textures are not being drawn. Since it … Read more

Comparison between XNA and DirectX (C#)

If you’re actually good at writing unmanaged code, then you’ll probably be able to write a faster graphics engine on top of DirectX. However, for the hobbyist, XNA has plenty of performance, both for 2D and 3D game development. Here is a good Channel 9 video where they run an XNA-built racing game on Xbox … Read more

How do I set the window / screen size in xna?

As of XNA 4.0 this property is now found on the GraphicsDeviceManager. Ie. this code would go in your Game’s constructor. graphics = new GraphicsDeviceManager(this); graphics.IsFullScreen = false; graphics.PreferredBackBufferHeight = 340; graphics.PreferredBackBufferWidth = 480; // if changing GraphicsDeviceManager properties outside // your game constructor also call: // graphics.ApplyChanges();

How to calculate bounce angle?

You might think that because your walls are aligned with the coordinate axes that it makes sense to write special case code (for a vertical wall, negate the x-coordinate of the velocity; for a horizontal wall, negate the y-coordinate of the velocity). However, once you’ve got the game working well with vertical and horizontal walls, … Read more

Convert string to Color in C#

Color red = Color.FromName(“Red”); The MSDN doesn’t say one way or another, so there’s a good chance that it is case-sensitive. (UPDATE: Apparently, it is not.) As far as I can tell, ColorTranslator.FromHtml is also. If Color.FromName cannot find a match, it returns new Color(0,0,0); If ColorTranslator.FromHtml cannot find a match, it throws an exception. … Read more