artur-ios-dev / flutter_control_pad

A control pad with a virtual joystick and buttons.
https://pub.dev/packages/control_pad
GNU General Public License v3.0
40 stars 24 forks source link

Joystick x and y #30

Open glennmichaelmejias opened 4 years ago

glennmichaelmejias commented 4 years ago

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

artur-ios-dev commented 4 years ago

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)

chrismit3s commented 3 years ago

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)
ramuno4 commented 3 years ago

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;
    }
)