Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
        }    
    }
share

migration rejected from stackoverflow.com Sep 26 '13 at 18:37

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as off-topic by Anko, Sean Middleditch, Jimmy Shelter, Seth Battin, bummzack Sep 26 '13 at 18:37

  • This question does not appear to be about game development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
I got the camera to rotate. I actually had to change bother the camera position and the lookat in the Y- axis. Still needing help with the collisions. Vector3 cameraPosition = new Vector3(0.0f, 120.0f, -100.0f); Vector3 cameraLookAt = new Vector3(0.0f, -20.0f, 0.0f); –  user2023188 Feb 3 '13 at 2:03
4  
This question appears to be off-topic because it is about debugging your code for you. –  Anko Sep 20 '13 at 14:26

1 Answer 1

After calling TestCollision, you could simply perform the following action:

if(sphereSphere.Intersects(surfaceSphere))
{
    sphere.velocity = -sphere.velocity;
}

This will simply 'bounce' the spheres off of each other.

For a more relaistic physics response, it is a good idea to use a physics engine or see what you learn in your physics module.

share