• 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
Nicholas Kong

Why is collision cast expensive?

33 posts in this topic

"Suppose the code can request a ray cast ahead of time"

It doesn't sit there and do nothing, it gets on with other work until the result is ready. Yes, it's a bit more complicated, but that's the world we live in, and that's what games have to to to get the best performance.

An old article, but it describes the current state of affairs:

http://www.gotw.ca/publications/concurrency-ddj.htm

I honestly don't understand the point you're trying to make, you're using a very arbitrary example and completely ignoring my point that often code(especially for games) does things in linear steps and has to wait on different parts to finish so threading may not be an optimal solution. I.e. there is no point to threading AI and collision detection because neither can do their job at the same time if the state of the objects isn't set from the opposite step already.

 

You can't really "request a ray cast ahead of time" when it checks the collision it does it all at once, broad phase then narrow phase on all the objects, more than that.. depends on the implementation but that is the gist of it. That especially becomes impossible to run concurrently if you loop through once and move objects because that would skew later collisions if run in seperate parallel loops.

 

Also that article.. I really don't get the point of you linking, it has no relevant information to software development or even any code or examples and is basically a rhetoric document on "the magic of concurrency" which is like a magic of classes document, classes have many limitations we have successively found more and more restrictive as time goes on and coding practices evolve. Thinking we can just magically thread things that cannot logically be threaded is a dreamer statement. Threading has a time and place like every other tool.

1

Share this post


Link to post
Share on other sites
Satharis:

You say ray casts cannot run concurrently with other game code. I say they can and and they do.

Beyond that, I can't be bothered to argue. I've made my point to anyone who cares to listen.

Edit: I lost patience here, sorry. Normal service will be resumed shortly.
-3

Share this post


Link to post
Share on other sites

the main difference is that an oct-tree divides the space into 8 regions in each step, so requires less recursion, but has to do a few more checks at each step (since there are 8 possible regions to step into). dividing the space in a binary-fashion allows simpler logic, but a larger number of internal nodes.

 

That's not necessarily true, you can implement an "oct-tree" with a b-tree under the hood; your recursion step should cycle over each axis.  Thus the number of recursive steps would be similar in complexity to a BSP-tree.

 

But I think my question was poorly worded.  Simply put, I'm curious about if it's worth it to "choose" a splitting plane versus naively cutting it down some midpoint in the dynamic real-time situation you were describing.  The obvious difference is that choosing the latter would be O(1), and the former would be (presumably) at least O(N).  That of course would pay off as searching would be faster.  If the tree were generated before-hand and kept static, then of course it'd be better to load the cost up front, but in the case of a real-time dynamically generated tree, I'm wondering if, generally, such an approach is still worth it.

 

It's a bit of a subjective question and it begs more for intuition/experience than a purely academic response, I suppose.

 

It's not about memory it's about how much CPU time doing a lot of ray casting takes really.

 

I'm wondering if there's a subtle point here that you're also describing.  If you're just talking about memory allocations, then of course it's almost never an issue, but isn't memory bandwidth is a large bottleneck for speed these days?  I don't work in the games industry, so I'm not aware of the current state, but isn't it a challenge to maintain cache coherency in collision detection systems, too?  Or is cache coherency kind of a solved issue at this point?

1

Share this post


Link to post
Share on other sites

Polarist, on 18 Feb 2013 - 19:22, said:
 

cr88192, on 15 Feb 2013 - 19:07, said:
the main difference is that an oct-tree divides the space into 8 regions in each step, so requires less recursion, but has to do a few more checks at each step (since there are 8 possible regions to step into). dividing the space in a binary-fashion allows simpler logic, but a larger number of internal nodes.

That's not necessarily true, you can implement an "oct-tree" with a b-tree under the hood; your recursion step should cycle over each axis. Thus the number of recursive steps would be similar in complexity to a BSP-tree.

But I think my question was poorly worded. Simply put, I'm curious about if it's worth it to "choose" a splitting plane versus naively cutting it down some midpoint in the dynamic real-time situation you were describing. The obvious difference is that choosing the latter would be O(1), and the former would be (presumably) at least O(N). That of course would pay off as searching would be faster. If the tree were generated before-hand and kept static, then of course it'd be better to load the cost up front, but in the case of a real-time dynamically generated tree, I'm wondering if, generally, such an approach is still worth it.

It's a bit of a subjective question and it begs more for intuition/experience than a purely academic response, I suppose.

ok.

well, it depends on whether or not the oct-tree has a calculated mid-point (as opposed to simply dividing into 8 equal-sized regions).

if it does, then both methods will involve a similar cost (a loop over all the items to find the midpoint), though with a potential difference that an oct-tree doesn't have to (also) calculate a distribution vector.

