Mike
Mike

Reputation: 146

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

Upvotes: 0

Views: 662

Answers (2)

annonymously
annonymously

Reputation: 4708

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 ;)

Upvotes: 2

phadaphunk
phadaphunk

Reputation: 13273

For a 2D game, Rectangles are a quick/easy way to get a basic collision detection.

MSDN article here

Since you only have to set the Rectangle boundaries for each object you want to detect collisions and then test the Rectangle1.Intersects(Rectangle2) Condition to see if they touch.

Upvotes: 2

Related Questions