Skip to content

Commit

Permalink
Fix joystick goes out of circle, do math
Browse files Browse the repository at this point in the history
  • Loading branch information
neursh committed Nov 24, 2024
1 parent c5161aa commit 2b88562
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions lib/screens/interact.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:vibration/vibration.dart';
import '../client_provider.dart';
Expand Down Expand Up @@ -255,17 +256,6 @@ class _JoyStickCustomState extends State<JoyStickCustom> {
Offset? beginPan;
bool _isOnCooldown = false;

double clamp(double value, double clampMin, double clampMax) {
if (value <= clampMin) {
return clampMin;
}
if (value >= clampMax) {
return clampMax;
}

return value;
}

@override
Widget build(BuildContext context) {
Size screen = MediaQuery.of(context).size;
Expand All @@ -282,8 +272,12 @@ class _JoyStickCustomState extends State<JoyStickCustom> {
double currentY = (details.localPosition.dy - beginPan!.dy) /
(screen.height / (widget.divisionFactor * 2));

currentX = clamp(currentX, -1, 1);
currentY = clamp(currentY, -1, 1);
double radius = sqrt(pow(currentX, 2) + pow(currentY, 2));

if (radius > 1) {
currentX /= radius;
currentY /= radius;
}

clientProvider.sendJoystick(widget.position,
(currentX * 32767).floor(), (-currentY * 32767).floor());
Expand Down

0 comments on commit 2b88562

Please sign in to comment.