1 of 11

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Introduction

The MathFunctions class provides helper methods for performing common game math.

World and Screen Coordinates

World to Pixel

The following code returns the screen pixel coordinates of a PositionedObject. Remember, by by default in world coordinates:

Positive X points to the right

Positive Y points up

In pixel coordinates

Positive X points to the right

**Positive Y points down (only when dealing with screen pixels)**

Add the following using statements

Assuming myObject is a valid PositionedObject

Pixel to World

Introduction

AbsoluteToWindow converts an absolute coordinate to a window coordinate (where 0,0 is the top left of the screen). This can be used to identify the screen coordinates of a FlatRedBall object. This is sometimes necessary to translate between 2D and 3D coordinates, or to obtain screen coordinates for systems which use screen-based coordinates (like Gum).

Code Example

The following can be used to obtain the screen coordiantes of a FlatRedBall Sprite:

Introduction

The RegulateAngle method can be used on a radian value to modify it so that it is always between 0 and 2*PI. This method is internally used on the PositionedObject class to keep all rotation and relative rotation values between 0 and 2*PI.

Method Signature

The method has two overloads: one that returns a modified float, and one that can modify a float passed as a ref:

Usage

This method can be used if it is more convenient for you to work with values between 0 and 2*PI. This method will not modify any values that are between 0 and 2*PI. It will modify values which are less than 0 and more than 2*PI.

For the graph below, we will use the following code:

Introduction

The GetPointInCircle method returns a random point inside a circle with the argument radius, centered at 0,0. This method returns an even distribution, as opposed to a "random radius, random angle" approach which returns a higher concentration of points at the center of the circle.

Code example

Add the following using statements:

Use this code to get a random point in a unit circle:

Introduction

Returns the shortest distance to rotate from the first argument angle to the second argument angle. All values are in radians. The absolute value of the return value will never be greater than PI (half of a circle). If the value is positive then the shortest distance is achieved by rotating counterclockwise. If the value is negative then the shortest distance is achieved by rotating clockwise.

Turning towards angle

The AngleToAngle method can be used to turn one object toward a direction. The following code creates a Sprite which will turn toward the point where the cursor is located. Add the following using statements:

Add the following at class scope:

Add the following to Initialize after initializing FlatRedBall:

Add the following to Update:

Range Checks

StartingValue | ReturnValue |
---|---|

The AngleToAngle can be used to check if one object is within an angle range. For example, you may be making a stealth game where the player must avoid being seen by enemies. We'll assume that the enemies have a property called ViewAngle which represents the edge-to-edge angle that the enemies can see. We'll also assume that this code has calculated the angle from the enemy to the player. For information on how to calculate this, see .

0

0

PI

PI

-PI

1.5PI

2PI

0

3 PI

PI

Introduction

The RoundFloat method can be used to round a float to the nearest non-whole argument value (called multipleOf). The following shows the result of using RoundFloat with multipleOf = 1: RoundFloat(1.2f) -> 1 RoundFloat(3.8f) -> 4 RoundFloat(-8.3) -> -8 If your multipleOf = 2, you would see the following results: RoundFloat(1.2) -> 2 RoundFloat(3.8f) -> 4 RoundFloat(-8.3) -> -8 This method is very useful in tile-based games where your tiles are not centered on whole values.

Code Example

The following shows how to use the RoundFloat method: Add the following using statement:

Add the following code wherever you need to use RoundFloat:

Code Example: Using RoundFloat for even-spacing index

RoundFloat can be useful for identifying the index of an object based off of its position. For this example, consider a list of Buttons, stacked vertically. The first button appears at Y = 10, and each button is spaced 35 units away from the next. In this situation, you can convert any absolute Y value to the index of the nearest button as follows:

Code Example: Using RoundFloat to Adjust Camera Offsets

Due to rendering issues in DirectX 9, offsetting the camera by a small amount can correct visual artifacts. The following code shows how to adjust the camera so its position is always offset by .1 pixels (meaning, it X position might be 10.1, 11.1, 12.1, etc):

AngleToVector converts a Radian value and returns a Vector3 pointing in the direction of that direction. Since a value of 0 points to the right, then AngleToVector will return a Vector of (1,0,0). As the angle increases the returned Vector3 will rotate clockwise.

The value returned from AngleToVector will equal a PositionedObject's RotationMatrix.Right given the same RotationZ.

Introduction

RotatePointAroundPoint is a method that can easily rotate one point around another. This method uses the System.Math.Atan2 method to calculate angles. All functionality provided by this method can also be obtained through System.Math.Atan2.

Code Example

The following code creates a Sprite which rotates about the origin when the user holds down the space bar. Add the following at class scope:

Add the following to Initialize after initializing FlatRedBall:

Add the following to Update:

Introduction

Code Example

The following code will calculate where an object will be given the set values:

Code Example - Determining Stopping Distance

Entities (including entities using the Top-Down plugin or the Platformer plugin) can slow down using acceleration. Your game may require calculating the distance that the entity will take to slow down. The following example shows how to calculate the distance it will take to slow down from full speed using the Top-Down plugin.

The GetPositionAfterTime function will return the position of an object given a set start position, an initial velocity value, and a constant acceleration value (constant during the movement time). Note that this function does not consider an object's value, so if a PositionedObject has a non-zero Drag, the results of this function will not match actual behavior.

Introduction

The RoundToInt method can be used to round a float or double value to an integer value. The return value is rounded as an integer which allows for == checks without having to worry about loss of precision.

Code examples

RoundToInt can be used to convert floats to integers

It will do mathematical rounding as well: