/// <summary> /// The default constructor for an explosion. /// </summary> /// <param name="physics">A reference to the physics engine.</param> /// <param name="explosionModel">The model to use for a particle in the explosion.</param> /// <param name="explosionTransforms">The graphical transforms to apply to the model.</param> /// <param name="explosionPosition">The position of the explosion.</param> public Explosion(PhysicsEngine.Environment physics, Model explosionModel, Matrix[] explosionTransforms, Vector3 explosionPosition) { this.objectModel = explosionModel; this.transforms = explosionTransforms; // Set up the particle parameters based on game constants ParticleSystemParameters psp = new ParticleSystemParameters(); psp.BirthTime = GameConstants.particleBirthTime; psp.BirthTimeVariance = 0; psp.Colour = GameConstants.explosionColour; psp.ColourVariance = GameConstants.colourVariance; psp.Direction = GameConstants.particleBaseDirection; psp.DirectionVariance = GameConstants.particleDirectionVariance; psp.InitialParticles = GameConstants.initialNumParticles; psp.LifeTime = GameConstants.particleLifeTime; psp.LifeTimeVariance = GameConstants.particleLifeTimeVariance; psp.Mass = 1; psp.MassVariance = 0.1f; psp.MaxParticles = GameConstants.explosionNumParticles; psp.Position = explosionPosition; psp.Speed = GameConstants.explosionParticleSpeed; psp.SpeedVariance = 0; // Create the particle system and add it to the physics engine. this.physicsReference = new ParticleSystem(psp); physics.Add(physicsReference); }
/// <summary> /// The default constructor for a list of explosions. /// </summary> /// <param name="maxExplosions">The maximum number of explosions that can be displayed at any one time.</param> /// <param name="environment">A reference to the physics engine.</param> /// <param name="model">The model to be used to draw a particle.</param> /// <param name="transforms">The graphical transforms to be applied to the particle.</param> public ExplosionList(int maxExplosions, PhysicsEngine.Environment environment, Model model, Matrix[] transforms) { this.explosions = new Explosion[maxExplosions]; this.physics = environment; this.explosionModel = model; this.explosionTransforms = transforms; }
/*************************************************************************************************************************/ /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // Update the screen size GameConstants.windowWidth = graphics.GraphicsDevice.Viewport.Width; GameConstants.windowHeight = graphics.GraphicsDevice.Viewport.Height; // Audio Initialisation audioEngine = new AudioEngine("Content\\Audio\\MyGameAudio.xgs"); waveBank = new WaveBank(audioEngine, "Content\\Audio\\Wave Bank.xwb"); soundBank = new SoundBank(audioEngine, "Content\\Audio\\Sound Bank.xsb"); // Camera Initialisation camera = new Camera(); camera.ViewMatrix = Matrix.CreateLookAt(camera.CameraPosition, camera.CameraFocusOn, Vector3.Up); camera.ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(GameConstants.perspective), aspectRatio, 1.0f, GameConstants.cameraMaxDistance); // Physics Initialisation PhysicsParameters parameters = new PhysicsParameters(); parameters.Gravity = new Vector3(0.0f, 0.0f, GameConstants.gravity); parameters.WindSpeed = new Vector3(0.0f, 0.0f, 0.0f); physics = new PhysicsEngine.Environment(); physics.PhysicsParameters = parameters; base.Initialize(); }
public GameObject(float radius, ConvexSegment hull, PhysicsEngine.Environment environment, Model model, Matrix[] transforms) { ConvexHull[] hulls = new ConvexHull[] { new ConvexHull(hull, Matrix.Identity) }; physicsReference = new GameEntity(radius, new Hull(hulls), environment); environment.Add(physicsReference); this.objectModel = model; this.transforms = transforms; }
/// <summary> /// The default constructor for a bullet. /// </summary> /// <param name="radius">A radius that encloses the entire bullet model, centred at (0,0,0) in object space.</param> /// <param name="hulls">The convex hull segments that enclose the bullet.</param> /// <param name="environment">A reference to the physics engine.</param> /// <param name="model">The model used for the bullets.</param> /// <param name="transforms">The graphical transforms that are applied to the bullet.</param> /// <param name="initialPosition">The initial position of the bullet.</param> /// <param name="initialVelocity">The initial velocity of the bullet.</param> /// <param name="soundBank">A reference to the SoundBank used for the game's sound effects.</param> /// <param name="bullets">A reference to the list of bullets.</param> /// <param name="asteroids">A reference to the list of asteroids.</param> /// <param name="explosions">A reference to the list of explosions.</param> public Bullet(float radius, ConvexHull[] hulls, PhysicsEngine.Environment environment, Model model, Matrix[] transforms, Vector3 initialPosition, Vector3 initialVelocity, SoundBank soundBank, BulletList bullets, AsteroidList asteroids, ExplosionList explosions) { physicsReference = new NonForceEntity(initialPosition, Vector3.Up, 25, radius, new Hull(hulls), new BulletAsteroidCollision(1.0f, soundBank, bullets, asteroids, explosions)); environment.Add(physicsReference); this.bulletModel = model; this.transforms = transforms; physicsReference.Velocity = initialVelocity; }
/// <summary> /// The default constructor for the list of asteroids. /// </summary> /// <param name="maxAsteroids">The maximum number of asteroids that can be active at any time.</param> /// <param name="radius">A radius that encloses the entire asteroid model, centred at (0,0,0) in object space.</param> /// <param name="hull">The convex hull segment that enclose the asteroid.</param> /// <param name="environment">A reference to the physics engine.</param> /// <param name="model">The model used for the asteroids.</param> /// <param name="transforms">The graphical transforms that are applied to the asteroid.</param> public AsteroidList(int maxAsteroids, float radius, ConvexSegment hull, PhysicsEngine.Environment environment, Model model, Matrix[] transforms) { this.asteroids = new Asteroid[maxAsteroids]; this.asteroidRadius = radius; this.hulls = new ConvexHull[] { new ConvexHull(hull, Matrix.CreateScale(GameConstants.asteroidScale)) }; this.physics = environment; this.asteroidModel = model; this.asteroidTransforms = transforms; }
/// <summary> /// Constructs all of the necessary balls for the game. /// </summary> /// <param name="layers">The number of layers of numbered balls to construct.</param> /// <param name="ballModels">The ball models to use in constructing the balls. /// The model in the nth position is the model for the nth numbered ball. /// The model in position 0 is the model for the cue ball.</param> /// <param name="ballHull">The hull to be used for each ball.</param> /// <param name="ballTransforms">The ball transforms.</param> /// <param name="physics">The physics environment to which the balls belong.</param> public Balls(int layers, Model[] ballModels, Hull ballHull, Matrix[] ballTransforms, PhysicsEngine.Environment physics) { this.physics = physics; this.layers = layers; this.ballModels = ballModels; this.ballTransforms = ballTransforms; this.ballHull = ballHull; SetUpBalls(); }
/// <summary> /// Constructs a new ball. /// </summary> /// <param name="initialPosition">The initial position of the ball.</param> /// <param name="initialOrientation">The initial orientation of the ball.</param> /// <param name="mass">The mass of the ball.</param> /// <param name="ballRadius">The radius of the ball.</param> /// <param name="ballHull">The ball's hull.</param> /// <param name="collisionHandler">The ball's collision handler.</param> /// <param name="physics">The physics engine environment to which the ball belongs.</param> /// <param name="airResistance">The air resistance of the ball.</param> /// <param name="number">The ball's number.</param> /// <param name="ballModel">The ball's model.</param> public Ball(Vector3 initialPosition, Vector3 initialOrientation, float mass, float ballRadius, Hull ballHull, CollisionHandler collisionHandler, PhysicsEngine.Environment physics, float airResistance, int number, Model ballModel) { physicsReference = new BallEntity(initialPosition, initialOrientation, mass, ballRadius, ballHull, collisionHandler, physics, airResistance); physics.Add(physicsReference); this.number = number; this.ballModel = ballModel; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // Set up camera GameVariables.screenWidth = graphics.GraphicsDevice.Viewport.Width; GameVariables.screenHeight = graphics.GraphicsDevice.Viewport.Height; camera = new Camera(); aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height; camera.SetUpViewAndProjectionMatrices(aspectRatio); gameMode = GameVariables.initialGameMode; controls = new PlayerControls(); score = new Score(); // Set up physics engine physics = new PhysicsEngine.Environment(); PhysicsEngine.PhysicsParameters pp = new PhysicsEngine.PhysicsParameters(); pp.Gravity = GameVariables.gravity; pp.WindSpeed = GameVariables.windSpeed; physics.PhysicsParameters = pp; // Set up physical objects (walls and balls) room = new Room(camera, Content, physics); int numberOfBalls = GameVariables.ballLayers * (GameVariables.ballLayers + 1) * (GameVariables.ballLayers + 2) / 6; Model[] ballModels = new Model[numberOfBalls + 1]; // Add 1 for the cue ball ballModels[0] = Content.Load <Model>("Models\\ball_white"); for (int i = 1; i <= numberOfBalls; i++) { if (i < 10) { ballModels[i] = Content.Load <Model>("Models\\ball_0" + i.ToString()); } else if (i <= 20) { ballModels[i] = Content.Load <Model>("Models\\ball_" + i.ToString()); } else { ballModels[i] = Content.Load <Model>("Models\\ball_extra"); } } Matrix[] ballTransforms = CommonFunctions.SetupEffectDefaults(ballModels[0], camera); ConvexSegment ballSegment = PhysicsEngine.CommonFunctions.LoadConvexHull(new System.IO.StreamReader(@"..\..\..\Content/Hulls/Ball20.hull"));//@"..\..\..\Content/Hulls/UnitCube.hull")); Hull ballHull = new Hull(new ConvexHull[] { new ConvexHull(ballSegment, Matrix.CreateScale(GameVariables.ballScale)) }); balls = new Balls(GameVariables.ballLayers, ballModels, ballHull, ballTransforms, physics); camera.TargetBall = balls.CueBall; camera.SetUpViewAndProjectionMatrices(aspectRatio); base.Initialize(); }
/// <summary> /// The default constructor for the ship class. /// </summary> /// <param name="radius">An estimate to the radius that encloses the ship, measured from (0,0,0) in object space.</param> /// <param name="hull">The convex hull for the ship model.</param> /// <param name="environment">A reference to the physics engine.</param> /// <param name="model">The model used to draw the ship.</param> /// <param name="transforms">The graphics transforms to be applied to the model.</param> /// <param name="initialPosition">The initial position of the ship.</param> /// <param name="initialSpeed">The initial speed of the ship.</param> /// <param name="soundBank">A reference to the game's SoundBank.</param> /// <param name="explosions">A reference to the list of explosions.</param> /// <param name="asteroids">A reference to the list of asteroids.</param> public Ship(float radius, ConvexSegment hull, PhysicsEngine.Environment environment, Model model, Matrix[] transforms, Vector3 initialPosition, float initialSpeed, SoundBank soundBank, ExplosionList explosions, AsteroidList asteroids) { ConvexHull[] hulls = new ConvexHull[] { new ConvexHull(hull, Matrix.CreateScale(GameConstants.shipScale)) }; physicsReference = new NonForceEntity(initialPosition, Vector3.Up, 25.0f, radius, new Hull(hulls), new ShipAsteroidCollision(soundBank, explosions, this, asteroids)); environment.Add(physicsReference); this.objectModel = model; this.transforms = transforms; // Set the initial speed of the object physicsReference.Velocity = Vector3.UnitZ * initialSpeed; this.soundBank = soundBank; }
/// <summary> /// The default constructor for an asteroid. /// </summary> /// <param name="radius">A radius that encloses the entire asteroid model, centred at (0,0,0) in object space.</param> /// <param name="hulls">The convex hull segments that enclose the asteroid.</param> /// <param name="environment">A reference to the physics engine.</param> /// <param name="model">The model used for the asteroids.</param> /// <param name="transforms">The graphical transforms that are applied to the asteroid.</param> /// <param name="initialPosition">The initial position of the asteroid.</param> /// <param name="initialVelocity">The initial velocity of the asteroid.</param> public Asteroid(float radius, ConvexHull[] hulls, PhysicsEngine.Environment environment, Model model, Matrix[] transforms, Vector3 initialPosition, Vector3 initialVelocity) { physicsReference = new DefaultEntity(initialPosition, new Vector3(CommonFunctions.GenerateRandom(-1, 1), CommonFunctions.GenerateRandom(-1, 1), CommonFunctions.GenerateRandom(-1, 1)), 40, radius, new Hull(hulls), new ElasticCollision(GameConstants.elasticityFactor), environment, 0.0f); environment.Add(physicsReference); this.objectModel = model; this.transforms = transforms; physicsReference.Position = initialPosition; physicsReference.Velocity = initialVelocity; }
/// <summary> /// The default constructor for a list of bullets. /// </summary> /// <param name="maxBullets">The maximum number of bullets that can be active at any time.</param> /// <param name="bulletSpeed">The speed that each bullet moves.</param> /// <param name="radius">A radius that encloses the entire bullet model, centred at (0,0,0) in object space.</param> /// <param name="hull">The convex hull segment that encloses the bullet.</param> /// <param name="environment">A reference to the physics engine.</param> /// <param name="model">The model used for the bullets.</param> /// <param name="transforms">The graphical transforms that are applied to the bullet.</param> /// <param name="soundBank">A reference to the SoundBank used for the game's sound effects.</param> /// <param name="asteroids">A reference to the list of asteroids.</param> /// <param name="explosions">A reference to the list of explosions.</param> public BulletList(int maxBullets, float bulletSpeed, float radius, ConvexSegment hull, PhysicsEngine.Environment environment, Model model, Matrix[] transforms, SoundBank soundBank, AsteroidList asteroids, ExplosionList explosions) { this.bullets = new Bullet[maxBullets]; this.bulletVelocity = new Vector3(0, 0, (-1) * bulletSpeed); this.bulletRadius = radius; this.hulls = new ConvexHull[] { new ConvexHull(hull, Matrix.CreateScale(GameConstants.bulletScale)) }; this.physics = environment; this.bulletModel = model; this.bulletTransforms = transforms; this.soundBank = soundBank; this.asteroids = asteroids; this.explosions = explosions; }
/// <summary> /// Constructs a new room. /// </summary> /// <param name="camera">The camera to be used to draw the room.</param> /// <param name="Content">The content manager to load models from.</param> /// <param name="physics">The physics environment to which the room belogns.</param> public Room(Camera camera, ContentManager Content, PhysicsEngine.Environment physics) { wallModel = Content.Load <Model>("Models\\Wall"); wallGraphicsTransforms = CommonFunctions.SetupEffectDefaults(wallModel, camera); ConvexSegment wallSegment = PhysicsEngine.CommonFunctions.LoadConvexHull(new System.IO.StreamReader(@"..\..\..\Content/Hulls/Wall.hull")); wallHull = new ConvexHull[] { new ConvexHull(wallSegment, Matrix.Identity) }; // Create wall entities leftWall = CreateWallEntity(-Vector3.UnitX, -MathHelper.PiOver2 * Vector3.UnitZ); rightWall = CreateWallEntity(Vector3.UnitX, +MathHelper.PiOver2 * Vector3.UnitZ); bottomWall = CreateWallEntity(-Vector3.UnitY, 0.0f * Vector3.UnitX); topWall = CreateWallEntity(Vector3.UnitY, MathHelper.Pi * Vector3.UnitX); backWall = CreateWallEntity(-Vector3.UnitZ, +MathHelper.PiOver2 * Vector3.UnitX); frontWall = CreateWallEntity(Vector3.UnitZ, -MathHelper.PiOver2 * Vector3.UnitX); physics.Add(new Entity[] { leftWall, rightWall, bottomWall, topWall, backWall, frontWall }); }
/*************************************************************************************************************************/ /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // Camera Initialisation camera = new Camera(); camera.ViewMatrix = Matrix.CreateLookAt(camera.CameraPosition, camera.CameraFocusOn, Vector3.Up); camera.ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); // Physics Initialisation PhysicsParameters paramaters = new PhysicsParameters(); paramaters.Gravity = new Vector3(0.0f, -10.0f, 0.0f); paramaters.WindSpeed = new Vector3(0.0f, 0.0f, 0.0f); physics = new PhysicsEngine.Environment(); physics.PhysicsParameters = paramaters; base.Initialize(); }
/// <summary> /// Handles collisions between the ship and an asteroid. /// </summary> /// <param name="left">The first object involved in the collision.</param> /// <param name="right">The second object involved in the collision.</param> /// <param name="point">The point that has been detected inside a hull.</param> /// <param name="normal">The normal to the closest surface to the point.</param> /// <param name="dist">The distance to the closest surface.</param> /// <param name="environment">A reference to the physics engine.</param> public override void Collide(Entity left, Entity right, Vector3 point, Vector3 normal, float dist, PhysicsEngine.Environment environment) { if (ship.IsAlive) { // Update Score Score.asteroidsHit++; explosions.AddExplosion(left.Position); ship.IsAlive = false; // Call the base collision handler base.Collide(left, right, point, normal, dist, environment); // Play the explosion sound soundBank.PlayCue("explosion2"); } }
/// <summary> /// Constructs a new ball with default orientation, mass, radius, elasticity, and air resistance. /// </summary> /// <param name="initialPosition">The initial position of the ball.</param> /// <param name="physics">The physics engine environment to which the ball belongs.</param> /// <param name="ballHull">The ball's hull.</param> /// <param name="number">The ball's number.</param> /// <param name="ballModel">The ball's model.</param> public Ball(Vector3 initialPosition, PhysicsEngine.Environment physics, Hull ballHull, int number, Model ballModel) : this(initialPosition, GameVariables.initialBallOrientation, GameVariables.ballMass, GameVariables.ballRadius, ballHull, new ElasticCollision(GameVariables.ballElasticity), physics, GameVariables.ballAirResistance, number, ballModel) { }
public GameEntity(float radius, Hull hull, PhysicsEngine.Environment enviroment) : base(RandomVector(), RandomVector(), RandomMass(), radius, hull, new ElasticCollision(GameConstants.Elasticity), enviroment, RandomAirResistance()) { this.Velocity = RandomVelocity(); }
/// <summary> /// Constructs a new BallEntity. /// </summary> /// <param name="position">The position of the ball.</param> /// <param name="orientation">The orientation of the ball.</param> /// <param name="mass">The mass of the ball.</param> /// <param name="boundingRadius">The bounding radius of the ball.</param> /// <param name="hull">The ball's hull.</param> /// <param name="collisionHandler">The ball's collision handler.</param> /// <param name="environment">The environment to which the ball belongs.</param> /// <param name="coefficientOfAirResistance">The air resistance coefficient of the ball.</param> public BallEntity(Vector3 position, Vector3 orientation, float mass, float boundingRadius, Hull hull, CollisionHandler collisionHandler, PhysicsEngine.Environment environment, float coefficientOfAirResistance) : base(position, orientation, mass, boundingRadius, hull, collisionHandler, environment, coefficientOfAirResistance) { }
/// <summary> /// Handles collisions between a bullet and an asteroid. /// </summary> /// <param name="left">The first object involved in the collision.</param> /// <param name="right">The second object involved in the collision.</param> /// <param name="point">The point that has been detected inside a hull.</param> /// <param name="normal">The normal to the closest surface to the point.</param> /// <param name="dist">The distance to the closest surface.</param> /// <param name="environment">A reference to the physics engine.</param> public override void Collide(Entity left, Entity right, Vector3 point, Vector3 normal, float dist, PhysicsEngine.Environment environment) { // Increase the score Score.AsteroidHit(); base.Collide(left, right, point, normal, dist, environment); // Add an explosion to the list of explosions explosions.AddExplosion(left.Position); soundBank.PlayCue("explosion2"); }