I haven’t had time to write anything as of late. Really busy with work and I’m spending most of my spare time either writing code or playing video games. I’d like to point people in the direction of
http://www.xblcg.info/
. I think this site is what community games has needed for the longest and glad to see the guys doing the good work.
Scientific Ninja has a post about how the term MMORPG is a little overused by hobbyist developers. It’s a good read.
Calculating an angle from a Vector2
Published February 28, 2009 Code Snippets Leave a CommentTags: Vectors, Xna
When you need to calculate an angle from a Vector2 structure, you can use this piece of code:
public static class Vector2Helper
{
public static float CalculateAngle(Vector2 v)
{
float angle = 0.0f;
if(v != Vector2.Zero)
{
v.Normalize();
angle = (float)Math.Acos(v.Y);
if(v.X < 0.0f)
angle = -angle;
}
return angle;
}
}
I used this to calculate an angle from the Vector2 of the Left Stick.
The original credit for this source code comes from here.
I haven’t had much time lately with work but one question I’ve seen asked many many times is how to draw lines of different widths. So, to cut to the chase I’ll share the code I’ve used to do it.
public void DrawLine(Vector3 p1, Color c1, Vector3 p2, Color c2, int width)
{
float distance = Vector3.Distance(p1, p2);
float halfDistance = distance / 2.0f;
float halfWidth = width / 2.0f;
Vector3 difference = p2 – p1;
Vector3 destination = new Vector3(p1.X + difference.X / 2.0f, p1.Y + difference.Y / 2.0f, p1.Z + difference.Z);
// Calculate angle between two points
float angle = (float)Math.Atan2(difference.Y, difference.X);
Vector3 v1, v2, v3, v4;
v1 = new Vector3(-halfDistance, -halfWidth, 0); // Top Left
v2 = new Vector3(halfDistance, -halfWidth, 0); // Top Right
v3 = new Vector3(halfDistance, halfWidth, 0); // Bottom Right
v4 = new Vector3(-halfDistance, halfWidth, 0); // Bottom Left
Matrix m =
Matrix.Identity *
Matrix.CreateRotationZ(angle) *
Matrix.CreateTranslation(destination);
v1 = Vector3.Transform(v1, m);
v2 = Vector3.Transform(v2, m);
v3 = Vector3.Transform(v3, m);
v4 = Vector3.Transform(v4, m);
DrawQuad(v1, c1, v2, c2, v3, c2, v4, c1);
}
I’ve left a lot of fluff code out. I usually check if the line is a width of 1 and draw a normal line. I also left out the code on how to draw a quad as that can be found many other places already.
I was listening to “.NET Rocks” show #414 this morning and there was an interview with Chris Marinos. While in college he wrote an Xna game that uses the WiiMote as an input device. He’s recently ported the code to F#. It sounded really interesting so go check it out.
One thing that was said in the interview that I didn’t really agree with though, Xna was said to be able to “run Xbox 360 games on your PC”. This isn’t right and it portrays Xna in the wrong way to me. Xna is Microsoft’s managed DirectX solution. Although it is right now geared more toward the game community, it can still be used for other purposes.
I just received my copy of C# In Depth by Jon Skeet. Jon has a great deal of knowledge on C# and if you’ve never ran into his blog I suggest you check it out here. I’ll post later more about the book but I’m pretty sure it’s a good one.
I’ve been talking with a guy on the creator forums lately about SpriteSheets and so I decided it might be a good idea to post my SpriteSheet class.
It’s very simple. Only reads sprites from left to right and assumes all Sprites are the same width and height.
#region Using
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion
namespace Snow.Xna.Graphics
{
///
///
public class SpriteSheet
{
#region Fields
string name;
Texture2D texture;
Rectangle[] rectangles;
int spriteWidth, spriteHeight;
#endregion
#region Properties
///
///
public string Name
{
get { return name; }
}
///
///
public Texture2D Texture
{
get { return texture; }
}
///
///
///
index
///
public Rectangle this[int i]
{
get { return rectangles[i]; }
}
///
///
public int Count
{
get { return rectangles.Length; }
}
///
///
public int Width
{
get { return texture.Width; }
}
///
///
public int SpriteWidth
{
get { return spriteWidth; }
}
///
///
public int Height
{
get { return texture.Height; }
}
///
///
public int SpriteHeight
{
get { return spriteHeight; }
}
#endregion
///
///
///
///
Width of each sprite.
///
Height of each sprite.
public SpriteSheet(string name, Texture2D texture, int spriteWidth, int spriteHeight)
: this(name, texture, spriteWidth, spriteHeight, 0)
{
}
///
///
///
///
Width of each sprite.
///
Height of each sprite.
///
The number of sprites in the sheet.
public SpriteSheet(string name, Texture2D texture, int spriteWidth, int spriteHeight, int count)
{
this.name = name;
this.texture = texture;
this.spriteWidth = spriteWidth;
this.spriteHeight = spriteHeight;
if(count == 0)
{
int numX = texture.Width / spriteWidth;
int numY = texture.Height / spriteHeight;
rectangles = new Rectangle[numX * numY];
}
else
{
rectangles = new Rectangle[count];
}
int x = 0, y = 0;
for(int i = 0; i < rectangles.Length; i++)
{
rectangles[i] = new Rectangle(x, y, spriteWidth, spriteHeight);
x += spriteWidth;
if(x >= texture.Width)
{
x = 0;
y += spriteHeight;
}
}
}
public static implicit operator Texture2D(SpriteSheet spriteSheet)
{
return spriteSheet.Texture;
}
}
}
You can create a new SpriteSheet and use it like this:
SpriteSheet spriteSheet = new SpriteSheet(“tiles”, Content.Load
spriteBatch.Begin();
spriteBatch.Draw(spriteSheet,
new Rectangle(0, 0, spriteSheet.SpriteWidth, spriteSheet.SpriteHeight),
spriteSheet[0],
Color.White);
spriteBatch.End();
Which loads a spritesheet with sprites of size 64×64. It then draws the first Sprite in the SpriteSheet. You of course wouldn’t want to load the spritesheet every frame as well.
Feel free to use this code without restriction.
Edit: I copied and pasted the second piece of code from somewhere else so I fixed two typos.
