Detecting where the ball hit the bounding box? XNA
just got started with XNA and thi开发者_如何学Pythons example would help out a lot!
I´ve done a simple test where i got some terrain and a ball, when the ball hits the terrain it stops. This i do by setting the ball position to just on top of the terrain on collision.
To my question, im using bounding box for this, but it doesnt look very well if the ball hits the terrain from the left or right side since it respawns on top. How do i check of it hits on side?
Short glitch of the code right now:
if (playerOne.BoundingBox.Intersects(terrain.BoundingBox)
playerOne.Position.Y = terrain.BoundingBox.Top;
Where playerOne is the ball.
Thanks in advance!
There is no built in way for XNA BoundingBox to help with determining which side has collided. Some people have split up the bounding box into 6 boxes (one for each face of your current box, like walls around a room) but this still gives a little trouble near the corners where you may have overlap and have to resolve which box you think hit first.
The ultimate solution is to create a Plane for each face of your current bounding box and if the bounding box detects a collision, then check for collision against each plane. When you have collision, it may be against 1, 2, or 3 planes at once. If it's against more than 1, you then need to determine point/plane contact positions for each involved plane & whichever is closer to the ball's previous position, that's the side it hit first.
I strongly recommend this tutorial, it covers your exact situtation by breaking the bat into different areas and setting various conditions for collision rebound by using the Math Helpers and radians.
http://ross-warren.co.uk/pong-clone-in-xna-4-0-for-windows/6/
It's side on but you should be able to adapt the principles.
精彩评论