Collision Detection

Jul 27, 2010 at 1:17 PM
I was wondering if there are any examples or samples of how to create events based on collision and learn how to use collision detection properly. I'm a newbie to game creating but would find it helpful. I'm trying to have a character and a soccer ball and the ball being in the middle but if he touches it then he is in control of the ball. This might be asking a lot but it would be very helpful since my 5 hours of playing with the XNA and Ox game engine I can just get a model to display in my scene.
Editor
Jul 28, 2010 at 9:47 AM

hey there, i wrote a project that used collision to add scores and destroy objects, im kinda out of the programming scene over the summer months, but ill see if i can dig it out when i get home from work (its kinda simple).

-bubzy.

Jul 28, 2010 at 2:41 PM
Thanks bubzy most appreciated. I checked your previous post but the link to your sample is a broken link.
Editor
Jul 29, 2010 at 8:23 PM
Edited Jul 29, 2010 at 8:24 PM

oooook

heres one code file, it may take some time to decipher it, but this is part of the example project i had up and running.

using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Physics;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Ox.Engine;
using Ox.Engine.Component;
using Ox.Engine.Utility;
using Ox.Scene.Component;

namespace oxanoid
{
    public class ImmovableSkinPredicate : CollisionSkinPredicate2
    {
        public override bool ConsiderSkinPair(CollisionSkin skin0, CollisionSkin skin1)
        {
            if (skin0.OnCollisionEvent(skin0,skin1))
            {
                return true;
            }
            else
            {
                return false;
            }
            return false;
        }
    }

    public class ExampleBall : ComponentScript<IStandardModel>, ICollidable
    {
        BatScript batCollisions;
        ExampleBlock[] wallCollisions;
        CollisionSkin colSkin;
        Blocks[] block;
        public CollisionSkin ballSkin;
        int time = 0;
        
        bool hitbat = false;

        public ExampleBall(IOxEngine engine, string parameters, Transfer<IOxComponent> component)
            : base(engine, parameters, component)
        {
            body = CreateBody();
            block = new Blocks[14];
            for (int i = 0; i < 14; i++)
            {
                int o = i + 1;
                block[i] = engine.GetScript<Blocks>("Block" +o);
            }

            batCollisions = Engine.GetScript<BatScript>("Bat");
            wallCollisions = new ExampleBlock[4];
            wallCollisions[0] = engine.GetScript<ExampleBlock>("TopWall");
            wallCollisions[1] = engine.GetScript<ExampleBlock>("LeftWall");
            wallCollisions[2] = engine.GetScript<ExampleBlock>("RightWall");
            wallCollisions[3] = engine.GetScript<ExampleBlock>("RottenWall");

            colSkin = batCollisions.skint;
        }

        public bool OwnsCollisionSkin(CollisionSkin skin)
        {
            return body.CollisionSkin == skin;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) DestroyBody();
            base.Dispose(disposing);
        }

     

        private Body CreateBody()
        {
            Vector3 position = Component.PositionWorld;
            Matrix orientation = Component.OrientationWorld;
            Body result = new Body();
            result.CollisionSkin = new CollisionSkin(result);
            result.CollisionSkin.AddPrimitive(
                new Sphere(Vector3.Zero, radius),
                new MaterialProperties(0.8f, 0.8f, 0.7f));
            result.Mass = mass;
            result.MoveTo(position, orientation);
        //    result.CollisionSkin.callbackFn += new CollisionCallbackFn(handleCollisionDetection);
            result.CollisionSkin.callbackFn += new CollisionCallbackFn(handleCollisionDetectionBlocks);
            ballSkin = result.CollisionSkin;
            result.EnableBody();
            return result;
        }

        private void DestroyBody()
        {
            body.DisableBody();
        }

        private void UpdateTransform()
        {
            
            Component.Position = body.Position;
            Component.Orientation = body.Orientation;
        }
        protected override void UpdateHook(GameTime gameTime)
        {
            base.UpdateHook(gameTime);
            time++;
            body.ApplyWorldImpulse(body.OldVelocity);
            body.Position = new Vector3(body.Position.X, body.Position.Y, 0);
            isBallOffScreen();
            UpdateTransform();
            
            
        }

        protected void isBallOffScreen()
        {
            if (body.Position.Y < -100)
            {
                batCollisions.lives -= 1;
             
                body.MoveTo(new Vector3(-10, 20, 0), Matrix.CreateRotationX(0));
            }
        }

