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

Sphere-box collision

10 posts in this topic

Hello, I'm trying to collide a box with a sphere. The box can turn in all directions, it's just a solid object. I can't figure out how to test a box with a sphere. The info I know when I'm testing: Sphere: - Radius - Position Box: - Size - Position - Rotation Can anyone please help me with some advice or code? GBas [Edited by - GraphicsBas on February 12, 2005 6:46:43 AM]
0

Share this post


Link to post
Share on other sites
Can you tell us how accurate you want this thing to be?

Also, what specific situations do you have? Will the sphere's be close to the same size as the rectangle?

One way is to use a sphere for the rectangle. Make it so that the sphere has equal volume as the rectangle. Than check for sphere-sphere collision.
0

Share this post


Link to post
Share on other sites
As Sagar suggested, some more info might be helpful. Since you say sphere, I'm assuming 3D. So then box.size is a vector? And is box.rotation a 3x3 matrix? Finally, do you want static (fairly easy) or swept (harder) intersection?

For static intersection, it goes something like this. First, you transform the sphere center into the local space of the box, where the box is axis-aligned. Then you find the closest point on the box to the sphere center. This can be done very easily - I'd search for 'sphere AABB intersection', but if that doesn't turn it up I can help you with that code. Finally, if the squared distance from the sphere center to the closest point is less than the squared radius, the box and sphere intersect. If you need it, you can construct a translation vector which will push the objects apart until they are just touching.
0

Share this post


Link to post
Share on other sites

To collision-test sphere against box:
For each side of box:
Collision-test sphere against plane of that side.
If collision occurs, within the bounds of the side, report that collision.
Else, report no collision.

To collision-test sphere against plane:
Find the line parallel to the plane normal, passing through the centre of the sphere.
Find the intersection of that line with the plane.
If the distance between that point and the sphere centre <= the radius of the sphere:
Report collision at that point.
Else, report no collision.
0

Share this post


Link to post
Share on other sites
Zahlman, unless I'm overlooking something, I don't think that method will work. It will return false negatives in certain cases where the sphere intersects a corner or edge of the box.

I may be misunderstanding though...

In any case, I would still go with the closest-point-on-box method, which will be faster and probably more robust.
0

Share this post


Link to post
Share on other sites
You are correct. It would also be necessary to test against each edge (in a similar way) and corner (just check distance from the corner). Doh.
0

Share this post


Link to post
Share on other sites
In 3D. I want it to be very accurate, I also want to find the normal of the collision and the collision depth. It's just a sphere dropping on a box. It can be close to the box. But also very far away.

I've got rotation in an vector but I constructed a 3*3 matrix of it while testing. The size of the box is also stored in one vector. The box can move and rotate in the simulation as well as the sphere.
I would really like to see some code please.

I need this for a physics engine.
0

Share this post


Link to post
Share on other sites
My initial thought is to make a plane for each surface of the cube. Then find the distance from the center of the sphere to the nearest point on the plane. Subtract the radius of the sphere.
Do this for each plane, if the results are all negative you have a collision.
Not definite this will work yet.

- As an optimisation maybe you wouldn't have to do it for each plane, only three planes then subtract or add the width of the cube for the 'opposite' sides.

Edit: This method seems to be the same as Zahlmans idea, I can't see where the false negatives come in though? I can see false positives around the corners though :/
0

Share this post


Link to post
Share on other sites
It's quite simple:

- First you have to get the sphere into the box' coordinate system (eg. by rotating the entire thing so that it is axis aligned).

- Then take the point within the box that is closest to the sphere's center. A plain set of if statements in C++:

if(sphere.center.x < box.leftx)
closest.x = box.leftx;
elseif(sphere.center.x > box.rightx)
closest.x = box.rightx;
else
closest.x = sphere.center.x;

// Same for all other dimensions you have



- Now, if the distance from this closest point to the sphere's center is less then the sphere's radius, you've got a collision.


Things quickly become complicated if one or both objects are moving, of course ;)

-Markus-
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