• 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
theBong

canvas hit detection

8 posts in this topic

i want to detect clicks on canvas elements which are drawn using paths. so far i have think of to store elements path in javascript data structure and then check the cordinates of hits which matches the elements cordinates. i belive there is algorithm already for thins kind o cordinate search. rendering each of element path and checking the hits would be inefficient when elements number is larger. can anyone point on me that?
also any other pointers and feedbacks are welcome.
0

Share this post


Link to post
Share on other sites
color maping approach sounds nice and different. have not think it that way. i was thinking about doing it using paths. if it is possible for you to give any idea using paths, will be great. It is for edcuational purpose so there is alot of room for me for experiments and learn.

thanks for your comments.
0

Share this post


Link to post
Share on other sites
[b]bwhitling [/b]is right on the money.

I've implemented this technique in OpenGL. In the sources that I've read, this technique is called "[b]color picking[/b]" if that helps your google searches. What's great about this technique is that you can assign a picking index to each element you wish to be pickable (clickable). This index can be an unsigned 32bit integer that you generate by incrementing a counter every time you create a pickable object. When you go to draw the pickable object as a solid color, all you need to do is convert the 32bit integer into four 8bit color components (RGBA) to get the color you should draw. Then when you read back the color, just convert the four 8bit color components back into a 32bit integer and you have the picking index of the object that got picked (clicked).
0

Share this post


Link to post
Share on other sites
You want to use the Context2D's "isPointInPath" method. [url="http://digitalarts.bgsu.edu/faculty/bonniem/Spring11/artc4330_1/notes/notes26.html"]Here's a tutorial[/url].
0

Share this post


Link to post
Share on other sites
[quote name='Sirisian' timestamp='1348859929' post='4984831']
You want to use the Context2D's "isPointInPath" method. [url="http://digitalarts.bgsu.edu/faculty/bonniem/Spring11/artc4330_1/notes/notes26.html"]Here's a tutorial[/url].
[/quote]
yes, i know that function. if i have 10 elements, i have to draw 10 paths and check the cordinates comes between that or not using that function. what i am wanting is some way (algorithm) to get selective elements which is closer or tend to be closer of the hit cordinate without looping through all elements. Edited by theBong
0

Share this post


Link to post
Share on other sites
[quote]how will "color picking" work for layered element?[/quote]

There are two ways to handle layered elements, depending on whether you wish to pick only one element at a time or multiple:[list=1]
[*]The easier case is picking a single element. When you go to render out the elements as colors, only render out the elements you care to to pick. For instance, if you have a button element with a label element inside, chances are you don't care to pick the label element, only the button. So just render the button as a solid color based on its picking index and skip the label element. If you render out the colors using depth testing, then the topmost elements will take precedence when picking any point that has elements overlapping.
[*]The harder case is picking multiple elements, like if you want to implement rubberband selection. In that case, I've implemented a technique where I cycle thru the elements separately performing three operations for each of them.
[/list][list]
[*]Render out the element as a solid color based on its picking index.
[*]Query the pixel(s) I care to pick at for color (picking index) and depth.
[*]Clear the color and depth buffers before rendering the next element.
[/list]
[indent=1]The query will tell me if I picked anything. Usually, I choose the color white (pickingIndex == int32.max) to represent nothing was picked, but black (pickingIndex == 0) could work too if you want to start your picking indices at 1 instead of 0. If I get a hit, I can then use the queried depth value to order the elements if that matters at all.[/indent]

In both scenarios, you can limit your rendering to the a viewport that only encompasses the area you care to pick to lower the overhead of rendering (typically a 1x1 pixel area unless you're rubberband selecting or you want to buffer your picking precision). I do recommend however, to save the viewport optimization portion for after you've perfected your picking code, I've historically had to tweak things several times before picking was working properly, and it's hard to debug my picking logic when I'm only rendering out a single pixel =P.
0

Share this post


Link to post
Share on other sites
[quote name='theBong' timestamp='1348860612' post='4984833']
[quote name='Sirisian' timestamp='1348859929' post='4984831']
You want to use the Context2D's "isPointInPath" method. [url="http://digitalarts.bgsu.edu/faculty/bonniem/Spring11/artc4330_1/notes/notes26.html"]Here's a tutorial[/url].
[/quote]
yes, i know that function. if i have 10 elements, i have to draw 10 paths and check the cordinates comes between that or not using that function. what i am wanting is some way (algorithm) to get selective elements which is closer or tend to be closer of the hit cordinate without looping through all elements.
[/quote]
You don't have to draw 10 paths. [url="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-ispointinpath"]You can store the path objects for instance and then perform the test without ever drawing them[/url]. (One of the overload is (path, x, y)). So you'll want to use a path object to make this more efficient. Color picking is going to be slower and will still require you to loop through all objects. If you want to test less then you probably want to use a spatial partitioning system for the path objects. That is insert the paths into a grid and only check the mouse against objects in the cell the coordinate is inside of.

(Also, not sure if it's supported yet but you'll have addHitRegion for treating drawn paths like DOM elements). Edited by Sirisian
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