XNA Game Development Forums
2012/05/21 19:29:50 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Has anybody managed to get some collision in the game?  (Read 2118 times)
Jonotron
Newbie
*
Offline Offline

Posts: 27


View Profile
« on: 2007/05/14 19:18:41 »

So has anybody got collision in yet?  I'm really eager to figure out how this works.
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #1 on: 2007/05/15 03:49:17 »

Yes using bounding box's and spheres you can use the Intersects method to determine if two models collide.
Logged
Jonotron
Newbie
*
Offline Offline

Posts: 27


View Profile
« Reply #2 on: 2007/05/15 07:52:32 »

Thanks!  I've googled a bit on that, and there's lots of help Cheesy. What I'm confused is how to go through each object and check to see if it's collided with any other object.  When looping through objects to check for collision, where do I call the loop from (demo project or engine)?  And another big thing is the terrain collision.  My models fall through the map (because of gravity).  I noticed you put up a "GetHeightAt" function which is really useful, now how would I use it?
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #3 on: 2007/05/16 04:44:30 »

I am sure I gave an example of Terrain collision with my class, if not then I apologies.

Here is an example of terrain collision, I wrote a demo to show a friend how I was doing animation in my engine, here is the code I used

Code:
namespace DemoAnimation
{
    class DemoAnimation
    {
        static RCGame game = new RCGame();
        static RCModel tiny = new RCModel("Content/Models/tiny", "Tiny");

        public static void Main()
        {
            if (game.ValidateGraphicsCard())
            {
                game.Input.OnUpdate += new UpdateMethod(KeyboardControls);
                game.Input.OnUpdate += new UpdateMethod(CameraControls);
                game.OnEngineDraw += new EngineDrawEvent(Draw);

                SetupScene();
                game.Run();
            }
        }
        private static void SetupScene()
        {
            RCShaderManager.AddShader(new RCShader(@"Content/Shaders/Ocean"), "WAT");
            RCShaderManager.AddShader(new RCShader(@"Content/Shaders/TerrainTextured"), "TTT");
            RCShaderManager.AddShader(new RCShader(@"Content/Shaders/TransformTexture"), "TT");
            RCShaderManager.AddShader(new RCShader(@"Content/Shaders/BoneAnimationTransform"), "ANIM");

            Vector3 LightPosition = new Vector3(0, 10, 5);

            RCSkyBox sb = new RCSkyBox(new string[] { "Content/Textures/SkyBox/HazyMind/top",
                                                      "Content/Textures/SkyBox/HazyMind/bottom",
                                                      "Content/Textures/SkyBox/HazyMind/left",
                                                      "Content/Textures/SkyBox/HazyMind/right",
                                                      "Content/Textures/SkyBox/HazyMind/front",
                                                      "Content/Textures/SkyBox/HazyMind/back" }, "sb");
            sb.SetShader("TT");
            game.Scene.AddObject(sb, "sb");

            string[] textures = new string[] { "Content/Textures/Terrain/sand", "Content/Textures/Terrain/grass", "Content/Textures/Terrain/rock", "Content/Textures/Terrain/snow" };
            RCTerrain terrain = new RCTerrain(textures, "Content/Textures/Terrain/TheValley", "Terrain");
            terrain.SetShader("TTT");
            terrain.LightPosition = LightPosition;
            terrain.Position = new Vector3(0, -30, 0);
            game.Scene.AddObject(terrain, "terrain");

            RCWater water = new RCWater("Content/Textures/SkyBox/HazyMind/cubeMap", "Content/Textures/Terrain/waves2", "water");
            water.SetShader("WAT");
            water.Width = 256;
            water.Height = 256;
            water.WaveFrequency = .2f;
            water.WaveAmplitude = .1f;
            water.DeepWaterColor = Color.Navy.ToVector4();
            water.ShallowWaterColor = Color.DarkSeaGreen.ToVector4();
            water.ReflectionColor = Color.DarkGray.ToVector4();
            water.ReflectionAmount = 0.7f;
            water.Position = new Vector3(0, -26, 0);
            water.AnimationSpeed = .05f;
            game.Scene.AddObject(water, "water");

            tiny.SetShader("ANIM");
            tiny.Scaling = new Vector3(.025f, .025f, .025f);
            tiny.Position = new Vector3(0, 0, -128f);
            tiny.Rotate(new Vector3(0, 1, 0), MathHelper.Pi);
            game.Scene.AddObject(tiny, "Tiny");

            RCHelper.Fog.FogColor = Color.WhiteSmoke;
            RCHelper.Fog.FogEnable = false;
            RCHelper.Fog.FogStart = .95f;
            RCHelper.Fog.FogEnd = .999f;
            RCHelper.Fog.FogTableMode = FogMode.Linear;           
        }
        public static void Draw()
        {
            RCTerrain terrain = (RCTerrain)game.Scene.GetObject("terrain").Object;
            Vector3 offset = new Vector3(tiny.Position.X, tiny.Position.Y + 10.4f, tiny.Position.Z);
            float y = terrain.GetHeightAt(offset).Z;
            if (terrain.Collision(offset, 0))
            {
                game.WriteText(game.Font, 10, 200, "Collision", Color.Red);
                tiny.Position = new Vector3(offset.X, (y - 10.6f), offset.Z);
            }
            if (y < offset.Y + .2f)
            {
                tiny.Position = new Vector3(offset.X, (y - 10.4f) - .1f, offset.Z);
            }
            tiny.Translate(new Vector3(0, 0, -.25f));

            if (tiny.Position.Z > 128)
                tiny.Position = new Vector3(0, 0, -128f);
        }

