Crimson Update: Vasculitis (v0.0.2.24380)
cubed2D
May 10th, 2007.
 So heres the layout of my engine at the momentÂ
Without any diagrams, things get messy quickly. I had the thing planned out in my head, i had no paper in my room, my windows were too small to write on (at home i design programs on my windows with dry wipe markers) so i decided to go ahead anyway, and not so surprisingly it got messy. I then decided to use blue J (a java ide we use in university) to draw down what id written so far. Now i know how things connect and i can visualize it better. Id say a visual program designer is a good tool, you can pull things and its cool, but not as cool as using a window!
 Animated sprites have been a fun feature to add, my implementation hangs around the concept of an animation sheet. my orc test sprite has some animations in separate.png files and some bunched together on the same .png. So how do i handle them? every animation gets a animation sheet, which contains its start frame, end frame and a reference to its texture. these are held in a dictionary as a value, the key to that dictionary is a CompassDirection (north, NorthEast ect). I have a load of these in a list, and i keep there indexes in another dictionary with a AnimationState(move, idle, jump etc). To get the correct animation sheet you just need to know what direction and animation you currently in and pull em out. More difficult problems were finding out the current direction and grabbing the current frame from a texture. At the moment i only support the xbox controller, so i needed a way to turn the vector2 returned from the thumb stick in to a cardinal direction. I did this using the magic of dot product. The dot product between two vectors will give you the angle between them. All i needed to do was to have a list of all the directions i wanted to test, try the dot product between them all and then use the animation with the smallest dot product (which would be the closet to the angle of the stick) Heres some code for you!
public class DirectionSet // the 8 compass directions represented in vectors
 {
 public Dictionary
 public DirectionSet()
 {
 directions.Add(0, new Vector2(0f, 1f));
 directions.Add(1, new Vector2(0.5f, 0.5f));
 directions.Add(2, new Vector2(1f, 0f));
 directions.Add(3, new Vector2(0.5f, -0.5f));
 directions.Add(4, new Vector2(0f, -1f));
 directions.Add(5, new Vector2(-0.5f, -1f));
 directions.Add(6, new Vector2(-1f, 0f));
 directions.Add(7, new Vector2(-1f, 1f));
 }
 }
Use this function to get the sticks direction:
public int GrabAnimationDirection(Vector2 stick)
 {
 float bestDot = -100;
 int bestAnim = 0;
 for (int i = 0; i < 8; i++)
 {
 Vector2 normal = Vector2.Normalize(directionSet.directions[i]);//normalise the directions
 float dot = Vector2.Dot(velocity, normal); //find the angle
 if (dot >= bestDot) //if its smaller, save it
 {
 bestDot = dot;
 bestAnim = i;
 }
 }
 return besttAnim;
 }
Then i just cast the returned int to my CompassDirection enum for use!
The other problem is alot easier, how do we choose the tile? First, we need a frame counter to increment. I increment it every 33ms (30fps). we also need to know the number of columns in the sprite sheet and the height and width of each sprite. with this info we can find the top left corner of each frame
int x = (frame % column) * width;
int y = (frame / column) * height;
Its really simple when you think about it, just grab a sprite sheet, and try these for a frame in the middle, I’m sure your understand how it works straight away!
The other useful thing i learned is how to cast a string in to a enum. Which is really useful when reading info out of an xml file.
(CompassDirection)Enum.Parse(typeof(CompassDirection), “North”);
remember to test its going to work first!
Enum.IsDefined(typeof(CompassDirection),”apples”);
would return false, because apples just arn’t directions no matter how hard they try.
anyways, Crimson 0.0.2.24380 new features!
Features:
- Better, more flexible animation system, which supports:
- Idle animations
- Movement
- Shooting
- Death
- and flexible to add others!
- Loading XML files which describe an animation, and serializing them in to actual animation sprites.
- early prototype of the input manager.
- updates to my content manager
also, for anyone who is interested, heres a short demo. It only works with an xbox controller at the moment. left stick moves you around, B jumps and A kills you. If anyone can run it and tell me how it runs on there pc id be grateful!
