Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dynamic joystick to follow correctly #4

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ pub fn update_joystick<S: Hash + Sync + Send + Clone + Default + Reflect + 'stat
// Dragging
TouchPhase::Moved => {
if is_some_and(knob.id_drag, |i| i == *id) {
knob.current_pos = *pos;
let half = knob.interactable_zone_rect.half_size();
if node.behaviour == VirtualJoystickType::Dynamic {
knob.base_pos = *pos;
let to_knob = knob.current_pos - knob.start_pos;
let distance_to_knob = to_knob.length();
if distance_to_knob > half.x {
let excess_distance = distance_to_knob - half.x;
knob.start_pos += to_knob.normalize() * excess_distance;
}
Comment on lines +60 to +65
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's becoming "a lot" of code duplicated so maybe creating a function could help.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a great idea, I was thinking of doing something similar, do you think you can do it in another PR, as soon as I accept this one?

}
knob.current_pos = *pos;
// knob.delta = (knob.start_pos - knob.current_pos).normalize_or_zero();
let half = knob.interactable_zone_rect.half_size();
let d = (knob.start_pos - knob.current_pos) / half;
knob.delta = Vec2::new(
d.x.signum() * d.x.abs().min(1.),
Expand Down Expand Up @@ -152,11 +158,17 @@ pub fn update_joystick_by_mouse<S: Hash + Sync + Send + Clone + Default + Reflec
if mouse_button_input.pressed(MouseButton::Left)
&& is_some_and(knob.id_drag, |i| i == 0)
{
knob.current_pos = *pos;
let half = knob.interactable_zone_rect.half_size();
if node.behaviour == VirtualJoystickType::Dynamic {
knob.base_pos = *pos;
let to_knob = knob.current_pos - knob.start_pos;
let distance_to_knob = to_knob.length();
if distance_to_knob > half.x {
let excess_distance = distance_to_knob - half.x;
knob.start_pos += to_knob.normalize() * excess_distance;
}
}
knob.current_pos = *pos;
let half = knob.interactable_zone_rect.half_size();
let d = (knob.start_pos - knob.current_pos) / half;
knob.delta = Vec2::new(
d.x.signum() * d.x.abs().min(1.),
Expand Down