# Useful expressions and formulas

## Offsetting a position by angle and distance

```
x = x + cos(angle ) * distance
y = y + sin(angle ) * distance
```

This can be used to cast rays or calculate movement vectors and many more

## Framerate independent lerp

```
x = lerp(self.X, target.X, 1 - speed ^ dt)
```

speed can be a number from 0-1, the closer to 0 the faster it moves to the target

## Remapping a range to a different range&#x20;

```
lerp(start1, stop1, unlerp(start2, stop2, value))
```

the returned range will be between start1 and stop1\
If the value goes over or under the start2, stop2 range the returned value will still continue to grow at the scale of start1, start2. \
If you want to avoid that you can wrap it with clamp(theFormulaAbove, start1, stop2)

*source: XHXIAIEIN*

## Wrapping around a number from 0 to a maximum

```
value = (number % maximum + maximum) % maximum
```

the number wraps around at less than 0 and at greater or equal maxOptions

*source: hcatarrunas*

## Displaying time

```
days = zeropad(floor(TimeInSeconds / 84600), 2)

hours = zeropad(floor(TimeInSeconds / 3600), 2)

minutes = zeropad(floor(TimeInSeconds / 60), 2)

seconds = zeropad(int((TimeInSeconds) % 60), 2)

milliseconds = mid(tokenat(str(TimeInSeconds), 1, "."), 0, 3)
```

alternatively you can use the Date objects expressions to do this e.g. Date.GetMinutes()\
(the Date object expects Milliseconds and does not zeropad)

*source: Jobel*

## Snapping a position to a grid

Top left

```
x = floor(x / gridSize) * gridSize
y = floor(y / gridSize) * gridSize
```

Center

```
x = floor(x / gridSize) * gridSize + gridsize / 2
y = floor(y / gridSize) * gridSize + gridsize / 2
```

## Converting decibel to 0-100% volume

```
decibel = 33 * log10(percent / 100)

percent = 100 * 10 ^(decibel / 33)
```

*source: R0J0Hound*

## Performance information

{% code overflow="wrap" %}

```
fps&" FPS | "&roundToDp(cpuutilisation*100,1)&"% CPU | "&roundToDp(gpuutilisation*100,1)&"% GPU"
```

{% endcode %}

set a text objects text to this ever 0.1 seconds for some decently formatted performance information i.e. FPS, GPU and CPU usage


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fed-4.gitbook.io/c3-cheat-sheets/event-sheet/useful-expressions-and-formulas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
