Problem with collision detection of a fast moving ball with a racket controlled by mouse

假如想象 提交于 2019-12-03 02:59:59

As others have pointed out, the problem is that the ball goes from being on a side of the pad in one frame to being on the other side in the next. Fast moving objects tend to do that if the barriers are too slim.

There are three very simple solutions for this problem:

  • Increase the size of the pad or the ball, which is what happened when you changed the collider size.
  • Establish a maximum speed for the ball, so that it can never move fast enough to go through the pads.
  • Increase the frequency with which Unity makes its physics calculations. It can be changed in Time Manager, reducing the value of Fixed Timestep. Beware of reducing this too much, or the physics engine will be unable to finish a call before the next round is supposed to start and it will never be able to catch up with the game.

Setting a maximum speed for moving objects is something that must always be done. You cannot risk having an important object skyrocket during the game and leave everything in an uncontrolled state.

What I think is happening is that each interval that happens the ball/racquet are moved and then checked for a collision. The issue is that the ball/racquet move to far in a single interval and miss the collision.

1) Ball before racquet 
2) Ball after racquet 

not

1) Ball before racquet 
2) Ball touching racquet 

So what you have to do is in your FixedUpdate() method of your ball GameObject is cast a ray from the current ball location to the previous ball location. If there is anything between those two points that should have been hit (ie the racquet) move the ball back to the reported hit point on the ray. This will trigger your existing collision detection stuff.

The reason increasing the size of the collider works as well is because the ball isn't skipping the lager collider. This has the drawbacks you mentioned in your question. The ray casting avoids this issue and allows the ball/racquet to move as fast or slow as they need to.

This is not a complete answer, but I remember dealing with a problem like this many years ago on slower machines.

The problem was using sprite-based collision detection; relying on pixels for the sprite and obstacle being rendered at the same coordinates. If the frame rate is so low that the sprite moves more than the obstacle's size in one frame, you will get into situations where it is (for example) left of the obstacle in one frame and right of the obstacle in the next frame without ever occupying the same pixels.

In this case sprite-based collisions do not work, you have to base collisions on vectors. Instead of checking each sprite pixel for collisions, record the position and convex hull of the sprite. After each frame, calculate a vector from the old position to the new position and intersect it with the convex hull of each obstacle.

There are multiple shortcuts you can make, like comparing only against bounding boxes at first and calculating the hull only if the vector intersects a bounding box, but this is the basic idea. Good luck.

I have been working on a 3d pong game as well, and have run into the exact same problems. I'm going to try enlarging everything like you guys did. As for the paddle adding speed and spin to the ball, I was baffled by this until I realized that changing the position of the paddle does not change its velocity. If the paddle was at zero velocity before it was moved, it will be at zero when the physics engine looks at it next frame. Un checking is kenimatic and controlling the paddle directly through the velocity property fixed the problem. It caused the paddle to jitter when hitting walls, but I fixed that by removing the walls from the paddle layer and handling boundaries manually in LateUpdate. Also, when you update the velocity, first store the new desired velocity in Update so that the controls work smoothly, then commit the changes in FixedUpdate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!