# 3d Collision detection help xna?

I was hoping if you could help me out here. I'm developing a 3d table tennis game, and got the collision detection working perfectly when the ball hits the racket. However, now I would like the ball to move depending on where it hit the racket (example if it hit the far left side, it goes to the left etc..) Heres the collision detection I used:

private bool IsCollision(Model model1, Matrix world1, Model model2, Matrix world2)
{
for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++)
{
BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere;
sphere1 = sphere1.Transform(world1);

for (int meshIndex2 = 0; meshIndex2 < model2.Meshes.Count; meshIndex2++)
{
BoundingSphere sphere2 = model2.Meshes[meshIndex2].BoundingSphere;
sphere2 = sphere2.Transform(world2);

if (sphere1.Intersects(sphere2))
return true;
}
}
return false;
}


Any help would be greatly appreciated as to how i could improve this so as to get what I desire. Thanks

What I think you're looking for is:

If the Racket's center position is Vector A and the center location of the ball at its current location is Vector B then A - B will give you a vector pointing from the center of the racket to the ball meaning it now points in the proper direction of the "perpendicular to the tangent" or also called the "Reflection Normal" - though you'll have to .Normalize() it to make it unit length. Once you have done so, a regular Vector3 reflection equation will take care of the rest for you. If you don't have that equation it's pretty straightforward and google "Vector3 reflection" will give it to you straight away. The ball's original heading vector is the "Incidence Vector" in those equations.

• Did you give this a shot? What was the result? – Dracorat Nov 4 '11 at 15:46
• hey sorry for taking long, I'm going to try it out now and let you know how it goes. Thanks once again :) – Kurt Ricci Nov 4 '11 at 23:20
• hey, im having a little bit of trouble here. So, if I understood correctly, I have 2 vector3, Position_rakcet and Position_ball. Now I subtract these 2 together and get Vector C. Now for the vector3 reflection, the best thing i could find was this site msdn.microsoft.com/en-us/library/bb198637.aspx. I'm not sure which part to use. Also, the way I'm currently moving the ball is by changing its .Z position. Now if the ball hits the racket at the side, I'm guessing that apart from changing it's .Z, I would also need to change its .X position. I'm I correct in thinking so? Thanks once again – Kurt Ricci Nov 5 '11 at 16:17
• I'm assuming that you're using a Vector3 for movement. Position += MovementVector. Even if you start with a Vector3(0,0,1) (one along the Z axis) then when you strike the surface, the reflection vector may not be 0,0,-1 - it could for instance be .3,.3,-.4 (off the top of my head - I'm just making these up for example) which would result in movement along all three axis. The vector returned from the function you linked will have all three parts - so you don't have to add the individual axis - you just change the movement vector to that result and continue to add the movement vector over time. – Dracorat Nov 7 '11 at 15:31
• Yes, Im moving the racket by Position_racket += velocity_racket (both Vector3). The ball starts stationary, at Position_ball (also Vector3). I'm still finding difficulty in calculating the reflection normal you mentioned before. Also that site uses planes, should I use it too, since I'm collising two models? – Kurt Ricci Nov 7 '11 at 16:43

If you're using a physics engine (which I think you're not, but you might consider it) I'd recommend you use an actor impostor for the racket. Instead of doing collision against the proper model of the racket, create a version for the physics which has convex and concave surfaces designed to bounce the ball according to where it intersects. In fact, you might use two actors, one for the netting so that you can return the ball with close to the full force it hits the net and one for the framing since a ball that hits the framing usually loses a lot of momentum in addition to radical direction changes.

• Hey, thanks for replying. I'm not using a physics engine though, and if possible I'm trying not to use one as this is an assignment and dont thing the lecturer would allow it :( – Kurt Ricci Nov 3 '11 at 17:00
• I'm going to add another answer then that I think will set you straight =) – Dracorat Nov 3 '11 at 18:13