Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In unity, i have a racket which is supposed to hit a ball, and the racket is controlled directly by the mouse, i.e the bat is being moved by the mouse using mouse axes and the using transform.translate() function to move the racket.

I expected that Unity3d's physics will not properly translate the racket's movement directly by mouse and impact the ball accordingly, and i would have to write something custom, and it turned out to be true.

But the ball's collision is not being detected properly when the racket is moving. When it is still, everything is fine, and the ball behaves as i like.

Now i went as far as writing a custom physics script (i use C# for scripting) in which i attached 4 raycasts of length 0.6F to the ball, and after doing some complex vector calculations, calculate the velocity of the ball after hitting the racket, and apply it directly to the velocity of the ball using rigidbody.velocity = calculateVelocity(). Now it is again working fine when the racket is not moving, but not when i move the racket. The exact (symptoms of the) problem is:

Using built-in Physics and collision detection: When the racket is moving, the ball sometimes pass straight through the racket, and sometimes, it slows down (to unbelievable levels).

Using my script to calculate velocity: The problem is the same, but it lets me identify what is wrong when i print the normal of the collider (the racket). It sometimes give the right normal, and sometime gives the negative of the normal vector, which means that it is going straight through the top surface and detecting the hit with the bottom side of the collider (racket).

The things i have tried:

  1. Increasing the size of the collider (it works with the wider box collider on the racket, but then obviously the ball moves from quite a distance away from the racket, and my own script works here, default physics give strange results when racket is moved), in short i don't get the reality that i want.

  2. Decreasing the fixed timestamp to 0.001, which significantly improved things, but still very very far away from the result i want, and the ball is again quite often picking the wrong side of the ball.

  3. Changing collision detection to continuous dynamic. Which didn't improve things either.

And in addition to the wrong side picked at collision, another problem i have observed is that after bouncing off the racket, the ball is moves but the racket is moved faster, it instead of moving in a complete arc or line, somehow appears in front of the ball, resulting in two hits. It is a conjecture based on what is visible.

Also it is clear that the "movement" aspect of the racket is not being read by Unity3d's built in physics, resulting in strange behavior when racket is moving using mouse hits the ball.

I am stuck, i have no idea where to move from here. Please tell me what is it that i am doing wrong.

like image 704
SpeedBirdNine Avatar asked Sep 09 '11 16:09

SpeedBirdNine


3 Answers

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.

like image 200
Elideb Avatar answered Oct 31 '22 09:10

Elideb


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.

like image 40
Justin808 Avatar answered Oct 31 '22 08:10

Justin808


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.

like image 3
Dour High Arch Avatar answered Oct 31 '22 10:10

Dour High Arch