# Calculating target speed

**URL:** <https://forum.kirupa.com/t/calculating-target-speed/214165>\
**Category:** Uncategorized\
**Created:** [January 26, 2007, 1:06am UTC](https://forum.kirupa.com/t/calculating-target-speed/214165 "2007-01-26T01:06:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [January 26, 2007, 1:06am UTC](https://forum.kirupa.com/t/calculating-target-speed/214165/1 "2007-01-26T01:06:07Z")

</div>

i have some code that works out a target speed based on how far right of the screen ur mouse is as follows:

```auto

if(this._xmouse > value1 && this._xmouse < value2){
//set targetSpeed depending on how much greater than value1 it is
targetSpeed = this._xmouse-value1;
}

```

is there an elegant mathematical way to calculate the targetSpeed value so that - it increases as xmouse gets greater, \*\*\*until \*\*\*xmouse is at the value that is the midpoint of value1 and value2, from when on, targetSpeed \*\*\*decreases \*\*\*as \_xmouse gets closer to value2

is that making sense?

so if value1 = 500, and value2 = 600, then as xmouse goes over 500, targetSpeed will increase 0, 1, 2, 3, 4 - up to 50. then as xmouse goes over 550 targetSpeed will decrease until xmouse is at 600, by then targetSpeed will be back to 0.

i can do it - but i think my ways are messy so im wondering if any maths/AS genius can suggest a simple method.
