1
\$\begingroup\$

I got a joystick displayed in the image below with range -1 to 1. Green area is 80% of the total circle and yellow area is 20%.

I want to get corrected values for both areas. For example:

  1. If given point falls under yellow range I want to get actual input value.
  2. if given point is within green area how can I get maximum value in green area ie. 80% at same direction.

enter image description here

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

This is some pretty trivial linear algebra.

Vector2 SnapToMinimumLength(Vector2 input, float minimum) { // Most vector libraries will give you a method to do this, // but in case you're not using one and aren't familiar with // vector magnitude/normalization, I'll show all the steps. float squaredLength = input.x*input.x + input.y*input.y; if (squaredLength > 0 && squaredLength < minimum * minimum) { float scale = minimum / Mathf.Sqrt(squaredLength); input.x *= scale; input.y *= scale; } return input; } 

To snap to the outer edge of the green area at 80% of the circle radius, you'd use:

Vector2 snappedInput = SnapToMinimumLength(rawInput, 0.8); 

The one exception here is when the input is (0, 0). Then it has no direction, so this code skips trying to snap it anywhere and returns the input unchanged. You could decide to force a direction arbitrarily (like saying (0, 0)->(0, 0.8)) or cache the last non-zero direction and keep using that, or fall back on some different behaviour appropriate to your application.

\$\endgroup\$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.