1
\$\begingroup\$

So, I'm trying to develop a 3d table tennis game, and I'm having problems when it comes to colliding the ball with the table. I have a bounding sphere for the ball and bounding box for the table, but the intersection isn't that accurate, and I'm guessing that the bounding box is incorrect since colliding the ball with the racket is good.

I'm trying to display the bounding box so that it would be easier to see where the error is, but I cant seem to implement the tutorials I've found with my code, or just don't know how. And, I'm I right in creating an AABB since my table isn't moving? If anyone could help, it would be greatly appreciated, or if anyone could suggest a better way to collide a ball with a box (if there is) thanks. I've pasted my bounding box detection, and the collision between the sphere and box. If more code is needed I'll post it. Thanks

http://pastebin.com/NDYmhXCh

http://pastebin.com/47vwJWSc

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Your code for creating the bounding box is correct, just make sure you're entering the correct world transform and off setting the position by whatever might have when you created it. Just keep debugging with break points, it's probably a small error in logic somewhere. Where are you calling the updateBoundingBox method from?

A quick tip, if you want to see if you created your aabb correctly without rendering it, just set your ball position to the aabb.max.X or aabb.min.Y or something like that inside the update method. That will show you the boundaries of the aabb without the need to create code for rendering.

\$\endgroup\$
1
  • \$\begingroup\$ Hey, It seems that the code for the bounding box was giving me the wrong values (not sure why), but then I did what you said and placed the ball about the edges of the table (don't know why I didn't think of that lol) and now my collision works like a charm. Thanks a real lot :) \$\endgroup\$
    – Kurt Ricci
    Commented Mar 5, 2012 at 21:57
3
\$\begingroup\$

Well, you're asking a few different questions there. I'll start by answering your main question which is about how to display a debug representation of a BoundingBox object.

How to render a BoundingBox

The easiest way I can think of is to get the corners of the bounding box and render it using a line list primitive. You can get the corners of the bounding box using the BoundingBox.GetCorners() method (documentation). This method returns 8 positions in world space. The first four represent the front face of the bounding box, and the final four represent the back face of the bounding box.

Then simply create a VertexPositionColor vertex for each of the corners, and render lines between them in the correct order. Whether you use indices or not is up to you.

No need to implement it though, just download this sample from the education catalog, add the DebugShapeRenderer class to your project, Initialize it and finally use the provided AddBoundingBox method. It does pretty much what I described above.

Creating BoundingBox for the table

And am I right in creating an AABB since my table isn't moving?

That's subjective. If after checking it out with the debug drawing code above the bounding box is being calculated correctly, feel free to use it.

But personally, in this situation, I would just create the bounding box manually to allow more control over the collision area. Something like the following code (using 1 game unit = 1 cm as the scale):

class Table
{
    Vector3 size = new Vector3(152.5f, 274f, 5f);
    float height = 76f;
    Vector3 position = Vector3.Zero;

    BoundingBox boundingBox;

    public Table()
    {
        Vector3 min = position + Vector3.Up * height - size / 2f; 
        Vector3 max = position + Vector3.Up * height + size / 2f;
        boundingBox = new BoundingBox(min, max);
    }
}

I find this approach much simpler overall.

Better way to perform collisions

Or if anyone could suggest a better way to collide a ball with a box

I think incorporating a physics engine such as JigLibX (link) would yield better results and serve you better in the long run. The problem with most applications like this isn't collision detection, but collision response, and a physics engine really makes life much easier in that department.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for answering but it seems that my bounding box function was giving me the wrong values :/. I can't really implement a physics engine as its sort of an assignment :/ But thanks again for answering, appreciate it :) \$\endgroup\$
    – Kurt Ricci
    Commented Mar 5, 2012 at 21:59
  • \$\begingroup\$ @KurtRicci Wasn't your question about how to render a bounding box for debugging? I mean the title is "3d bounding Box border xna", not "Is my bounding box calculation code right?". \$\endgroup\$ Commented Mar 5, 2012 at 22:28
  • \$\begingroup\$ ye, it sort of was. I wanted to draw the bounding box to check if it was calculating it correctly mostly, but now i've removed the function completely since i've managed another way. I'll most prob still use a lot of whats said from your post as I still need to calculate when it hits the walls and floor, which i'm guessing is more complicated as I've got more sides. So thanks once again. \$\endgroup\$
    – Kurt Ricci
    Commented Mar 5, 2012 at 22:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .