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 2D engine in Xna and am running in a bit of trouble with Collision implementation. What I want to do is have an interface ICollidable that I can add to GameObjects that are collidable.
I am trying to get it to work by using this awnser: http://gamedev.stackexchange.com/a/40432/33751

My question is how do I add the objects that use ICollidable to the List in the CollisionManager dynamically?

By case would be that the manager 'sees' what objects use the ICollidable interface and automatically add it to the list.

Second question is, how can I get the properties and stuff from the objects that contain the interface to actually preform the collision detection in CollisionManager? would a simple cast suffice?

ICollidable:

interface ICollidable
{
    void onCollision(ICollidable other);
}

CollisionManager:

class CollisionManager : GameObject
{

    private List<ICollidable> collidables = new List<ICollidable>();

    public CollisionManager(Game game)
        : base(game)
    {
        game.Components.Add(this);
    }


    void addCollidable(ICollidable obj){
        // This function needs to be called when ICollidable is inherited dynamically
        collidables.Add(obj);
    }

    public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
    {
        base.Update(gameTime);

        for (int i = 0; i < collidables.Count; i++)
        {
            for (int a = i + 1; a < collidables.Count; i++)
            {
                if(collision(collidables[i], collidables[a])){
                    collidables[i].onCollision(collidables[a]);
                }
            }
        }

    }

    private bool collision(ICollidable obj)
    {
        // How can i get obj's properties etc for checking collision.

        return false; 
    }
}

PS. Never worked with interfaces in c# before, if I am doing it completely wrong, please let me know

Thanks in advance.
Yme-Jan

share|improve this question

1 Answer 1

up vote 0 down vote accepted

For example if you want to get the ICollidable's "BoundingBox" (Rectangle), you have to add it to your interface.

interface ICollidable
{
    void onCollision(ICollidable other);
    Rectangle boundingBox {get; set;}
}

Of course you have to implement them in the Class which implements the interface too.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.