FlatRedBall.Math.Geometry.Line
The LastCollisionPoint is a property which can tell you the last location where collision occurred. This can be used if you would like to play effects or perform custom physics where collision has occurred.
The following example creates two Lines and calls CollideAgainst between them. It then uses the LastCollisionPoint (if it is valid) to position a circle. One of the lines (Line1) can be controlled with the keyboard, resulting in a changing LastColliisionPoint.
This example is structured for Glue, but can be duplicated in a code-only FlatRedBall project:
Add the following to class scope in a Screen:
Add the following to CustomInitialize in a Screen:
Add the following to CustomActivity in a Screen:
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.
Add the following at class scope:
Add the following in Initialize after Initializing FlatRedBall:
Add the following in Update:
Add the following in Initialize after Initializing FlatRedBall:
Lines must be one pixel thick. Thicker lines are not supported.
Lines can only draw solid colors - patterns and gradients are not supported.
The following sample creates a line, a , and an . The line is controlled with the keyboard and it changes colors when it collides with the other two shapes. Add the following using statements:
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).
A line can be modified by changing both its properties as well as through the RelativePoint property. The following code connects two rectangles with a line. Add the following in Update:
Lines, just like any other , can either be drawn on top of or below types that can sort with each other, such as and . They will not sort according to their Z value.
The SetFromAbsoluteEndpoints method can be used to set a line so that its endpoints are located at the argument locations. This method assigns:
RelativePoint1
RelativePoint2
Position
RotationZ
Consider the following code:
The code above shows how to create a line using the SetFromAbsoluteEndpoints. To help explain what SetAbsoluteEndpoints does, we can consider how to create the same line without using SetFromAbsoluteEndpoints. To do the same as above, the following assignments would be necessary:
RelativePoint1 = (-50, 0, 0)
RelativePoint2 = (50, 0, 0)
Position = (50, 0, 0)
RotationZ = 0
The following code creates a line which connects the points X=0,Y=0 and X=100,Y=200:
The RelativePoint1 and RelativePoint2 values define the shape of a line relative to its absolute position. These values can be set to modify how a line will draw - specifically its angle and length. For simplicity the RelativePoint1 can be set to (0,0), so simply modifying the second point will adjust the line.
RelativePoint1 and RelativePoint2 are used to define the line in combination with the absolute point. The most common, and recommended, approach to create a line is to have RelativePoint1 be (0,0) and have RelativePoint2 define the direction of the line relative to its position. For example, consider the following code and image:
Although not recommended, RelativePoint1 and RelativePoint2 can be set such that the Line's position is not the Line.
Although FlatRedBall allows this kind of Line, and in some cases it may be beneficial for custom logic, it is not recommended for standard collision, and some methods (such as CollideAgainstClosest) will throw exceptions if the Position does not coincide with RelativePoint1. Therefore, unless your game has special requirements, RelativePoint1 should always have a Value of (0,0).
The following code creates a vertical line. The bottom-end of the line will be located at the line's absolute Position. The top-end of the line will be located 100 units up.
The following code creates a horizontal line. The left-end of the line will be located at the line's absolute Position. The right-end of the line will be located 100 units up.
The Line class supports 3D lines (lines with non-zero relative points or absolute positions). The following shows how to create a grid of 3D lines. Note that it requires a 3D camera: