Having trouble with the vehicle control class

hey gents! im playing around with the vehicle control class to control the physics vehicle for my kart racing game im creating. so here’s the problem im running into, i made it so that when your holding W it slowly accelerates and then de-accelerates when you let go, im trying to make the steering do something similar while holding A or D i want it to turn the wheels to the respective directions gradually and when its let go return back to the centre (forward) but with the code i currently have the wheels just spas out and stop responding,

here’s the class that control’s the vehicle:

package com.bcart.controllers;

import com.bcart.screens.HudSource;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.control.AbstractControl;

public class PlayerController extends AbstractControl implements AnalogListener, ActionListener, HudSource {

    private Node vehicleNode;
    private InputManager inputManager;
    private VehicleControl vehicleControl;
    private int boostCharge = 0;
    private int acceleration;
    private int turningRight;
    private int turningLeft;
    private boolean accelerating;
    private boolean stop;
    private boolean right;
    private boolean left;
    private int aValue = 10;
    private int tValue = 1;
    private int MAX_ACCELERATION = 100000;
    private int MAX_RIGHT = 100;
    private int MAX_LEFT = 100;

    public PlayerController(InputManager inputManager, Node vehicleNode) {
        this.inputManager = inputManager;
        this.vehicleNode = vehicleNode;
        this.vehicleControl = vehicleNode.getControl(VehicleControl.class);
        setUpKeys(inputManager);
    }

