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.
The following shows how to use the RoundFloat method: Add the following using statement:
Add the following code wherever you need to use RoundFloat:
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:
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):