interestingly, regardless of how exactly it is done, the total complexity would remain the same: O(n log n).


this is because (unlike a traditional BSP), my approach doesn't "choose" the plane, it calculates it.
basically, all you really need is an averaged center-point, and a vector describing how the "mass" is distributed relative to the point.

so, pseudocode:

point=vec3(0,0,0); count=0; cur=list;
while(cur)
{
    point=point+cur->origin;
    count++;
    cur=cur->next;
}
point=point/count;

cur=list;
dirx=vec3(0,0,0);
diry=vec3(0,0,0);
dirz=vec3(0,0,0);
while(cur)
{
    dir=cur->origin-point;
    dirx=dirx+dir*dir.x;
    diry=diry+dir*dir.y;
    dirz=dirz+dir*dir.z;
    cur=cur->next;
}
dir=v3norm(v3max(dirx, v3max(diry, dirz)));  //normalized greatest-vector
plane=vec4(dir, v3dot(point, dir));

left=NULL; right=NULL; mid=NULL; cur=list;
while(cur)
{
    f=v3ndot(cur->origin, plane);
    if(fabs(f)<cur->radius)
        {cur->chain=mid; mid=cur; }
    if(f<0)
        {cur->chain=left; left=cur; }
    else
        {cur->chain=right; right=cur; }
    cur=cur->next;
}
...

Quote
 

Satharis, on 16 Feb 2013 - 06:07, said:
It's not about memory it's about how much CPU time doing a lot of ray casting takes really.

I'm wondering if there's a subtle point here that you're also describing. If you're just talking about memory allocations, then of course it's almost never an issue, but isn't memory bandwidth is a large bottleneck for speed these days? I don't work in the games industry, so I'm not aware of the current state, but isn't it a challenge to maintain cache coherency in collision detection systems, too? Or is cache coherency kind of a solved issue at this point?

personally, I haven't usually found cache to be a huge issue on recent PC hardware.

even then, it isn't usually as much of an issue at present, as memory bandwidth has increased considerably over the past several years (relative to CPU speed increases), making the limit harder to run into (currently typically only really happens during bulk memory-copies and similar AFAICT, rather than in general-purpose code).

it was much worse of a problem 10 years ago though.


ATM, branch-prediction-failures seem to have become a much bigger issue (manking conditionals very costly in some algorithms where the ability of the CPU to accurately predict branches is fairly low).

a particular example in my case was devising a branch-free version of the Paeth filter, mostly as the conditionals inside the filter were eating lots of clock-cycles.

Edited by cr88192
0

Share this post


Link to post
Share on other sites

this is because (unlike a traditional BSP), my approach doesn't "choose" the plane, it calculates it.
basically, all you really need is an averaged center-point, and a vector describing how the "mass" is distributed relative to the point.

 

Ah, I see, that sort of calculation should be relatively cheap to what I was imagining.  Thanks for explaining.

 

even then, it isn't usually as much of an issue at present, as memory bandwidth has increased considerably over the past several years (relative to CPU speed increases), making the limit harder to run into (currently typically only really happens during bulk memory-copies and similar AFAICT, rather than in general-purpose code).

 

it was much worse of a problem 10 years ago though.

 

This is good to know.  A lot of what I know about game programming is, unfortunately, dated to roughly 10 years ago.  It doesn't help that I was reading a bunch of articles from Intel recently to catch up, who may be blowing the bandwidth issue out of proportion  (I don't know, just a guess).

0

Share this post


Link to post
Share on other sites

this is because (unlike a traditional BSP), my approach doesn't "choose" the plane, it calculates it.
basically, all you really need is an averaged center-point, and a vector describing how the "mass" is distributed relative to the point.

 

Ah, I see, that sort of calculation should be relatively cheap to what I was imagining.  Thanks for explaining.

 

this is part of why/how it is "quick and dirty"...

 

 

 

even then, it isn't usually as much of an issue at present, as memory bandwidth has increased considerably over the past several years (relative to CPU speed increases), making the limit harder to run into (currently typically only really happens during bulk memory-copies and similar AFAICT, rather than in general-purpose code).

 

it was much worse of a problem 10 years ago though.

 

This is good to know.  A lot of what I know about game programming is, unfortunately, dated to roughly 10 years ago.  It doesn't help that I was reading a bunch of articles from Intel recently to catch up, who may be blowing the bandwidth issue out of proportion  (I don't know, just a guess).

 

yeah.

 

granted, it probably depends a lot on the code.

 

but, 10 years ago, it was fairly common to hit the limit if things didn't all fit in cache, in general, such as when working with arrays, ...

