I am working on a game programming class project and need some help. I cannot find a good tutorial on how to get these 3 frictionless spheres to collide with each-other, the surface and the walls of the environment. Also, I cannot get the camera to tilt down on the X-axis towards the surface of the environment. This is what I have so far:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace MyProject1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GameObject surface = new GameObject();
Random r = new Random();
const int numSpheres = 3;
GameObject[] spheres;
Vector3 sphereMinPosition = new Vector3(-65.0f, 1.0f, 10.0f);
Vector3 sphereMaxPosition = new Vector3(65.0f, 50.0f, 0.0f);
Vector3 cameraPosition = new Vector3(0.0f, 0.0f, -150.0f);
Vector3 cameraLookAt = new Vector3(0.0f, 10.0f, 0.0f);
Matrix cameraProjectionMatrix;
Matrix cameraViewMatrix;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
cameraViewMatrix = Matrix.CreateLookAt(
cameraPosition,
cameraLookAt,
Vector3.Up);
cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f),
graphics.GraphicsDevice.Viewport.AspectRatio,
1.0f,
10000.0f);
spheres = new GameObject [numSpheres];
for (int i = 0; i < numSpheres; i++) {
spheres[i] = new GameObject();
spheres[i].model = Content.Load<Model>(
"Models\\sphere");
spheres[i].scale = 0.5f;
}
surface.model = Content.Load<Model>("Models\\surface");
surface.scale = 5.0f;
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
UpdateSpheres();
base.Update(gameTime);
}
void UpdateSpheres()
{
foreach (GameObject sphere in spheres)
{
if (sphere.alive)
{
sphere.position += sphere.velocity;
if (sphere.position.Y < -20.01f)
{
sphere.alive = false;
}
}
else
{
sphere.alive = true;
sphere.position = new Vector3(
MathHelper.Lerp(
sphereMinPosition.X,
sphereMaxPosition.X,
(float)r.NextDouble()),
MathHelper.Lerp(
sphereMinPosition.Y,
sphereMaxPosition.Y,
(float)r.NextDouble()),
MathHelper.Lerp(
sphereMinPosition.Z,
sphereMaxPosition.Z,
(float)r.NextDouble()));
sphere.velocity = new Vector3(0.0f, -1.0f, 0.0f);
}
}
}
void TestCollision(GameObject surface)
{
BoundingSphere surfaceSphere =
surface.model.Meshes[0].BoundingSphere;
surfaceSphere.Center = surface.position;
surfaceSphere.Radius *= surface.scale;
foreach (GameObject sphere in spheres)
{
if (sphere.alive)
{
BoundingSphere sphereSphere =
sphere.model.Meshes[0].BoundingSphere;
sphereSphere.Center = sphere.position;
sphereSphere.Radius *= sphere.scale;
if(sphereSphere.Intersects(surfaceSphere))
{
}
}
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawGameObject(surface);
foreach (GameObject sphere in spheres)
{
if (sphere.alive)
{
DrawGameObject(sphere);
}
};
base.Draw(gameTime);
}
void DrawGameObject(GameObject gameObject)
{
foreach (ModelMesh mesh in gameObject.model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
effect.World = Matrix.CreateFromYawPitchRoll(
gameObject.rotation.Y,
gameObject.rotation.X,
gameObject.rotation.Z) *
Matrix.CreateScale(gameObject.scale) *
Matrix.CreateTranslation(gameObject.position);
effect.Projection = cameraProjectionMatrix;
effect.View = cameraViewMatrix;
}
mesh.Draw();
}
}
}
}
And here is the Game Object class:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace MyProject1
{
class GameObject
{
public Model model = null;
public Vector3 position = Vector3.Zero;
public Vector3 rotation = Vector3.Zero;
public float scale = 1.0f;
public Vector3 velocity = Vector3.Zero;
public bool alive = false;
}
}
