How could we rotate vehicle back straight when dealing with touch inputs?

https://developer.playcanvas.com/en/tutorials/vehicle-physics/

Please help me program the vehicle used in the above tutorial to rotate back straight after when the player is done swerving left or right and is holding the touch on the screen, i.e when swipeDelta = 0;

Here is my code. I have implemented touch movements.

VehicleControls.prototype.update = function (dt) {

    var targetVehicle = this.targetVehicle ? this.targetVehicle : this.entity;

    if (targetVehicle && this.canPlay) 
    {
        var steering = 0;
        var throttle = 0;

        if(this.touching)
        {
            this.upButtonPressed = true;

            let swipeDelta = 0;
            
            if (this.touching) 
            {
                swipeDelta = this.currentPosition.x - this.startPosition.x;

                if(swipeDelta == 0)
                {
                    this.leftButtonPressed = false;
                    this.rightButtonPressed = false;
                }
                else if(swipeDelta < 0)
                {
                    this.leftButtonPressed = true;
                    this.rightButtonPressed = false;
                }
                else if(swipeDelta > 0)
                {
                    this.leftButtonPressed = false;
                    this.rightButtonPressed = true;
                }

                this.startPosition.copy(this.currentPosition);
            }
        }
        else
        {
            this.upButtonPressed = false;
        }

        if (this.leftButtonPressed || this.leftKeyPressed) steering++;
        if (this.rightButtonPressed || this.rightKeyPressed) steering--;
        if (this.upButtonPressed || this.upKeyPressed) throttle++;
        if (this.downButtonPressed || this.downKeyPressed) throttle--;

        targetVehicle.script.vehicle.fire('vehicle:controls', steering, throttle);
    }

Hi @yash_mehrotra,

Can you share an example of the vehicle project with your code added to give it a try?

Here is the link.

https://playcanvas.com/project/1021455/overview/demo

Just confirm, this isn’t purely related with touch inputs right?

Giving your game a try, even with keyboard input the car doesn’t rotate back straight after I stop turning it. Is that correct?

Yes exactly. but I am more focused on touch controls.

Here is a quick try based on your code:

        if (this.leftButtonPressed || this.leftKeyPressed) steering++;
        if (this.rightButtonPressed || this.rightKeyPressed) steering--;
        if (this.upButtonPressed || this.upKeyPressed) throttle++;
        if (this.downButtonPressed || this.downKeyPressed) throttle--;

        // --- if the vehicle isn't turning, steer to get it back on truck
        if (!this.leftButtonPressed && !this.rightButtonPressed && !this.leftKeyPressed && !this.rightKeyPressed) {

            const forward = targetVehicle.forward;
            const dot = forward.dot(pc.Vec3.FORWARD);

            if (dot < 0.999) { // --- check if we are looking left/right

                const isLeft = forward.x < 0;
                steering += isLeft ? -1 : 1; // --- apply auto steering until we look forward again
            }
        }

        targetVehicle.script.vehicle.fire('vehicle:controls', steering, throttle);

1 Like

This works well. Thank you @Leonidas .

1 Like