Line

Introduction

A line is defined by two endpoints, so in mathematical terms it is actually a segment. Lines can be used to perform 2D collisions against any other shapes.

Line Sample

The following sample creates a line, a Circle, and an AxisAlignedRectangle. The line is controlled with the keyboard and it changes colors when it collides with the other two shapes. Add the following using statements:

using Microsoft.Xna.Framework.Graphics;
using FlatRedBall.Math.Geometry;

Add the following at class scope:

AxisAlignedRectangle rectangle;
Circle circle;
Line line;

Add the following in Initialize after Initializing FlatRedBall:

rectangle = ShapeManager.AddAxisAlignedRectangle();
rectangle.X = 50;
rectangle.Color = Color.Red;

circle = ShapeManager.AddCircle();
circle.X = -50;
circle.Color = Color.Blue;

// Creates a horizontal line of length 2 by default.
line = ShapeManager.AddLine();

Add the following in Update:

InputManager.Keyboard.ControlPositionedObject(line);

if (line.CollideAgainst(rectangle))
{
    line.Color = rectangle.Color;
}
else if (line.CollideAgainst(circle))
{
    line.Color = circle.Color;
}
else
{
    line.Color = Color.White;
}

RelativePoint Properties

A line can be modified by changing both its PositionedObject properties as well as through the RelativePoint property. The following code connects two rectangles with a line. Add the following in Update:

using Microsoft.Xna.Framework.Graphics;
using FlatRedBall.Math.Geometry;

Add the following in Initialize after Initializing FlatRedBall:

AxisAlignedRectangle rectangle1 = ShapeManager.AddAxisAlignedRectangle();
rectangle1.Position = new Vector3(-2, 4, 0);
rectangle1.Color = Color.Green;

AxisAlignedRectangle rectangle2 = ShapeManager.AddAxisAlignedRectangle();
rectangle2.Position = new Vector3(4, -1, 0);
rectangle2.Color = Color.Red;

// The most common way to make a connecting line is to set the line's
// positions at one of the end points, then simply calculate the relative
// position of the other endpoint.  That is, the position of the line does
// not have to be the midpoint.
Line connectingLine = ShapeManager.AddLine();
connectingLine.Position = rectangle1.Position;
connectingLine.RelativePoint1 = new Point3D(0, 0, 0);

// Now just get the position of rectangle2 relative to rectangle1
// and put that in RelativePoint2.
connectingLine.RelativePoint2 = new Point3D(
    rectangle2.X - rectangle1.X,
    rectangle2.Y - rectangle1.Y,
    rectangle2.Z - rectangle1.Z); // will be 0 in our case

Line limitations

  • Lines, just like any other Shapes, can either be drawn on top of or below types that can sort with each other, such as Sprites and Texts. They will not sort according to their Z value.

  • Lines must be one pixel thick. Thicker lines are not supported.

  • Lines can only draw solid colors - patterns and gradients are not supported.

Last updated