    private void setUpKeys(InputManager inputManager) {
        inputManager.addMapping("ACCELERATE", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("TURN_LEFT", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("BREAK", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("TURN_RIGHT", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("HANDBREAK", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addMapping("BOOST", new KeyTrigger(KeyInput.KEY_LSHIFT));
        inputManager.addMapping("TEMP", new KeyTrigger(KeyInput.KEY_L));
        //inputManager.addMapping("SHOOT", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        inputManager.addListener(this,
                "ACCELERATE", "TURN_LEFT", "REVERSE", "TURN_RIGHT",
                "HANDBREAK", "BOOST", "TEMP");
        inputManager.setCursorVisible(false);
    }

    @Override
    protected void controlUpdate(float tpf) {
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    @Override
    public void onAnalog(String car, float value, float tpf) {

        switch (car) {
            case "ACCELERATE":
                accelerating = true;
                if (accelerating) {
                    stop = true;
                    if (acceleration <= MAX_ACCELERATION) {
                        vehicleControl.accelerate(acceleration += aValue);
                    }
                } else {
                    stop = true;
                    accelerating = false;
                }
                break;
            case "TURN_LEFT":
                left = true;
                if (left) {
                    if (turningLeft <= MAX_LEFT) {
                        vehicleControl.steer(turningLeft += tValue);
                    }
                } else {
                    left = false;
                    vehicleControl.steer(turningLeft -= tValue);
                }
                //vehicleControl.steer(-100);
                break;
            case "REVERSE":
                break;
            case "TURN_RIGHT":
                right = true;
                if (right) {
                    if (turningRight <= MAX_RIGHT) {
                        vehicleControl.steer(turningRight += tValue);
                    }
                } else {
                    right = false;
                    vehicleControl.steer(turningRight -= tValue);
                }
                //vehicleControl.steer(100);
                break;
            case "HANDBREAK":
                break;
            default:
                vehicleControl.steer(0);
        }
    }

    @Override
    public void onAction(String boost, boolean isPressed, float tpf) {
        if (isPressed) {
            switch (boost) {
                case "BOOST":
                    if (boostCharge > 0) {
                        --boostCharge;
                    }
                    break;
                case "TEMP":
                    if (boostCharge < 3) {
                        ++boostCharge;
                    }
                    break;
            }
        } else if (stop) {
            stop = true;
            vehicleControl.accelerate(0);
            acceleration = 0;
            vehicleControl.clearForces();
            System.out.println("stopping");
        }
    }

    @Override
    public int getSpeed() {
        return 1000;
    }

    @Override
    public int getCharge() {
        return boostCharge;
    }

    public Node getVehicleNode() {
        return vehicleNode;
    }
}

First, your left/right steering are going to compete with one another in really strange ways.

Second, in the same vein, your logic for limiting the max is a little strange because your values will always be allowed to go one tValue larger than max.

Third, I suspect that the values you are passing to steer() are truly giant in comparison to what it probably takes.

Probably, you want to have your own single turning value that you add/subtract to when turning left or right but otherwise tends to fall to 0.

…then you want to figure out what range steer() is expecting and scale or limit it to that range.

the steer method takes in a degree, max is 360 and least is 0 (0 being forward) so 100 either way would be an Obtuse Angle, also im not quite sure what you meant by my logic limiting the max turning factor, though this might just be my ignorance. ill have a crack at making the steering value a single variable and add/subtract to it instead of 2 separate ones.
nevertheless thanks for the reply ill update you shortly with my progress.

OKAY, SO.
I have made some progress with this.
now you can press the a key and when you let go it will set the wheels back to forward BUT, it only does it once and then it wont let you turn AT ALL after that(you can still accelerate). it still does not gradually turn to the left/right on keypress it instantly jumps to the value, i tried making it so the value was a float rather than an int but it stopped responding after that

here’s what i have now:

package com.bcart.controllers;

import com.bcart.screens.HudSource;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.control.AbstractControl;

public class PlayerController extends AbstractControl implements AnalogListener, ActionListener, HudSource {

    private Node vehicleNode;
    private InputManager inputManager;
    private VehicleControl vehicleControl;
    private int boostCharge = 0;
    private int acceleration;
    private float turnValue = 0;
    private boolean zeroing;
    private boolean accelerating;
    private boolean stop;
    private boolean right;
    private boolean left;
    private int aValue = 10;
    private int tValue = 1;
    private int MAX_ACCELERATION = 100000;
    private int MAX_RIGHT = 1;
    private int MAX_LEFT = -1;
    private boolean startZero;

    public PlayerController(InputManager inputManager, Node vehicleNode) {
        this.inputManager = inputManager;
        this.vehicleNode = vehicleNode;
        this.vehicleControl = vehicleNode.getControl(VehicleControl.class);
        setUpKeys(inputManager);
    }

    private void setUpKeys(InputManager inputManager) {
        inputManager.addMapping("ACCELERATE", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("TURN_LEFT", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("BREAK", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("TURN_RIGHT", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("HANDBREAK", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addMapping("BOOST", new KeyTrigger(KeyInput.KEY_LSHIFT));
        inputManager.addMapping("TEMP", new KeyTrigger(KeyInput.KEY_L));
        //inputManager.addMapping("SHOOT", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        inputManager.addListener(this,
                "ACCELERATE", "TURN_LEFT", "REVERSE", "TURN_RIGHT",
                "HANDBREAK", "BOOST", "TEMP");
        inputManager.setCursorVisible(false);
    }

    @Override
    protected void controlUpdate(float tpf) {
        if (startZero) {
//            if (turnValue < 0) {
//                turnValue++;
//            } else if (turnValue > 0) {
//                turnValue--;
            vehicleControl.steer(0);
        } else {
            startZero = false;
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    @Override
    public void onAnalog(String car, float value, float tpf) {

        switch (car) {
            case "ACCELERATE":
                accelerating = true;
                if (accelerating) {
                    if (acceleration <= MAX_ACCELERATION) {
                        vehicleControl.accelerate(acceleration += aValue);
                    }
                    stop = true;
                }
                break;
            case "TURN_LEFT":
                left = true;
                if (left) {
                    zeroing = false;
                    if (turnValue >= MAX_LEFT) {
                        vehicleControl.steer(turnValue--);
                    } else {
                        zeroing = true;
                    }
                }
                //vehicleControl.steer(-100);
                break;
            case "REVERSE":
                break;
            case "TURN_RIGHT":
                right = true;
                if (right) {
                    zeroing = false;
                    if (turnValue <= MAX_RIGHT) {
                        vehicleControl.steer(turnValue++);
                    } else {
                        zeroing = true;
                    }
                }
                //vehicleControl.steer(100);
                break;
            case "HANDBREAK":
                break;
            default:
                // vehicleControl.steer(0);
                //  turning = 0;
                right = false;
                left = false;
        }
    }

    @Override
    public void onAction(String boost, boolean isPressed, float tpf) {
        if (isPressed) {
            switch (boost) {
                case "BOOST":
                    if (boostCharge > 0) {
                        --boostCharge;
                    }
                    break;
                case "TEMP":
                    if (boostCharge < 3) {
                        ++boostCharge;
                    }
                    break;
            }
        } else if (stop) {
            vehicleControl.accelerate(0);
            acceleration = 0;
            vehicleControl.clearForces();
            System.out.println("stopping");
        } else if (zeroing) {
            startZero = true;
        } else {
            startZero = false;
        }
    }

    @Override
    public int getSpeed() {
        return 1000;
    }

    @Override
    public int getCharge() {
        return boostCharge;
    }

    public Node getVehicleNode() {
        return vehicleNode;
    }
}

You make things so complicated.

Pseudo code:

if( leftIsPressed ) {
    turn -= turnAccel * tpf;
} else if( rightIsPressed ) {
    turn += turnAccel * tpf;
} else if( turn > 0 ) {
    turn -= turnDecel * tpf;
} else if( turn < 0 ) {
    turn -= turnDecel * tpf;
}
vehicleControl.steer(turn);

…roughly speaking.

I used your pseudo code to try and get it working and with a little playing around with it i got it somewhat working, now it will gradually turn to the right/left but will not reset back to the centre when its let go. i tried debugging it to see if it was ever hitting the decel in the update and it wasnt, I tried to erect the issue with no avail. im going to keep at it though!

code update:

package com.bcart.controllers;

import com.bcart.screens.HudSource;
import com.jme3.bullet.control.VehicleControl;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.control.AbstractControl;

public class PlayerController extends AbstractControl implements AnalogListener, ActionListener, HudSource {

    private Node vehicleNode;
    private InputManager inputManager;
    private VehicleControl vehicleControl;
    private int boostCharge = 0;
    private int acceleration;
    private int aValue = 10;
    private int turnAccel = -1;
    private int turnDecel = 1;
    private int MAX_ACCELERATION = 100000;
    private boolean accelerating;
    private boolean stop;
    private boolean leftIsPressed;
    private boolean rightIsPressed;
    private float turn;
    private boolean turning;
    private int MAX_LEFT = 1;
    private int MAX_RIGHT = -1;

    public PlayerController(InputManager inputManager, Node vehicleNode) {
        this.inputManager = inputManager;
        this.vehicleNode = vehicleNode;
        this.vehicleControl = vehicleNode.getControl(VehicleControl.class);
        setUpKeys(inputManager);
    }

    private void setUpKeys(InputManager inputManager) {
        inputManager.addMapping("ACCELERATE", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("TURN_LEFT", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("BREAK", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("TURN_RIGHT", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("HANDBREAK", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addMapping("BOOST", new KeyTrigger(KeyInput.KEY_LSHIFT));
        inputManager.addMapping("TEMP", new KeyTrigger(KeyInput.KEY_L));
        //inputManager.addMapping("SHOOT", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        inputManager.addListener(this,
                "ACCELERATE", "TURN_LEFT", "REVERSE", "TURN_RIGHT",
                "HANDBREAK", "BOOST", "TEMP");
        inputManager.setCursorVisible(false);
    }

    @Override
    protected void controlUpdate(float tpf) {
        if (leftIsPressed) {
            if (turn < MAX_LEFT) {
                turn -= turnAccel * tpf;
            }
        } else if (rightIsPressed) {
            if (turn > MAX_RIGHT) {
                turn += turnAccel * tpf;
            }
        } //     if (turning = false) {
        else if (turn > 0) {
            turn -= turnDecel * tpf;
        } else if (turn < 0) {
            turn -= turnDecel * tpf;
        }
        // }
        vehicleControl.steer(turn);
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    @Override
    public void onAnalog(String car, float value, float tpf) {

        switch (car) {
            case "ACCELERATE":
                if (stop != true) {
                    if (acceleration <= MAX_ACCELERATION) {
                        vehicleControl.accelerate(acceleration += aValue);
                    }
                } else {
                    stop = false;
                }
                break;
            case "TURN_LEFT":
                turning = true;
                if (turning) {
                    leftIsPressed = true;
                } else {
                    leftIsPressed = false;
                }
                break;
            case "REVERSE":
                break;
            case "TURN_RIGHT":
                turning = true;
                if (turning) {
                    rightIsPressed = true;
                } else {
                    rightIsPressed = false;
                }
                break;
            case "HANDBREAK":
                break;
            default:
        }
    }

    @Override
    public void onAction(String car, boolean isPressed, float tpf) {
        if (isPressed) {
            switch (car) {
                case "BOOST":
                    if (boostCharge > 0) {
                        --boostCharge;
                    }
                    break;
                case "TEMP":
                    if (boostCharge < 3) {
                        ++boostCharge;
                    }
                    break;
            }
        } else {
            stop = true;
            System.out.println("stopping");
            vehicleControl.accelerate(0);
            vehicleControl.clearForces();
            acceleration = 0;

        }
    }

    @Override
    public int getSpeed() {
        return 1000;
    }

    @Override
    public int getCharge() {
        return boostCharge;
    }

    public Node getVehicleNode() {
        return vehicleNode;
    }
}

I think you mean that to be +=

Edit: my pseudo code had a typo… but some debugging should have shown that.

Just an update i have finished the steering.
it now will steer to the left and right gradually and snap back to the center when not being pressed.
thanks for your help @pspeed