        public static void KeyboardControls()
        {
            if (game.Input.Keys.Contains(Keys.Escape))
            {
                if (game.Sounds != null)
                    game.Sounds.StopAllMusic();

                if (game.Graphics.IsFullScreen)
                    game.Graphics.ToggleFullScreen();

                game.Exit();
                return;
            }

            if (game.Input.Keys.Contains(Keys.F))
            {
                if (!game.Graphics.IsFullScreen)
                    game.Graphics.ToggleFullScreen();
            }
            if (game.Input.Keys.Contains(Keys.N))
            {
                if (game.Graphics.IsFullScreen)
                    game.Graphics.ToggleFullScreen();
            }

            if (game.Input.Keys.Contains(Keys.Left))
            {
                RCCameraManager.ActiveCamera.Translate(new Vector3(-0.1f, 0, 0));
            }
            if (game.Input.Keys.Contains(Keys.Right))
            {
                RCCameraManager.ActiveCamera.Translate(new Vector3(0.1f, 0, 0));
            }

            if (game.Input.Keys.Contains(Keys.Up))
                RCCameraManager.ActiveCamera.Translate(new Vector3(0, 0, -0.1f));

            if (game.Input.Keys.Contains(Keys.Down))
                RCCameraManager.ActiveCamera.Translate(new Vector3(0, 0, 0.1f));


            if (game.Input.Keys.Contains(Keys.X))
            {
                RCCameraManager.ActiveCamera.RevolveTarget = new Vector3(0, 0, 1);
                RCCameraManager.ActiveCamera.Position = new Vector3(0, 0, 1);
                RCCameraManager.ActiveCamera.Rotation = new Quaternion(0, 0, 0, 1);
            }
        }
        public static void CameraControls()
        {
            Vector2 mouseMove = game.Input.MouseMoved;

            RCCameraManager.ActiveCamera.Rotate(new Vector3(1, 0, 0), mouseMove.Y * 0.01f);
            RCCameraManager.ActiveCamera.Rotate(new Vector3(0, 1, 0), mouseMove.X * 0.01f);
        }
    }
}

The terrain collision for tiny is in the Draw method. For collision detection for model on model take a look at the ray picking code and substitute the ray with the model you want to check for collision against.
Logged
Jonotron
Newbie
*
Offline Offline

Posts: 27


View Profile
« Reply #4 on: 2007/06/17 13:39:41 »

 Does the terrain collision detecting have to be done in the demo.Draw() Procedure?  I want to make a physicsmanager and call that from the engine.Update() sub.  Could I do it that way?

[EDIT]I've managed to get that into my physicsManager.  I have another problem, when I try to make it so the camera cannot go through the terrain... it goes through the terrain at some points then at other points, it does what I want it to do.  When it DOES go through the terrain at some points, it stops it approximately 5f from the terrain.

Here is the code for camera collision:
Code:
public static void CheckCameraCollision()
        {
            //check terrain collision first
            Terrain terr = (Terrain)ObjectManager.Objects["terrain"];
           
            if (terr != null)
            {
                Vector3 campos = CameraManager.ActiveCamera.Position;
                Vector3 offset = new Vector3(campos.X, campos.Y + 5f, campos.Z);
                float y = terr.GetHeightAt(offset).Z;

                if (terr.Collision(offset, 0))
                {
                    //COLLISION
                    CameraManager.ActiveCamera.Position = new Vector3(offset.X, (y + 5.2f), offset.Z);
                }
                if (y < offset.Y + .2f)
                {
                    CameraManager.ActiveCamera.Position = new Vector3(offset.X, (y + 5f) - .1f, offset.Z);
                }

                if (CameraManager.ActiveCamera.Position.Z > 128)
                    CameraManager.ActiveCamera.Position = new Vector3(0, 0, -128f);

            }
        }
« Last Edit: 2007/06/17 18:36:55 by Jonotron » Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #5 on: 2007/06/19 01:22:35 »

I had this issue, it turned out that I wax mixing up my X and Y planes when checking for height in the height map. Use the GetHeightAt method to find out what your collision function thinks the terrain height should be where you collide with the terrain.
Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
MOVED: XNA Tutorials :: Game Engine and Game Development :: Tutorial 1 Hazy Mind 3D Engine mikeschuld 0 2365 Last post 2006/10/30 21:35:32
by mikeschuld
Xna vs Managed DX Hazy Mind XNA Engine jadams 13 4129 Last post 2007/03/09 11:55:12
by Tiago
New XNA Game General Discussion YellowShadow 5 2325 Last post 2007/04/25 22:10:09
by XNASorcerer
Problems with dll attempting managed Hazy Mind 3D Engine pjank42 2 2896 Last post 2007/03/11 18:42:00
by mikeschuld
Terrain Traversing/Collision Hazy Mind XNA Engine Quinn 1 1605 Last post 2007/02/28 00:27:04
by Ardman
Detecting Model Collision...? General Discussion « 1 2 » DaphydTheBard 15 4291 Last post 2007/03/27 01:29:52
by Nemo Krad
Climbing stairs with collision detection Hazy Mind XNA Engine internetking 8 3789 Last post 2007/03/19 16:32:35
by mikeschuld
Mouse selection (collision?) Hazy Mind XNA Engine Requests Zan 2 2699 Last post 2007/05/19 16:32:50
by Zan
3D Collision Detection and Physics General Discussion « 1 2 » LucianX 15 4650 Last post 2007/08/02 20:54:38
by mikeschuld
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.154 seconds with 18 queries.