flutter_control_pad icon indicating copy to clipboard operation
flutter_control_pad copied to clipboard

Joystick x and y

Open glennmichaelmejias opened this issue 4 years ago • 3 comments

How to get the x and y of the joystick? there are only two results on directionchanged.

glennmichaelmejias avatar Aug 14 '20 13:08 glennmichaelmejias

Right now you get angle and distance from the center. You need to do some math to reverse it back to x,y. Or some PR is welcome that would allow to choose a mode (to return either that or coordinates)

artur-ios-dev avatar Aug 15 '20 09:08 artur-ios-dev

I don't know if you're still interested in the solution or if you figured it out yourself, but the math to get x and y is as follows (r is the radius and a the angle):

x = r * cos(a)
y = r * sin(a)

chrismit3s avatar Jan 01 '21 23:01 chrismit3s

To add to @chrismit3s 's answer, you would first need to convert the angle given by the JoystickView widget into radians, as the cos(x) and sin(x) functions from the dart:math library only accept angles in radians.

Here would be the code to implement this:

import 'dart:math';
import 'package:control_pad/views/circle_view.dart';
...
JoystickView(
  onDirectionChanged: (double degrees, double distanceFromCenter) {
      double radians = degrees * pi / 180;
      double x = cos(radians) * distanceFromCenter;
      double y = sin(radians) * distanceFromCenter;
    }
)

ramuno4 avatar Feb 12 '21 11:02 ramuno4