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

Only have 4 directions #6

Open
ChopsKingsland opened this issue Dec 31, 2020 · 4 comments
Open

Only have 4 directions #6

ChopsKingsland opened this issue Dec 31, 2020 · 4 comments

Comments

@ChopsKingsland
Copy link

I would like to use this to control an RPi car, but I only need 4 directions (up, down, left & right). How do I make these mutually exclusive (eg It can't be up left)?

@cptx032
Copy link
Owner

cptx032 commented Jan 1, 2021

Hi ChopsKingsland!
Your project looks cool!
I think that the easy way to adapt the example to make it mutually exclusive is remove the ifs side by side, and rearrange them using else-ifs, for example:

Instead:

if ( joystick.up ) text += 'up ';
if ( joystick.down ) text += 'down ';
if ( joystick.left ) text += 'left ';
if ( joystick.right ) text += 'right';

You can do:

if (joystick.up)
	text = 'up'
else if (joystick.down)
	text = 'down'
else if (joystick.left)
	text = 'left'
else if (joystick.right)
	text = 'right'

Let me know if this this helps you :D

@ChopsKingsland
Copy link
Author

That works, apart from the way that I control the car sends a request to a URL, and I don't what to sending requests hundreds of times per second. Is there a way to only do this when the position of the joystick changes?

@cptx032
Copy link
Owner

cptx032 commented Jan 2, 2021

A simplistic approach is to create a variable to store the last direction, something like:

function do_my_request(dir) {
    console.log("REQUEST", dir)
}
var direction = null
window.addEventListener("load", () => {
    let joystick = new JoyStick({
        radius: 80,
        x: window.innerWidth / 2,
        y: window.innerHeight /2,
        inner_radius: 70
    })
    
    function run_all_time() {
        if (joystick.up) {
            if (direction != 'up')
                do_my_request('up')
            direction = 'up'
        }
        else if (joystick.down) {
            if (direction != 'down')
                do_my_request('down')
            direction = 'down'
        }
        else if (joystick.left) {
            if (direction != 'left')
                do_my_request('left')
            direction = 'left'
        }
        else if (joystick.right) {
            if (direction != 'right')
                do_my_request('right')
            direction = 'right'
        }
        
        requestAnimationFrame(run_all_time)
    }
    run_all_time()
})

Maybe this works for you

@cptx032
Copy link
Owner

cptx032 commented Jan 2, 2021

The last example have some glitches when passing through the angles. You can give an look in the first example of https://yoannmoi.net/nipplejs/#demo too. They have an attribute called "angle", that gives you what you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants