• 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

Simple collision detection for bounding cylinders

2 posts in this topic

Hi everyone,


I have been messing around with methods for doing basic collision detection recently. I was colliding with trees at the time, so I decided to use a simple radius test - i.e. if the camera comes within a distance 'rad' of any of the tree trunks in the scene, a collision has occurred. I initially started by simply reverting the camera back to its previous position before the collision occurred, thereby stopping the player in their tracks until their camera movement no longer intersects with the tree. This worked for a simple proof of the idea, but it was pretty ugly as a final implementation (being stopped in your tracks when you simply clip a tree lightly is not a fun experience after a few minutes).


I wanted to make a collision model which 'slides' you round the tree as you try to walk through it, rather than stopping you in your tracks as soon as you touch it. So, after some thinking, I came up with an easy way to do this. It's probably something people have seen before, but I'm going to post it here anyway in case anyone's interested in criticising it and discussing it. I'll try and describe the collision detection method assuming there's only one tree in the scene which we have to worry about. It can easily be adapted to handle more than one tree using things like for loops, etc... (I'm using it for 30 trees at the moment).


So, here's the method:


1. Check for collision. This is the simple part - every time you move your camera (or render a frame), check the distance of the camera from the tree's centre. Since the camera and tree are on the same level (the tree trunk extends out of the ground and above your head in the +y direction), we only need to worry about the x and z components of position:



D3DXVECTOR3 camTreeDist = D3DXVECTOR3(cam.x-tree.x, 0, cam.z-tree.z); //Vector describing the distance to the centre of the tree
if (D3DXVec3Length(&camTreeDist) < rad){collision = TRUE;} //Check for collision


This code uses the built-in DirectX function D3DXVec3Length() to determine whether the distance between the tree's centre and the camera is less than a certain value (rad). If you aren't using DirectX, then it's simple to find the length of a vector using a different function, or your own code.


2. So you now know that your camera has collided with the tree and is inside its bounding radius. At this point, we could simply revert the position of the camera back to its previous position before the tree collision, and the camera would stand still until the player moves in a direction which doesn't intersect with the tree. This isn't a very fun experience, for the aforementioned reasons. So how can we make the player 'slide' around the tree like in most first-person video games? With vectors! This picture describes the problem:




3. We have walked into the tree's trunk to the location marked as 'position' in the diagram. We actually want to 'slide' around the edge of the tree instead of walking through it. The place where we actually want to be is marked as 'desired position' in the diagram. How do we get to this desired location? From the diagram, it is easy to see that we need to extend our vector 'camTreeDist' to our desired location in order to put us on the outside of the tree trunk, but also to avoid the problem of simply reverting back to the camera's old position and preventing fluid movement. How do we do this? By adding a constant amount of 'camTreeDist' to our camera's position to put us outside the tree (denoted by the dotted line). This ensures that we have moved around the tree, but we are still outside its collision radius, such that:




Rearranging this formula means that ? can be calculated using the following formula:




Remember that the modulus of a vector can be calculated in Direct3D using D3DXVec3Length(). ? here represents the amount of 'camTreeDist' that we must add to our position inside the tree in order to put us in the equivalent location if we were outside the tree (the dotted line in the diagram). So, now all that remains is to actually translate our camera into the desired position:


cam += ((rad/D3DXVec3Length(&camTreeDist))-1)*camTreeDist;


...and that's it! These simple formulae will ensure that your camera slides smoothly around obstacles rather than awkwardly stopping when a collision is detected. Of course, this will only work for 'radial' objects where you can model their boundaries as a simple radius. It could also be applied to objects like sprites rather than a first person camera.





Share this post

Link to post
Share on other sites
That's a great technique and a good explanation. I never understand why people want to revert motion after a collision when this is so easy to do. It works equally well for walls.

There's a potential division by zero if you hit the centre of the tree. It's fine if the maximum movement is less than the radius, otherwise you ought to check.

Share this post

Link to post
Share on other sites

Thanks, I'm quite glad that it works, seeing as how the idea came to me when I was falling asleep last night! It does have a few other problems though - at the moment it only works for one object at a time, at least using my implementation of it anyway. I guess it could work for walls too, if I use the distance of the camera position from a plane describing the wall rather than simply the distance between two points.


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