The game has actually been finished and published on Xbox Indie Games (Under the name Argh Aliens link -> http://marketplace.xbox.com/
Monday, 5 March 2012
It's so easy to fall behind
The game has actually been finished and published on Xbox Indie Games (Under the name Argh Aliens link -> http://marketplace.xbox.com/
Tuesday, 7 February 2012
Refactor
I've been through the code base and had a bit of a tidy up.
There is now a static class which holds all of the controls for the player, this is really useful as it maps an xbox pad button reference to a key so I don't have to keep remembering which key I assumed would be the start button!
I've also removed all game variables (like start lives, score multipliers. Etc) from the classes that use them as there was too much duplication and find and replace. Now there is a singleton options class that can be used to get option settings. Why isn't it a static class? Because I want to be able to set the values in the options if need be and static would fix them.
Another change is the camera and lights are now part of a scene object that is passed into the level.
Player objects are also now created at game state and passed into the level. This means I don't have to track player variables outside the level for when the next level is created. It makes sense as the state of a player exists across multiple levels.
It needs some nicer graphics too. I'll try my best!
Oh and an xbox trial mode!
Adding some incentives
So now you can collect fuel power ups to top your fuel up and also if you score over a certain number then you get an extra life!
Friday, 20 January 2012
Creating state and screens
So far the game has loaded, jumped straight into the action and on completing a level just jumped straight back to playing.
I need to have some sort of naturally flowing state to the game such as...
Start page
Playing game
Level complete page
Game over page
And probably a Game paused page
This will allow the user to naturally flow through the main states of the game and provide input into what happens. Such as start or quit the game, pause play, see how well they did on a level or review how well they did at game over.
I've already got my enum to let me know what state my playing game is so I've added more states to this to represent these extra sections.
All I need to do is have some screens displayed to the user with a way of choosing options.
I've created a base Game screen class that draws a background image on screen and You can also provide it an array of menu choices which get rendered to screen. The default class also allows navigation and selecting menu items.
Now for each game state that requires a screen, I just extend this base class and add any custom functions such as displaying the end of level score or letting the user start a game.
There are much better state systems for XNA but this one is very easy to implement and serves my needs just fine.
Now my game has a nice flow of states.
Splash: check for user input to determine controlling game pad.
Main menu: can start the game, view controls or quit game.
Playing: game play mode
Paused: game froze, can also quit to main menu
Level complete: show score break down
Game over: show final score
Quit confirm: any time the user wants to quit just get them to confirm it.
Monday, 16 January 2012
Creating the HUD
Sunday, 15 January 2012
Adding a skybox
Sunday, 8 January 2012
A bit more of a challenge
Adding the peeps
I've found the original
Saturday, 7 January 2012
Making gravity work against you
Thursday, 5 January 2012
Making use of particles for explosions
So far when I collide with a building. Nothing happens other than the player UFO bounces off.
I've added some quick code to make the player disapper and reset position.
But it would be nice to have a cool explosion. Well these new particles will work nicely.
Firstly I've created a wrapper class to contain the particle effects and then I can just pass this to my player object and upon collision just add the explosion particle to the game world.
Very easy to implement and now when I crash there is a satisfying explosion.
I decided to add another particle effect too for when you thrust the player UFO it now has an emission trail shoot out of the bottom.
All I did was copy the fire particle system and tweak it with a new graphic to get my desired effect.
This is kind of a distraction from getting my core game done but it was a fun experience and has added a nice polish to the game.
Wednesday, 4 January 2012
Particles are fun
I want to see my bounding boxes
Tuesday, 3 January 2012
Giving the player a bit of character
So far the player UFO just slides around the map. It would be nice to have some sort of animation. Something like the UFO spinning around.
I think you can pre animate models and then play them in XNA, not sure though I've not really researched it.
My needs are simple, I Just want to rotate the 3D object through the Y axis and my base3Dobject already has a rotation property. All I need to do is add to this value every game iteration and my 3D object will rotate as the world matrix of the object takes scale, rotation and position into consideration.
Now the player UFO spins around when I run the game.
But now I'm getting some strange collision detection issues. I can cut right into the buildings and sometimes when I land I drop through the building.
What's going on? I've only changed the rotation!
Don't let the player escape
Quick test on the xbox
Cleaning up collision logic
Originally I Just used a couple of booleans to let me know if and where the player had collided.
Not very informative though, so I've added a collision enun that now tells me the state of collision
* none
* building
* roof
I can then use these accordingly to achieve other game mechanics later on.
Shadows and learning to read
So my first attempt at implementing the shadow mapping example was a mess.
I figured I would add all of the shadow creation code straight to the base3Dobject as that held all the lighting information.
Then I would call the draw shadow method on the player object only and I should get a shadow.
Well, mostly right but the shadow mapping uses something called a RenderTarget2D.
Not really sure exactly what it is but I think it is what is used to draw the shadows.
You set the render target as the item you want to cast shadows then pass this render target to other objects so they know where to draw the shadows.
What I had originally done was to define this render target object in everyone of my objects so they were all casting shadows only on themselves.
All I needed to do set this render target as a scene variable and pass it to my objects and now I have my shadow working the way I want.
The lighting is a bit basic at the moment as everything is lit very uniformly. This is because there is only one light source processed. For now I'm happy enough and I'll look into multi pass lighting later on.
I've added some textures to my objects in 3D max just to make it look a bit nicer.
Things are starting to shape up, I have the basics of a game.
Monday, 2 January 2012
My fun with shadow mapping
The first thing I noticed when trying to adapt the shadow mapping example from Msdn was it extends the model render content pipeline again.
No big deal as I've already done this to get my bounding boxes so I've just ripped out the new custom effect rendering code and added it to my existing custom content pipeline.
This example uses a custom effect and not the standard basic effect. From what I can tell, this custom effect is responsible for figuring out which pixel should be in shadow from the shadow map.
Once I had added the code to my custom content processor extension I started getting some strange long error messages about the effect is null.
It turns out that if you have a custom content processor that uses a custom effect, you have to tell visual studio that the model using the custom content processor also knows the name of the custom effect file. You just do this in the properties explorer.
So my initial implementation of the shadow map didn't work as planned. Partly because I had no idea what I was doing and blindly copy and pasting and hoping for the best.
Casting a shadow
From doing some research, it would seem that shadows in games and game engines is not an easy thing.
Getting a good looking shadows or even shadows at all takes some doing.
The Riemers XNA tutorials have a good section on dynamic lighting and using HLSL to write realistically lit scenes. The output is quite impressive but the effort to implement is substantial and not required for what I want.
I Just want a shadow cast from directly under the player UFO, nothing more than that.
So more research shows that shadow volumes are an easy way to build basic shadows.
There are XNA tutorials on shadow volumes and implementing them with the XNA stencil buffer. Problem is, every example I found was for XNA 3.1 or before and none of them seemed to work in XNA 4.0.
Rather than mess around with trying to get it to work I found another way using shadow maps.
Msdn has a nice example of how to implement a shadow map (link) and it gives me essentially what I'm after (and it works in XNA 4.0)
Setting the world alight
My use of 3D packages in the past like 3Dmax and povray help me realise that shadows are cast from scene light sources that can cast shadows.
These light sources are part of the world and cast shadows by varying techniques like ray tracing. This works fine in pre rendered scenes as the shadows don't dynamically move depending on player input.
Also, XNA doesn't have world lighting, you apply how an object is lit and you can have this info as a global variable so all objects are lit the same.
Problem is that no object knows about any other objects lighting and therefore can't block any light rays.
This means that shadows aren't a given in XNA. You can't just apply a light source and say cause this to cast shadows.
So it's not as easy as I thought. But I still think shadows are the best way to achieve my game mechanic so time to google!
Making my landing zone
So I need a way of checking the type of collision that has occurred.
For now I'll just have a boolean that states if the collision is safe.
So I need to determine what is a safe and unsafe collision.
If it hits the side its unsafe, if it hits the top then It's safe.
I could do all sorts of things like calculate the intersecting path to determine the plane intersected.
But my needs are simple.
If the intersection occurs and the max.Y of the player UFO bounding box is less than the max.Y of the building block bounding box then it can't have hit the top. So its unsafe.
Otherwise it must have collided with the top, therefore assume it's safe? Not quite.
The player UFO can just barely touch the top of the building block to cause an intersection and it will be considered safe. Which looks silly as it looks like the player UFO is balancing on the edge of a building. Really it should crash.
Again this isn't a hard check. If the player UFO has has collided with the top of the building and the player UFO bounding box x and z min and max limits is contained within the x and z min and max limits of the block bounding box, then it can be considered the player UFO has landed safely and is not over hanging the building at all.
So now I have the means to know if the building collision is safe or not.
I need to make this more challenging later based on the speed of the player descent. If you come down too fast then you crash land and die (just like in lunar lander)
But for now I have the basics of a game. I have my map and a controllable player that can collide with the landscape.
It works pretty well, the one annoying thing is you never really know exactly where the player UFO is hovering over so before you start your descent you can't be sure if you will crash.
This was a problem in the 2D version of the game too, and I solved it with a player shadow showing which building you were over.
So shadows are still in my opinion the easiest game mechanic to aid the player in knowing their position and helping them to a safe landing.
Better collision detection
So now I have bounding boxes to use, it is obvious from running the game that the clipping issue is much better.
Now the player UFO actually collides when you would expect it to.
Although it still rapidly bounces off walls, which isn't too bad as you would blow up anyway.
The biggest issue is when the player UFO lands on the top of a building, it flickers like crazy because of this bouncing. It doesn't look as if has landed. It's looks like it is having a fit!
This is because I'm reversing the velocity upon collision. Which is fine for the x and z axis but doesn't really help on the Y axis.
Also the world matrix for the objects is calculated in the update based on the current object position. Then the collision is checked, then the object is drawn.
When the collision is checked it resets the position but not the world, So when it comes to draw it, it actually draws the intersecting frame.
Easily fixed by storing the safe world matrix and using that when drawing the object.
This is common practice, check for collision and reset before you even draw it's position, then you never see the intersection.
So now the collision works well and I think is acceptable.
My only issue is what to do on a collision. If the player UFO touches the side of a building, then it's bad. However if it touches the top then it shouldn't die as it should be landed.
Extending the content pipeline
So XNA provides a very nice content pipeline.
Basically the way it takes content or media and makes it available in the programming environment and game.
So the model processor is the content processor that lets you work with the 3D objects. It's what creates the bounding spheres and provides all the other methods.
So you can extend any of these processors and use your own custom one on any content imported.
You don't have to do this at all. I could work out bounding box coordinates from my model mesh at run time, but it would need to be constantly updated and therefore processor intensive.
Extending the content processor means I can pre process the models and provide the data I need at point of initialization so the processor only has to do this once at game load (or at least model load)
So with a custom model processor that generates my bounding box data for me, means an more efficient solution.
Now all I need to do is set the content processor of the model fbx files (in the property explorer in visual studio) to the new custom model processor.
Now I have a bounding box for my 3D objects.
Collision detection the easy way
So the easiest way to do collision detection in XNA is to have a bounding box around the 2D Sprite, basically a rectangle.
Then if two sprites collide, then their bounding boxes intersect and you can check for this with the standard Intersect method in XNA.
Great, so a 2D concept is easy to move to a 3D world, just add the Z axis.
It would seem XNA 4.0 doesn't support 3D bounding boxes any more and just has bounding spheres.
Same concept but rather than having a cube to check for intersection you have a sphere that surrounds the model (or sub elements of the model).
So far so good, all I need to do is add a collision routine for checking if the player sphere has intersected with the building block spheres.
If a collision occurs then I just set the player position back to the position it was before the player moved (I also reverse the velocity of the object)
So running the game and yes collision works, somewhat.
The UFO seems to heavily clip the building blocks and it will disappear inside a building until the collision occurs.
Then when it does occur, it seems the player UFO bounces rapidly against the colliding object.
Firstly, why do I get this clipping effect? Well it's because my objects are not best suited to bounding spheres as they are more cube like in shape so the spheres will not surround the model in the desired way.
Bounding boxes would be much better. But XNA 4.0 doesn't have them any more.
Looking on google, it seems like someone has added them back by extending the content pipeline.(link)
Basically they have built a custom content processor for 3D models that adds bounding boxes for all objects in the model mesh.
That's what I need.
Adding a player
The player is Just my UFO object that again extends from my base3Dobject. I've actually gone a little further here and extended it to a moveable3Dobject first as I may have other moveable objects that aren't player controlled.
The new moveable object has two key attributes:
* velocity - the direction the object is travelling in
* acceleration - the multiplier of how quickly to move in the direction of the velocity.
The update code of this object now updates the position of the object based on the set velocity multiplied by the acceleration. This will simply move the object around when I apply a velocity to it and as long as it is accelerating.
so now to add my player, I just call the same draw code and the UFO appears, doesn't do anything but it's on screen.
So in my player object that extends the moveable object, I need a method to take player input and change the velocity accordingly.
So far I'm just using the keyboard but it's easy to plug in the xbox controller code later on (with a nice input class I wrote for my 2D engine)
So if I press left or right arrow I update the velocity.x value to move the sprite across the x axis (width of the map).
If I press up or down arrow then I update the velocity.z to move through the Z axis (depth of the map).
So what about the Y axis? That's my altitude axis. I want the player to drop to the ground to simulate gravity unless they are using the thrust.
This is easily done by adding to velocity.Y if no thrust is applied so the UFO will fall.
If the player presses space then thrust is applied and I decrease velocity.y in increments of thrust values to give the impression that the UFO is slowly thrusting up against gravity.
So now when I run the game I can move the UFO aroud the world and it falls down unless I thrust.
So far so good, except when the UFO model gets to a a building object, it just passes straight through it. This is because the 3D objects are just pixels rendered to screen with a z buffer to hide objects blocked by other objects in the viewpoint.
They objects have no mass or physical being in the world.
This is exactly the same in a 2D game. Sprites will happily draw over each other. The solution is collision detection.
Same goes in a 3D world, I need to check if the objects in the game world are colliding and then act accordingly.
Building my map
So I've cleaned up my 3D object loading and drawing code and created my own custom class called base3Dobject.
It holds the basic attributes requied, handles loading the content, alignment to world and drawing of the object.
I've also created a basic camera object to hold my world camera settings as this will be needed by all of my 3D objects.
Now I want to draw my building grid for the game's landscape.
The map is a simple grid of varying height buildings that the player will need to traverse and land on.
All I need to do is create a 2D array to hold the width and depth of the map (number of buildings wide by number of buildings deep) and for each building location (I'll call this the foundation position) I need to set the number of blocks high the building will be.
So now I have a 2D array of heights for each of my buildings.
I could just iterate this array every time I want to update or draw a building.
But that will mean I will need to dynamically generate objects on every iteration and have nested loops.
Now I don't want to be creating temp objects at run time because I know how the xbox garbage collector will behave (now!) So I need a pre defined array of blocks that is allocated in memory so it never needs to bother the garbage collector at run time.
Each block is a positioned 3D object of my cube model. Now all I need to do is loop through this array and update and draw each block in the set position.
I need to make sure the position of each block is offset accordingly so all the blocks don't draw on top of each other. This is easy by using the current width and depth of the foundation multiplied by the offset (the model width and depth) and then the same done to the randomly generated height.
So now when I run my game, I get a nice simple grid of buildings.
Models in XNA
Loading content into XNA is simple and 3D content is no different. Just create a Model object and use the content.load method to load the fbx file.
I have some basic knowledge of what a 3D object will need to be of any use.
It needs a position (where it is in the game world). 2D sprites also need a position (an x y coordinate) and in the past I've used a vector2 object to hold this data.
A 3D world has an x and y coordinate as objects can move left, right, up and down. But they also move further and closer to the view or across the Z axis (depending on your point of view of course)
Well I only need to hold an extra position value for the Z position then.
Vector3 will do this :)
I also need some other basic info, scale and rotation.
So I can set these on my model object and at least I can position my object in the game world.
I'll tidy this experiment up later by creating my own custom objects to hold my 3D game objects.
Now I need to draw the object to screen.
2D games use sprites and XNA provides spritebatch to make this A one line call.
3D objects are more complex so I don't have this simple one liner.
Msdn has a very simple draw example of how to render the model.
It basically goes through each of the meshes in the object and sets the relevant parameters on the object and any effects required (lighting and the camera view and projection onto the object)
XNA has some strangeness when using spritebatch as wellso I had to set some parameters on the graphicsdevice before and after rendering 3D objects.
To make sure the 3D object is displayed at the right position, in the right scale and rotated correctly to the 3D world, I need to take all the points and calculate the world variables. This is done with some matrix multiplication and now I have a world aligned object.
Next thing is to position the camera and its viewpoint so it can see the object when the scene is rendered.
And I need a light source to illuminate the object.
Once this basic stuff is added, I can run the code and my object is on screen with basic light shading! That Was easy!
Sunday, 1 January 2012
First things first
So where do I start with my introduction into 3D?
Ok so what do I need to achieve?
* draw some buildings of varying heights in a grid
* have a player object that I can move around in 3D space and have it fall to the ground unless I'm thrusting
* view the whole scene through a controllable camera
Fairly straightforward so far.
But I need to get the absolute basics, how do I get a 3D model? How do I load it in to XNA and how do I get a camera viewpoint of the scene?
Well XNA takes all sorts of 3D model formats but I've decided on .fbx (no real reason for this, it just works)
There are plenty of resources on the web where I could download 3D objects from, but I want to make My own. So what are my choices? Well, anything that can export to fbx!
Blender can do this and is free, many people swear by it. I tried it but it's not the easiest thing to use.
Luckily, I've got 3D studio max and have used it a fair bit so it was easier for me to use.
I started by creating a basic cube that would be the building block for my towers. No textures or anything clever just yet, I just want some assets to play with.
Then I made a player UFO, just a torus and a sphere again with no textures.
So now I have some assets to play with I just need to get them into XNA and render them to screen.
This is not a tutorial
Just a quick note to say this won't be a tutorial into how to write a game in XNA. There are many great XNA tutorials on the web written by much better programmers than me, I don't want people to learn from my examples :p
I'd also highly recommend the app hub forums if you do need help with XNA.
My first attempt at 3D games in XNA 4.0
I've tinkered with XNA before but only to make 2D games.
My biggest being my 2D tile based game engine that never worked on the xbox due to my bad understanding of garbage collection on the xbox.
Anyway, I've decided to have another go at finally getting an indie game published on xbox live.
I first need a game idea to build and learn the trade as I go.
I wrote a flash game many years ago called argh aliens and it was basically a pseudo isometric lander game where you had to thrust accordingly to capture humans. It was a simple but fun premise so I thought why not remake it in XNA.
I started down the same path as before, trying to do it all in 2D as I had done in flash.
As I had never really done any 3D development, I thought it better to stick to what I know. But then I started hitting all the same problems I had with the flash game:
* Occlusion of the player sprite when it goes behind a building (solved originally using marks)
* Casting shadows on the buildings from the player to show your relative position (solved by working out the location of where a semi transparent sprite should be, but this looked less than pleasing)
* Losing sight of player when going behind a building (solved by not having smaller buildings in the distance #hack)
All of these issues are easy to solve with a 3D engine as they are naturally solved by:
* Occulsion will be handled by the z buffer and I never have to worry about it. Models in the foreground will naturally hide objects behind them.
* Light sources can be added and cause shadows to be cast on other objects in the scene (note: ok so shadows aren't that simple!)
* In a 3D world, I have control of the camera position and view point, so I can move the camera to always keep the player in view
So it seems obvious to me to step out of my comfort zone and try this game in 3D.
I've decided to blog about my experience just in case my learnings and mistakes help anyone else along the way.
The code is all publicly available on github (I will post the link).
I'm actually a fair way through the development but I will historically blog my journey up till the point I am now.
Followers
Blog Archive
-
▼
2012
(31)
-
►
January
(28)
- Creating state and screens
- Creating the HUD
- Adding a skybox
- A bit more of a challenge
- Adding the peeps
- I've found the original
- Making gravity work against you
- Making use of particles for explosions
- Particles are fun
- I want to see my bounding boxes
- Giving the player a bit of character
- Don't let the player escape
- Quick test on the xbox
- Cleaning up collision logic
- Shadows and learning to read
- My fun with shadow mapping
- Casting a shadow
- Setting the world alight
- Making my landing zone
- Better collision detection
- Extending the content pipeline
- Collision detection the easy way
- Adding a player
- Building my map
- Models in XNA
- First things first
- This is not a tutorial
- My first attempt at 3D games in XNA 4.0
-
►
January
(28)