Search code examples
c#xnacollision-detection

Xna Collision Dection


Hi guys im making a simple tron lightcycle type game in xna and wondered if anyone knew what the most efficient way of drawing the trail and doing collision on it would be?

Thanks


Solution

  • That really depends on whether it's a 3d or 2d game, but I think these tutorials should cover the way of doing effects for both:

    http://rbwhitaker.wikidot.com/3d-tutorials

    http://rbwhitaker.wikidot.com/2d-tutorials

    For 3d collision detection, you can use BoundingBox objects to approximate a light cycle, and possibly a BoundingSphere could also come in handy.4

    EDIT:

    Where LightEmitter is a Vector2 where the trail will come out,

    LightTrail is a Texture2D with a 1-pixel wide section of light trail,

    LastEmitterPos is a Vector2 showing the last position of LightEmitter

    and Trails is a RenderTarget2D whose RenderTargetUsage is set to RenderTargetUsage.PreserveContents:

    example Draw method:

    GraphicsDevice.SetRenderTarget(Trails);
    
    spriteBatch.Begin();
    
    
    for (float i = 0; i <= (LightEmitter - LastEmitterPos).Length(); i++)
    {
        Vector2 Pos = Vector2.Lerp(LastEmitterPos, LightEmitter, i/(LightEmitter - LastEmitterPos).Length());
        spriteBatch.Draw(LightTrail, Pos, new Rectangle(0, 0, 32, 3), Color.White, MathHelper.ToRadians(90.0f), new Vector2(16, 1.5f), 1.0f, SpriteEffects.None, 1f);
    }
    
    spriteBatch.End();
    
    GraphicsDevice.SetRenderTarget(null);
    
    spriteBatch.Begin();
    spriteBatch.Draw(Trails, Vector2.Zero, Color.White);
    spriteBatch.End();
    
    base.Draw(gameTime);
    

    There are a few glitches and a lot of optimization needed here but I'm sure this can give you a good idea what direction to go in. Or at least a bit of encouragement if nothing else ;)