Jump to content

  • Log In with Google      Sign In   
  • Create Account


Vitrual touchscreen joystick


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 ElDuro   Members   -  Reputation: 507

Like
0Likes
Like

Posted 01 July 2013 - 08:50 PM

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, 01 July 2013 - 08:55 PM.


Sponsor:

#2 DonaldHays   Members   -  Reputation: 192

Like
2Likes
Like

Posted 02 July 2013 - 08:53 AM

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
float radiusJoystick = 100;
 
// 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
if(magnitudeOfTouchRelativeToJoystickCenter > radiusJoystick) {
    // 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.

#3 ElDuro   Members   -  Reputation: 507

Like
0Likes
Like

Posted 02 July 2013 - 04:13 PM

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.



#4 DonaldHays   Members   -  Reputation: 192

Like
2Likes
Like

Posted 02 July 2013 - 05:09 PM

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.



#5 ElDuro   Members   -  Reputation: 507

Like
0Likes
Like

Posted 02 July 2013 - 07:35 PM

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



#6 JeffCarp   Members   -  Reputation: 278

Like
0Likes
Like

Posted 02 July 2013 - 11:21 PM

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, 02 July 2013 - 11:39 PM.


#7 ElDuro   Members   -  Reputation: 507

Like
0Likes
Like

Posted 07 August 2013 - 03:05 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS