Lines and Collisions

Line Collisions

This small line collision demo demonstrates the rendering and collisions of lines. The anchors change color based on the mouse position (above or below a line).

While XNA provides line lists and other primitives, I chose to draw lines with a simple one pixel texture applied to a rectangle.

// initialize

texture = new Texture2D(GraphicsDevice, 1, 1, false,
Int32[] pixel = { 0xFFFFFF };
texture.SetData<Int32>(pixel, 0, texture.Width * texture.Height);

// update

Vector2 firstPosition = new Vector2(10, 10);
Vector2 SecondPosition = new Vector2(100, 100);

Vector2 diff = secondPosition - firstPosition;
Vector2 scale = new Vector2(1.0f, diff.Length() / size);
float angle = (float)(Math.Atan2(diff.Y, diff.X)) - MathHelper.PiOver2;

// render

spriteBatch.Draw(texture, firstPosition, new Rectangle(0, 0, 1, 1),
    Color.White, angle, Vector2.Zero, scale, SpriteEffects.None, 1.0f);

September 15, 2011 | project

Hi, I am Chris Brough. I am a twenty-one year old senior studying Game Programming at Champlain College in Burlington, Vermont.

I've had an immense interest in technology and video games for as long as I can remember. When I'm not developing my knowledge in programming, I enjoy drawing and playing video games.

Among the many interests I have in programming, I'm currently focused on data-oriented design, networking, and 3D graphics.

Contact info: