# Vitrual touchscreen joystick

This topic is 1629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to implement a joystick in cocos2d-x. The idea is to allow the user to slide a circle within a larger circle so the direction of the slide controls the direction the character moves. If there is a better way to do this please let me know but so far my code looks something like this:

if(joystick.x < xMin)
joystick.x = xMin;
if(joystick.x > xMax)
joystick.x = xMax;
if(joystick.x < yMin)
joystick.x = yMin;
if(joystick.x > yMax)
joystick.x = yMax;

The joystick in this code represents a circular sprite that the user can slide around to control the character. But as you can see, the code confines the sprite so that it stays within a box formed by the sides xMin,xMax,yMin and yMax. I don't want that. I want to confine the sprite within a larger circle. How can I do this? Please and thank you.

Edited by phirekid718

##### Share on other sites
You can get the behavior you want using basic vector math. I'll be using pseudocode in this reply because I'm not familiar with what vector types Cocos2d-x has built-in.

The idea is that your joystick can be represented as a circle, and the touch as a point that's offset from the center of that circle. If the touch is further away from the center of the circle than the radius of the circle, then clamp the touch to the edge of the circle.

// First, you have the vector that represents the center of the joystick
vec2 centerJoystick = { centerXCoordinate, centerYCoordinate };

// Then, you have the radius of the joystick

// Then you have the current touch location
vec2 touchLocation = { touchLocationX, touchLocationY };

// Then you compute the offset of the touch location from the center of the joystick
vec2 touchRelativeToJoystickCenter = Vector2Subtract(touchLocation, centerJoystick);

// Then you take the magnitude of the relative touch offset vector. That represents the distance of the touch from the center of the joystick
float magnitudeOfTouchRelativeToJoystickCenter = Vector2Magnitude(touchRelativeToJoystickCenter);

// If that magnitude is greater than the radius of the joystick, then the touch is outside the joystick, so we need to clamp it
// We clamp by multiplying the relative touch offset vector by a scalar. The vector will be clamped when its magnitude is equal to the radius of the joystick. So we want to multiply the vector by a scalar that reduced its magnitude to that of the joystick
float scalar = radiusJoystick / magnitudeOfTouchRelativeToJoystickCenter;
touchRelativeToJoystickCenter = Vector2Scale(touchRelativeToJoystickCenter, scalar);
}


And that's about all there is to it. After this code is done, the magnitude of touchRelativeToJoystickCenter will not be greater than the radius of the circle.

##### Share on other sites

Thanks. It was the clamping that troubled me. Is there a way to do it without calculating square roots? I have read that it was expensive. But so far this works fine.

##### Share on other sites

Square roots are relatively expensive, yes, and so one might want to be reluctant to use them deep in a tight loop that'll be iterated tens of thousands of time per frame (hundreds of thousands? Millions? I dunno the order of magnitude, computers are really fast these days, even phones are quite fast). A square root here and there in UI-level code (like clamping virtual joysticks) has, in my experience, an effectively-zero cost.

If you really want to avoid the square root, though, I think the math would still work out if you squared the joystick radius, and then didn't do the square root stage of computing the vector magnitude (so you did magnitude = v.x * v.x + v.y * v.y). I think that would work, but I haven't tested to see. Honestly I just do the square roots without worrying about it in similar code of mine.

##### Share on other sites

Thanks. I was going to leave it as is. I just wanted to see if there was some programming trick that could be used.

##### Share on other sites

I do not know if recent math libraries implement a function similar to what is used in the "Fast inverse square root" method (wikipedia article with code & history), but perhaps this is some benefit to you if you ever have performance issues with the sqrt function. No idea how this measures in performance to anything, though.

Cheers!

Edited by JeffCarp

##### Share on other sites

Are there any 2d games on touch screen devices with a good joystick (or dpad) implementation? I haven't found a good reference for this, so I have researching physical joysticks and what this comes down to is called a "restrictor gate". (see link under "8-way")

http://www.slagcoin.com/joystick/restrictors.html

Now I want to test the same concept, except within an octagon.

So to clamp a point within a rectangle:

if(point.x < throwRect.getMinX())
point.x = throwRect.getMinX();
if(point.x > throwRect.getMaxX())
point.x = throwRect.getMaxX();
if(point.y < throwRect.getMinY())
point.y = throwRect.getMinY();
if(point.y > throwRect.getMaxY())
point.y = throwRect.getMaxY();

To clamp a point within a circle:

if(distance > throwDistance)
{
point.x = stickCenter.x + cos(angle) * throwDistance;
point.y = stickCenter.y + sin(angle) * throwDistance;
}

How can I clamp a point within an octagon? Thank you