Wednesday, March 25, 2009

Skeet Slider game

I didn't even think I was going to be able to get a game done this week. I was spending a bunch of time on HLSL (the XNA shader language) and not getting where I needed to get. I decided I still needed to get a game done this week and I have yet to do a 2D game using XNA so I decided (yesterday) that would be the best way to complete the project in the time left.

The game is called Skeet Slider. Not my best by far, but much better than the past few weeks while I've been learning the XNA framework. You slide a puck across the bottom of the screen trying to avoid bouncing blocks. It's all about timing. I wanted to add a feature to change the velocity of the puck, but I'm out of time. Here is a screen shot of the game on level 4, the highest level. I spent a little time at the end to add better art for the game so it is cleaner that what I've written in XNA so far.



I also messed with the publish system in XNA Game Studio 3.0 and created an installer you can use to run the game. It is available here: http://www.woodsgoods.com/gawxna/01skeetslider.

I have also been considering offering the source code to some of these games to try and cover some of my time. It's a minimal $5 to purchase the C# source code to the Skeet Slider XNA Game. Please tell me if you think this is reasonable or not? I could really use some feedback.

Wednesday, March 18, 2009

SpinMag game

I used the same rotating magnet from last week and added a different scoring mechanic. I also added a levels using png images to place items on the map. There are four levels, but the game is really tough. Currently it only has one life. I wanted to add more, but ran out of time. I again spent a bunch of time reading about texturing and lighting and have not made the progress I had hoped. I'm considering a game that uses spot lights as a mechanic next week.

This is a screen shot of the game.

This is the map image. Each pixel represents the location of one of the 3d spheres.

Here is the code that reads the map image and converts it to 3d objects.

protected void LoadMap(String mapname)
{
// the map is just an image file with pixels of various colors
// red = barrier
// black = open space
// cyan = closed space
// green = starting point
Texture2D map1 = content.Load<Texture2D>(mapname);

mapwid = map1.Width;
maphei = map1.Height;
int numpix = mapwid*maphei;

if ((map == null) (map.Length < numpix))
{
map = new byte[numpix];
}

// want this map to fit over the circle, so calculate the size of each ball
// scale of the ball
mapballsz = (int)(maxballdist*2) / mapwid;
mapxstart = -(mapwid / 2-1) * mapballsz;
mapystart = -(maphei / 2-1) * mapballsz;

/*
* the sample I used to write this method
* ms-help://MS.VSCC.v90/MS.VSIPCC.v90/MS.XNAGS30.1033/XNA/GetData``1_B8CC1053 */

Rectangle sourceRectangle = new Rectangle(0, 0, mapwid, maphei);

Color[] retrievedColor = new Color[numpix];


map1.GetData<Color>(
0,
sourceRectangle,
retrievedColor,
0,
numpix);

goalcount = 0;
int i;
for (i = 0; i < numpix; i++)
{
if (retrievedColor[i] == Color.Cyan)
{
map[i] = MAP_CLOSED;
}
else if (retrievedColor[i] == Color.Red)
{
map[i] = MAP_BAR;
}
else if (retrievedColor[i] == Color.Blue)
{
map[i] = MAP_GOAL;
goalcount++;
}
else
{
map[i] = MAP_OPEN;
}
}
}

Wednesday, March 11, 2009

SpinDart

The games are getting closer, but still have a ways to go on getting my art pipeline down in a week. That's where I spent a bunch of time this week trying to get some models out of 3ds Max which I'm new to. I got the models out, then left them on a different machine so this screen shot is untextured models.

It isn't what I started out to make and I may make a different game with this same mechanic next week. Locking the z axis and making this a 2d game helped the time constraints some, but I would like to be able to get that third axis in a week long game cycle.

The game is a "gravity" style game which is one of my favorites. Impossible to control which means the mechanic doesn't really work, but I'm not 100% sure it is impossible to control. Given a shared high score system I think I would see some different approaches to making points, but alas I haven't written the shared high score system yet...




Here is a code snipet that does the collision checking. I didn't use the XNA system which I looked into a little and decided it was easier to write this simplified code.

private void checkCollision()
{
// check the position of the ball with that of the spinner
Vector3 vspin; // = Vector3.Backward; // pointed up
Matrix m = Matrix.CreateRotationY(spinnerRot);
vspin = Vector3.Transform(Vector3.Forward, m);

// I know the spinner is 7.2 units before scaling (looking in milkshape)
vspin = vspin * 7.42f * scaleXY;

// now check the position of the ball in z space
if ((ballPos.Length()>vspin.Length()+ballradius) && (distToOriginLineXZ(ballPos, vspin) < ballradius))
{
// collision

// wwh score or death?
score = score + 1;
}

// check if we are inside the center of the spinner
// spinner sphere radius = 2
if (ballPos.Length() < 2 * scaleXY + ballradius)
{
// wwh score or death?
mode = STATE_OVER;
if (score > highscore)
score = highscore;
//score = score + 1;
}
}
private float distToOriginLineXZ(Vector3 pt, Vector3 oline)
{
// used this reference
// http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/
// p1 = 0.0,0.0
// p2 = oline.x,oline.z
// p3 = pt.x,pt.z

// oline is a line from the origin
Vector3 vi = Vector3.Zero;
float u = ((pt.X - 0.0f) * (oline.X - 0.0f) + (pt.Z - 0.0f) * (oline.Z - 0.0f)) / (oline.Length()*oline.Length());
vi.X = u * (oline.X);
vi.Z = u * (oline.Z);

pt = pt - vi;
return pt.Length();
}

Wednesday, March 4, 2009

Sphere Pong

This week's mechanic I was working on didn't really work, but it was a fun idea. I created a 3d pong game where you move the puck around a sphere and try and keep the ball in the middle. The problem is there is no reference point. I need some sort of skysphere or spherical markings or maybe also a tail on the ball so you can more easily tell direction. Ran out of time, but may come back and revisit this one someday.

I'm still learning XNA and the graphics look crummy, but for me right now this is more about learning the api than working on making a good looking game. Forward progress at any rate.
For a code sample a lot of the time was spent in positioning the puck around the sphere. I only finished the x,z plane as it didn't work for that, so no reason to add the third dimension. Here is the code to position the puck around the sphere.
// move the paddle using the upVector (true) or the cross product of the origin and up (false)
// direction is a + or - 1 for up, down, left or right
private void movePaddle(bool usingUpVector, float dir)
{

if (!usingUpVector)
{
// need to take the cross between the up and the origin
Vector3 vp = paddlePos;
vp.Normalize();
// vector change
Vector3 vc = Vector3.Cross(vp, paddleUp) * dir;
vc.Normalize();
vc = vc * paddleSpeed;

// find the new location, will be outside the sphere
vp = paddlePos + vc;

// fix up the distance ot the sphere
vp.Normalize();
vp = vp * radius;

paddlePos = vp;

// the up vector should not have changed, right?

// move the camera behind the puck
fixupCamera();
}
else
{
// for this case, take the cross product
// move in the direction of the up vector (+ or -)
// position against the spehere
// use the cross product to calculate the new up vector
}
}