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

Selecting units in a 3d evironment

9 posts in this topic

I'm making an strategy game using openGL on Android.

It is time to upgrade my unit selection code, which now consists of simple ray to plane intersection.

What I did the last time is use my own math functions and transformations to figure out which unit the user is clicking.

So the question arises: Once I've rendered a scene, is there an easy way to figure out which the unit the user is tapping?

I'm not using any engine here, just openGL on android with my home-brew math library. Edited by SillyCow
0

Share this post


Link to post
Share on other sites
There's two ways, as far as I know.

The first, revert the clicked screen coordinate to world coordinates by using your projection in reverse. This is done by inverting the modelviewprojection matrix, which if you aren't familiar with it is going to be tricky. Then use the world coordinate to look up the nearest unit.

The second and simpler approach is to have an ID buffer. Objects (or their selection area) should render a uint unique ID to a buffer, which you then sample at the point you click with glReadPixels. Then just have a lookup of the ID to get the object.
1

Share this post


Link to post
Share on other sites
[quote name='Zoomulator' timestamp='1341320624' post='4955275']The first, revert the clicked screen coordinate to world coordinates by using your projection in reverse. This is done by inverting the modelviewprojection matrix, which if you aren't familiar with it is going to be tricky. Then use the world coordinate to look up the nearest unit.[/quote]Why invert? [img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]

What I do sounds very similar to what he is already doing: computing the camera matrix (inverse view matrix, without inverting,) this produces the world space position as well as the basis vectors for "camera." When I have window coordinates to pick, I convert them to normalized device coordinates then use the world-space information + projection information (i.e. near plane distance) to construct a ray, then perform a search to find the hit. If scaling is involved in the camera matrix calculation, I use the properties of matrices to avoid normalizing the basis / direction vectors.

I like this approach because I need the world-space camera information for other functions such as culling, billboarding, etc.

If you need pixel perfect picking then the selection buffer approach described by Zoomulator is a simple solution. Though if the user does not tap exactly on a pixel covered by the unit it will not register a hit. Edited by scniton
1

Share this post


Link to post
Share on other sites
The inverse matrix exactly reverses the transformations you did to get the screen coordinates. I forgot to mention that you also need the depth buffer value as the z coordinate in this case and normalizing the xy coordinate which you mentioned..

I also realised that it's probably just the view-projection you'd want to get the coordinate. The model matrices can be left out..*

A single matrix multiplication and you're done, as long as you got the inverting right.
VPmatrix * WorldPos = NormScreenCoord
InverseVPmatrix * NormScreenCoord = WorldPos

No need for a raycast search or compensation for scaling, but still requires a spatial search of some kind to get the object.

Regarding the ID buffer, maybe the tapping could be resolved by sampling an area rather than a single pixel. Check the pixels for an ID, and if there's more than one that isn't just background, count which one there's more of. Or something like that.. I don't really know how touch screens calculate those things.

In my current project I'm using both. The ID buffer to look up objects, since it's a pretty fast mapping, but for moving things I use the screen-to-world transformations. I don't know how optimal it is, but it lets me get away with mapping things without keeping more advanced query structures that can do spatial and ray cast searches.

* I'll have to look this up though Edited by Zoomulator
1

Share this post


Link to post
Share on other sites
Sorry, I don't have any good example code for it.. I worked it out myself. I used [url="http://www.khanacademy.org/"]khan academy[/url] to get my matrix inversion right.. the ID buffer should be easy enough if you know how to manage framebuffers in OpenGL.

I'm not using legacy GL though. v3.3 and shaders, with the fragment shader outputting both to a color buffer and an ID buffer. I guess it's OpenGL ES for android? It's kindof like 3.3 scaled down?

You could also make two rendering passes, one for color and one for ID and binding different buffers.

Once you've got the ID buffer, it's very easy to use the glReadPixels to get at the information.

Sorry that it's a bit thin on code. See it as an exercise ;)
Anything specific that you can't figure out? I'll try to help
0

Share this post


Link to post
Share on other sites
[quote name='SillyCow' timestamp='1341379005' post='4955523']
Does anyone have a link to an article, or some sample code?
Doesn't matter which language.
[/quote]While my personal code is kind of tailored to my application, [url="http://clb.demon.fi/MathGeoLib/"]MathGeoLib[/url]'s frustum([url="https://github.com/juj/MathGeoLib/blob/master/src/Geometry/Frustum.h"].h[/url]|[url="https://github.com/juj/MathGeoLib/blob/master/src/Geometry/Frustum.cpp"].cpp[/url]) code is very clean (though I personally prefer the "[url="http://www.lighthouse3d.com/tutorials/view-frustum-culling/radar-approach-testing-points/"]radar[/url]" approach to intersection testing.)

An example of what I described:
If your view transform is something like this:
Translate1*Scale*RotateZ*RotateX*RotateY*Translate2

Your camera transform is this:
Translate2' * RotateY' * RotateX' * RotateZ' * Scale' * Translate1'

Now you can extract the position of the camera from the resulting matrix, like in the image on [url="http://www.songho.ca/opengl/gl_transform.html#modelview"]this page[/url].
Note: You'll likely want the left / up / forward vectors to be normalized, what you can do as an optimization is the following:
Translate2' * RotateY' * RotateX' * RotateZ' * Translate1' * Scale' (only apply inverse scaling to the transformation part of the matrix).
That way the columns in the rotation part of the matrix are all unit.

This gives you the camera information.

Take the coordinates of the tapping and convert them from window coordinates to normalized device coordinates, then your ray's direction is simply:
Forward * nearplane distance + Left * x (in NDC) + Up * y (in NDC)
Where forward/left/up are extracted from the camera matrix as described above, the ray starts from your camera's position (translation part extracted from the matrix.)
Note, you might have to change some of the signs for the direction equation depending on the convention you use for the window coordinates origin.

Disclaimer: I don't remember why there's a conversion to NDC nor do I have the time to verify it right now.
Also, I had written in my notes, that this assumes a symmetric frustum, but I can't remember why either. Edited by scniton
1

Share this post


Link to post
Share on other sites
There's another alternative, less flexible but should work nonetheless. Make a 3d cursor like you would make any other unit, as a 3d model hovering the terrain, and its coordinates on the terrain are controlled by the mouse. It won't work with the UI, though...
0

Share this post


Link to post
Share on other sites
[quote name='DiegoFloor' timestamp='1341410166' post='4955622']
There's another alternative, less flexible but should work nonetheless. Make a 3d cursor like you would make any other unit, as a 3d model hovering the terrain, and its coordinates on the terrain are controlled by the mouse. It won't work with the UI, though...
[/quote]

This runs on a touch screen. Hence, there is no mousemove event before the screen is tapped.
0

Share this post


Link to post
Share on other sites
[quote name='SillyCow' timestamp='1341415062' post='4955649']
[quote name='DiegoFloor' timestamp='1341410166' post='4955622']
There's another alternative, less flexible but should work nonetheless. Make a 3d cursor like you would make any other unit, as a 3d model hovering the terrain, and its coordinates on the terrain are controlled by the mouse. It won't work with the UI, though...
[/quote]

This runs on a touch screen. Hence, there is no mousemove event before the screen is tapped.
[/quote]

Ah, sorry. Didn't read it was for mobile.
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