• 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.
  • entries
    94
  • comments
    271
  • views
    150219

Simulating UIScrollView in Cocos2D

Sign in to follow this  
Followers 0
evanofsky

604 views

[color=rgb(51,51,51)][font=Georgia]

I wrote this article for the Sidebolt company blog. Reposting it here for your reading pleasure!

[/font][/color]
[color=rgb(51,51,51)][font=Georgia]

We publish our games on both Mac and iOS. Since UIKit is not available on Mac, we have to build all of our UI by hand in Cocos2D. One of the hardest parts to get right was emulating the UIScrollView bouncy scroll formula. Here it is in action:

[/font][/color]

https://vimeo.com/80289079
[color=rgb(51,51,51)][font=Georgia]

There are tons of implementations available in nearly every language and framework, but few of them nail all the important properties of the UIKit scroll:

[/font][/color]
[color=rgb(51,51,51)][font=Georgia]

When the user is touching the screen: the page should be glued to their finger. Easy enough.

[/font][/color]
[color=rgb(51,51,51)][font=Georgia]

After the user flicks the screen and lets go: the page should "glide", decelerating slowly until it comes to a stop. Most implementations get this half-right because it's also pretty easy: just multiply the velocity by a decay factor (we use 0.95) every frame. The real pitfall lies in calculating the velocity at the moment the user lets go of the page. Here's the naive implementation:

[/font][/color]-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event{ _lastFrameTouch = [[CCDirector sharedDirector] convertTouchToGL:touch]; _isTouching = true;}-(BOOL) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent*)event{ CGPoint currentTouch = [[CCDirector sharedDirector] convertTouchToGL:touch]; page.position = ccpAdd(page.position, ccp(0, currentTouch.y - _lastFrameTouch.y)); _lastFrameTouch = currentTouch;}-(BOOL) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent*)event{ CGPoint currentTouch = [[CCDirector sharedDirector] convertTouchToGL:touch]; _velocity = currentTouch.y - _lastFrameTouch.y; _isTouching = false;}-(void) update:(ccTime)dt{ if (!_isTouching) { _velocity *= 0.95f; _page.position = ccpAdd(_page.position, ccp(0, _velocity)); }}
[color=rgb(51,51,51)][font=Georgia]

This works most of the time, but you may find that occasionally the page flies off at warp speed after a very light touch. The reason is that while the user is removing their finger from the screen, their fingertrip is actually changing shape. The center of that shape may move very quickly at the exact instant they release their finger, even if the finger as a whole has only moved straight away from the screen. The solution is not to calculate the velocity when the touch is ended, but to filter the velocity continually like so:

[/font][/color]-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event{ _lastFrameTouch = [[CCDirector sharedDirector] convertTouchToGL:touch]; _isTouching = true;}-(BOOL) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent*)event{ CGPoint currentTouch = [[CCDirector sharedDirector] convertTouchToGL:touch]; _unfilteredVelocity = currentTouch.y - _lastFrameTouch.y; _page.position = ccpAdd(_page.position, ccp(0, _unfilteredVelocity)); _lastFrameTouch = currentTouch;}-(BOOL) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent*)event{ _isTouching = false;}-(void) update:(ccTime)dt{ if (_isTouching) { const float kFilterAmount = 0.25f; _velocity = (_velocity * kFilterAmount) + (_unfilteredVelocity * (1.0f - kFilterAmount)); _unfilteredVelocity = 0.0f; } else { _velocity *= 0.95f; _page.position = ccpAdd(_page.position, ccp(0, _velocity)); }}
[color=rgb(51,51,51)][font=Georgia]

When the page is gliding and it hits the bottom: this is where some implementations start to break down. The page should go past the edge and elastically snap back. Many implementations just stop the page immediately, or worse, bounce off the bottom inelastically.

[/font][/color]-(void) update:(ccTime)dt{ if (_isTouching) { const float kFilterAmount = 0.25f; _velocity = (_velocity * kFilterAmount) + (_unfilteredVelocity * (1.0f - kFilterAmount)); _unfilteredVelocity = 0.0f; } else { const float kScrollSnapMaxSpeed = 17.0f; const float kScrollSnapMaxAcceleration = 2.2f; const float kScrollSnapBackRatio = 0.1f; bool decay = true; // Calculate screen bounds and page bounds CGSize size = _page.contentSize; size.width *= _page.scaleX; size.height *= _page.scaleY; CGPoint anchor = _page.anchorPoint; CGPoint pos = _page.position; CGPoint min = ccp(pos.x - (size.width * anchor.x), pos.y - (size.height * anchor.y)); CGPoint max = ccp(pos.x + (size.width * (1.0f - anchor.x)), pos.y + (size.height * (1.0f - anchor.y))); CCNode* p = _page.parent; CGSize parentSize = p ? p.contentSize : [CCDirector sharedDirector].winSize; CGRect parentRect = CGRectZero; parentRect.size.width = MIN(size.width, parentSize.width); parentRect.size.height = MIN(size.height, parentSize.height); parentRect.origin.x = 0; parentRect.origin.y = parentSize.height - parentRect.size.height; if (min.y > parentRect.origin.y) // We want negative velocity { decay = false; _velocity = MIN(_velocity, kScrollSnapMaxSpeed); _velocity += MAX(((min.y - parentRect.origin.y) * -kScrollSnapBackRatio) - _velocity, -kScrollSnapMaxAcceleration); } if (max.y < parentRect.origin.y + parentRect.size.height) // We want positive velocity { decay = false; _velocity = MAX(_velocity, -kScrollSnapMaxSpeed); _velocity += MIN(((parentRect.origin.y + parentRect.size.height - max.y) * kScrollSnapBackRatio) - _velocity, kScrollSnapMaxAcceleration); } if (decay) _velocity *= 0.95f; _page.position = ccpAdd(_page.position, ccp(0, _velocity)); }}
[color=rgb(51,51,51)][font=Georgia]

When the user has their finger on the screen and tries to scroll past the edge (overscroll): a lot of implementations keep the page glued to the user's finger when this happens. It's easy to miss, but UIKit actually slows the scroll down to about 40% of the normal speed.

[/font][/color]-(BOOL) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent*)event{ currentTouch = [[CCDirector sharedDirector] convertTouchToGL:touch]; _unfilteredVelocity = currentTouch.y - _lastFrameTouch.y; const float kOverscrollRatio = 0.4f; // calculate min, max, and parentSize if (min.y > 0.0f) _unfilteredVelocity *= (1.0f - (min.y / parentSize.height)) * kOverscrollRatio; if (max.y < parentSize.height) _unfilteredVelocity *= (1.0f - ((parentSize.height - max.y) / parentSize.height)) * kOverscrollRatio; page.position = ccpAdd(page.position, ccp(0, _unfilteredVelocity)); _lastFrameTouch = currentTouch;}
[color=rgb(51,51,51)][font=Georgia]

And that's it! The results speak for themselves. Happy scrolling!

[/font][/color]

0
Sign in to follow this  
Followers 0


0 Comments


There are no comments to display.

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