• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
phirekid718

Vitrual touchscreen joystick

6 posts in this topic

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
0

Share this post


Link to post
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
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.
2

Share this post


Link to post
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.

0

Share this post


Link to post
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.

2

Share this post


Link to post
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.

0

Share this post


Link to post
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
0

Share this post


Link to post
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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0