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

Unify digital steering #460

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .changes/unreleased/Changed-20240826-080222.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Changed
body: Steering is now smoother, especially on Android.
time: 2024-08-26T08:02:22.121774707+02:00
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Aurélien Gâteau <[email protected]>
* Copyright 2024 Compl Yue
*
* This file is part of Pixel Wheels.
*
Expand All @@ -19,18 +19,21 @@
package com.agateau.pixelwheels.gameinput;

import com.agateau.pixelwheels.GamePlay;
import com.agateau.pixelwheels.racescreen.HudButton;
import com.badlogic.gdx.math.MathUtils;

class TouchInputUtils {
static float applyDirectionInput(HudButton leftButton, HudButton rightButton, float direction) {
if (leftButton.isPressed()) {
direction += GamePlay.instance.steeringStep;
} else if (rightButton.isPressed()) {
direction -= GamePlay.instance.steeringStep;
public class DigitalSteering {
private float mRawDirection = 0;

public float steer(boolean left, boolean right) {
if (left == right) {
// Either both left & right are pressed or none of them are
mRawDirection *= 0.4;
} else if (left) {
mRawDirection += GamePlay.instance.steeringStep;
} else {
direction = 0;
mRawDirection -= GamePlay.instance.steeringStep;
}
return MathUtils.clamp(direction, -1, 1);
mRawDirection = MathUtils.clamp(mRawDirection, -1, 1);
return mRawDirection * mRawDirection * Math.signum(mRawDirection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
package com.agateau.pixelwheels.gameinput;

import com.agateau.pixelwheels.Assets;
import com.agateau.pixelwheels.GamePlay;
import com.agateau.pixelwheels.bonus.Bonus;
import com.agateau.pixelwheels.debug.DebugStringMap;
import com.agateau.pixelwheels.racescreen.Hud;
import com.agateau.ui.InputMapper;
import com.agateau.ui.VirtualKey;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.math.MathUtils;

/** Base class for InputMapper-based GameInputHandlers */
public abstract class InputMapperInputHandler implements GameInputHandler {
private final InputMapper mInputMapper;
private final GameInput mInput = new GameInput();
private final DigitalSteering mSteer = new DigitalSteering();

InputMapperInputHandler(InputMapper inputMapper) {
mInputMapper = inputMapper;
Expand All @@ -42,14 +42,11 @@ public GameInput getGameInput() {
mInputMapper.isKeyPressed(VirtualKey.DOWN)
|| mInputMapper.isKeyPressed(VirtualKey.BACK);
mInput.accelerating = !mInput.braking;
if (mInputMapper.isKeyPressed(VirtualKey.LEFT)) {
mInput.direction += GamePlay.instance.steeringStep;
} else if (mInputMapper.isKeyPressed(VirtualKey.RIGHT)) {
mInput.direction -= GamePlay.instance.steeringStep;
} else {
mInput.direction *= 0.4;
}
mInput.direction = MathUtils.clamp(mInput.direction, -1, 1);
mInput.direction =
mSteer.steer(
mInputMapper.isKeyPressed(VirtualKey.LEFT),
mInputMapper.isKeyPressed(VirtualKey.RIGHT));
DebugStringMap.putPercent("direction", (float) (Math.abs(mInput.direction)));
mInput.triggeringBonus = mInputMapper.isKeyPressed(VirtualKey.TRIGGER);

return mInput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
/** Handle input using pie buttons in the bottom left and right corners */
public class PieTouchInputHandler implements GameInputHandler {
private final GameInput mInput = new GameInput();
private final DigitalSteering mSteer = new DigitalSteering();
private PieButton mLeftButton, mRightButton, mBrakeButton, mBonusButton;

public static class Factory implements GameInputHandlerFactory {
Expand Down Expand Up @@ -61,8 +62,7 @@ public Array<GameInputHandler> getAllHandlers() {
public GameInput getGameInput() {
mInput.braking = mBrakeButton.isPressed();
mInput.accelerating = !mInput.braking;
mInput.direction =
TouchInputUtils.applyDirectionInput(mLeftButton, mRightButton, mInput.direction);
mInput.direction = mSteer.steer(mLeftButton.isPressed(), mRightButton.isPressed());
mInput.triggeringBonus = mBonusButton.isPressed();
return mInput;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
/** Handle input using buttons on the sides */
public class SidesTouchInputHandler implements GameInputHandler {
private final GameInput mInput = new GameInput();
private final DigitalSteering mSteer = new DigitalSteering();
private HudButton mLeftButton, mRightButton, mBonusButton;

public static class Factory implements GameInputHandlerFactory {
Expand Down Expand Up @@ -61,11 +62,7 @@ public Array<GameInputHandler> getAllHandlers() {
public GameInput getGameInput() {
mInput.braking = isBraking();
mInput.accelerating = !mInput.braking;
if (!mInput.braking) {
mInput.direction =
TouchInputUtils.applyDirectionInput(
mLeftButton, mRightButton, mInput.direction);
}
mInput.direction = mSteer.steer(mLeftButton.isPressed(), mRightButton.isPressed());
mInput.triggeringBonus = mBonusButton.isPressed();
return mInput;
}
Expand Down
Loading