        public bool handleCollisionDetectionBlocks(CollisionSkin owner, CollisionSkin collidee)
        {
            // here is handled what happens if your Object collides with another special Object (= OtherObject)
            
                for (int o = 0; o < 14; o++)
                {
                    if (collidee.Equals(block[o].colSkin)) // if the ball hits a block component.
                    {
                        if (block[o].time > 20)
                        {
                            block[o].time = 0;
                            block[o].hitPoints -= 10;
                            batCollisions.score += 10;
                            return true;
                        }
                    }
                }
                for (int i = 0; i < 3; i++)
                {    
                if (collidee.Equals(wallCollisions[i].colSkin)|| collidee.Equals(colSkin))
                {

                    return true;
                } 
                
                else if (collidee.Equals(body.CollisionSkin))
                {
                    return false;
                }
                // all other collisions will be handled by physicengine
            }
           

            return false;
        }



        private const float radius = 5;
        private const float mass = 100;

        private readonly Body body;
    }
}

 

hope this helps a little. the important thing is to set up the collision skins and the predicates

as you may be able to see, this script is calling in other scripts which have their collision skins already set up and loading these into arrays. it will then check an object when the skins intersect and decide what to do based on your code.

really sorry if my explanation is poor, im totally out of practice with this (building synthesizers with electronics at the moment ) :D

please ask any questions however as it may stimulate the few lonely neurons festering in the recesses of my skull.

please note that some of this code may be outdated too due to the engine updates since this was written, dont copy and paste it, just get the idea :D

-bubzy

Jul 29, 2010 at 9:30 PM

Hey bubzy,

 

Thanks for uploading that code I'm still confused on what certain aspects of the code does. Is there a complete solution for all this? I'm just curious to what these scripts look like

  batCollisions = Engine.GetScript<BatScript>("Bat");

wallCollisions[0] = engine.GetScript<ExampleBlock>("TopWall");
wallCollisions[1] = engine.GetScript<ExampleBlock>("LeftWall");
wallCollisions[2] = engine.GetScript<ExampleBlock>("RightWall");
wallCollisions[3] = engine.GetScript<ExampleBlock>("RottenWall");

I get the wall would bounce off which is easy enough just not sure what code is in the walls or the bat. Once I get this I should have a better idea on how to plan my soccer game based on collisions and how to handle them. Thank you.
Editor
Jul 30, 2010 at 5:56 AM

http://rapidshare.com/files/409932172/oxanoid.zip

i think this is limited to 10 downloads, but its the entire project at least all the code files, hopefully you can get an idea here

incidentally, the objects "wallcollisions[0] + (1,2,&3)" are all part of the exampleblock script set as they are just surfaces to bounce off.

hope this helps

-bubzy.

Jul 30, 2010 at 11:56 PM
I will be checking out your example and play around with it thanks bubzy. I really appreciate it.
Aug 1, 2010 at 12:19 AM
Hey Bubzy, I tried your project and can't get it to compile. I've been getting errors after re-adding all the references to the project. IStandardModel also the IOxEngine doesn't work a total of 16 compiled errors. I'm not sure if it was due to this project being older or what but if you can help me on that it would be much appreciated so I can build the example and see how it works. Thanks, Schuranator
Editor
Aug 2, 2010 at 9:38 PM

i really don't have the time to rewrite this code to work with the current engine build im afraid. it was posted with its intention of being a reference work as opposed to being a working example game (although this was its original purpose)

however. bryan removed the superfluous 'I' references. you may try just removeing them so instead of "IOxEngine" you would be using just plain old "OxEngine".

other than this ill try and squeeze some bits of coding in and see if i can rework it, but no promises.

cheers

-bubzy

Aug 3, 2010 at 1:28 AM
Hey Bubzy, I made all the changes you suggested plus some for the Component.ScaleWorld; which isn't available. But get an TargetInvocationException when I try to build. I just wanted to test out your example but I've played with it for over 3 hours with no progress so if you can find some time that would be wonderful.
Editor
Aug 6, 2010 at 6:31 AM

Ok, ive written a short prog to demostrate collision detection, the old oxanoid was way too far gone for me to update it nearly went beserk trying to do so,

this has pretty much the same functionality ( yes it doesnt work 100% but i wrote it in an evening :p ) you control the ball with the gamepad.

http://rapidshare.com/files/411329958/Collidatron.zip

hope this helps

-bubzy

p.s i didnt clean up the code, its got loads of edited out crap in it, let me know if theres a particular line that you are struggling with (curse my bad coding habits)