now it generally requires doing lots of SIMD operations or similar, or running all cores at high load, ..., since normal scalar code doesn't usually run fast enough.

 

 

basically, while CPU clock speeds have increased by a factor of around 2-3, memory speeds have increased by a factor of around 8-10.

 

granted, it is a little worse off if one considers it per-core, or includes lots or multithreaded SIMD-based code, which can also hit the limit, but multithreaded SIMD-heavy code is still a relative minority of the code in use, and at least from the POV of "generic" single-threaded scalar code, things have gotten better...

 

granted, Intel tends to assume a lot more agressive use of SIMD than often seen in practice, where at least AFAICT, most of us (?) are mostly using SIMD as a nifty feature to speed up 3D vector math, rather than writing piles of highly vectorized code (probably because vectorization is generally a huge PITA...).

 

granted, in any case per-scale it is worse than 20 years ago (where the CPU speeds and RAM speeds were much closer).

0

Share this post


Link to post
Share on other sites
I'll have another go at explaining why ray casts in many game engines are asynchronous and why that's important for performance.

Let's assume that the physics is already multithreaded, as this is often the case. Let's assume also that game logic and AI are not multithreaded, because that's quite difficult to do correctly. So game logic and AI are taking up a big portion of the frame time, and any calculations that can be offloaded to another thread will be a win.

Now consider ray casts. Game logic and AI may require thousands of them. Line of sight checks for AI. Damage tests for weapons. They are performed by the physics engine, which is thread safe already.

So you tell the game programmers they can use all the ray casts they want as long as they request them early and use the result later. A queue builds up all the requests for raycasts in a frame. Then, at a suitable point they are all processed. The main thread is notified that the results are ready. Everyone is happy, and the game programmers don't complain about slow ray casts anymore.

The article I linked to is a classic. It isn't academic and dreamy. It's about reality. The next generation of consoles will have eight cores (http://www.eurogamer.net/articles/df-hardware-spec-analysis-durango-vs-orbis). The PS3 already has that many. PCs won't be far behind.

Concurrency isn't "a tool that has its place", it runs through the whole design of a modern game engine, and will be even more important as time goes on.
0

Share this post


Link to post
Share on other sites

I'll have another go at explaining why ray casts in many game engines are asynchronous and why that's important for performance.

Let's assume that the physics is already multithreaded, as this is often the case. Let's assume also that game logic and AI are not multithreaded, because that's quite difficult to do correctly. So game logic and AI are taking up a big portion of the frame time, and any calculations that can be offloaded to another thread will be a win.

Now consider ray casts. Game logic and AI may require thousands of them. Line of sight checks for AI. Damage tests for weapons. They are performed by the physics engine, which is thread safe already.

So you tell the game programmers they can use all the ray casts they want as long as they request them early and use the result later. A queue builds up all the requests for raycasts in a frame. Then, at a suitable point they are all processed. The main thread is notified that the results are ready. Everyone is happy, and the game programmers don't complain about slow ray casts anymore.

 

some of this depends a lot on the game engine architecture though.

 

for example, in my engine, ray-casts aren't handled by the physics engine, but rather the "server end" logic (which basically deals with things like managing the scene-graph, communicating with the client, and basically providing the environment for the game-logic to do its thing).

 

so, the physics engine basically sits off by the side, with the server-end mostly shuffling data between the physics engine and the main scene-graph, ...

(I am using a custom physics engine with an OpenGL-like API design).

 

 

I would guess all this likely presumes an event-driven approach to game-logic though, like say, triggering an event when the results of the trace get back, ... rather than performing a trace and expecting to get back the results immediately (currently more how it works in my case), ...

 

 

The article I linked to is a classic. It isn't academic and dreamy. It's about reality. The next generation of consoles will have eight cores (http://www.eurogamer.net/articles/df-hardware-spec-analysis-durango-vs-orbis). The PS3 already has that many. PCs won't be far behind.

Concurrency isn't "a tool that has its place", it runs through the whole design of a modern game engine, and will be even more important as time goes on.

 

yes, interesting...

 

the problem though is that it is currently a bit of a challenge gradually migrating away from the more traditional "single giant thread which does everything" style of software development.

 

as-is, it more ends up looking like "several giant threads which do everything...".

0

Share this post


Link to post
Share on other sites
Yes, I'm making a few assumptions:

1. That the physics engine does the ray casts, and it is already multithreaded.

2. That there is a performance issue on the main thread.

3. That ray casts account for a significant amount of processor time.

If any of those don't apply, it's not an appropriate technique. And it does require some reorganisation of the code, so it's not free.

The ideal, really, would be for all systems to be multithreaded perfectly. But this is an unsolved problem, so we do what we can.
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