githubEdit

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:

LineTutorial.png Note that FlatRedBall expects shape colors to be pre-multiplied. Therefore a half-transparent red value would have a R,G,B,A value of (128,0,0,128).

RelativePoint Properties

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

Add the following in Initialize after Initializing FlatRedBall:

ConnectedRectangles.png

Line limitations

  • Lines, just like any other Shapesarrow-up-right, can either be drawn on top of or below types that can sort with each other, such as Spritesarrow-up-right and Textsarrow-up-right. 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

Was this helpful?