Pages

Monday, August 15, 2011

3d asteroid game in with sources

A basic 3D asteroid game in openGL with C#
This article is intended for beginners that want to start in 3d game programming and don’t know where to start. It is programmed in Opengl using VS 2008 and a small graphic engine I made myself called Shadowengine. It has the basics of what a game should have: a Score, levels of difficulty and a life counter. All this is written a small amount of code lines, trying to be simple and understandable.

The first problem I had to achieve is the scene to look like outer space, for that issue I set the background color to black. In opengl is set this way
Gl.glClearColor(0, 0, 0, 1);//red green blue alpha
The other problem were the stars and I solve it by drawing random white points on the screen, the algorithm is more or less this way I generate a random point and measure the distance to the spaceship, and is if is less than a predefined number I discard it and repeat the process until are creates the desired stars look at the code:
public void CreateStars(int cantidad)
        {
            Random r = new Random();
            int count = 0;
            while (count != cantidad)
            {
                Position p = default(Position);
                p.x = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
                p.z = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
                p.y = (r.Next(110)) * (float)Math.Pow(-1, r.Next());
                if (Math.Pow(Math.Pow(p.x, 2) + Math.Pow(p.y, 2) +
    Math.Pow(p.z, 2), 1 / 3f) > 15)
                {
                    stars.Add(p);
                    count++;
                }
            }
        }
The score is a number which increases over time and it grows faster everytime I pass a level. The level increases every 450 frames.
The last issue I had to adress is the problem of asteroid collision. I make for the ship three positions(one for th ship and two for the wings) and everytime in a while I check the distance between all the asteroid and those trhee positions. If is under a predefinde value I execute the collission event.
The project has 6 classes
AsteroidGenerator.cs It handles the creation of asteroids in random places and with random sizes and speeds. It also have the method to query wether or not  It have been a collision between the spaceship and an asteroid
Asteroid.cs It handles all concerning an asteroid such as the movement, texture selection, drawing, among others
Star.cs It has a method to create random white points and to draw them. 
Camera.cs It handles the selection of the user camera and to set the right perspective view of the scene
SpaceShip.cs This class contains all methods and attributes regarding the spaceship, such as movement, drawing, etc.
Controller.cs This class manages the game creation, game logic and contains all the classes needed to run the game. Here is a portion of code
using System;
using System.Collections.Generic;
using System.Text;

namespace Naves
{
    public class Controller
    {
        Camera camara = new Camera();
        Star star = new Star();
        SpaceShip spaceShip = new SpaceShip();
        public SpaceShip Nave
        {
            get { return spaceShip; }set { spaceShip = value; }
        }
        public Camera Camara
        {
            get { return camara; }
        }
        public void BeginGame()
        {
            AsteroidGenerator.GenerateAsteroid(35, false);
        }
        public void ResetGame()
        {
            AsteroidGenerator.GenerateAsteroid(35, true);
            spaceShip.Reiniciar(); 
        }
        public void CreateObjects()
        {
            star.CreateStars(450);
            spaceShip.Create(); 
            Asteroid.Crear(); 
        }
        public void DrawScene()
        {
            star.Draw();
            AsteroidGenerator.DrawAsteroids();
            spaceShip.Dibujar(); 
        }
    }
}
  
Main.cs This is the form wich contains this visual components of the game. It contains the controller class and give the player information throught proper windows controls. It has a timer to draw perodically the scene and has the code for texture and object loading.
If you want to add sound to the project uncomment the commented lines and press ctrl+alt+E and under managed debuggin asistants uncheck the loaderlock exceptionI am hoping to receive feedback from this example. , Anonymous comments are enabled.
Here is the download link
http://filebeam.com/4219ffee44dcabb93daa30c61109b91a

4 comments:

  1. i have read almost all your blog to understand how your shadowengine works, i don't speak english too much so it was very difficult. whatever, thanks for the code

    ReplyDelete
  2. Ok feel free to ask, whaterver you like and I will answer it to you.

    ReplyDelete
  3. Any chance you have this in C++ ?

    ReplyDelete
  4. hello
    I dont have this code in c++
    I should have to code it
    do you really need it?

    ReplyDelete