Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collision with fast bodies. #202

Open
fjguerrero opened this issue May 5, 2015 · 1 comment
Open

Collision with fast bodies. #202

fjguerrero opened this issue May 5, 2015 · 1 comment

Comments

@fjguerrero
Copy link

Hello!

I'm developing a free kick game, where the player has to shoot a ball and smash a target.

I have a problem with the collisions when the ball is too fast. I uploaded this video showing the problem. You can see, for example in the last shot [1:28], how the ball cross the wall, the target and the net without problems.
https://www.youtube.com/watch?v=qd8A39i_YbQ

  • World Settings:
    • world.quatNormalizeSkip = 0;
    • world.quatNormalizeFast = false;
    • world.defaultContactMaterial.contactEquationStiffness = 1e128;
    • world.defaultContactMaterial.contactEquationRelaxation = 4;
    • world.gravity.set(0, -9.82, 0);
    • world.solver.iterations = 20;
    • world.solver.tolerance = 0.0;
  • maxSubSteps: 10
  • The sizes are realistic:
    • Ball: Sphere, 0.22m.
    • Wall Box: Box, 0.5m. (Sleeping by default).
    • Average Target; Cylinder, 1m - 2m with 0.4m depth. (Sleeping by default).
    • Net: Box, 0.4m depth.
  • The maximum possible impulse force applied to the ball is 20.
  • Contact Materials:
    • contactEquationStiffness: 1e128,
    • contactEquationRelaxation: 4,
    • frictionEquationStiffness: 1e128,
    • frictionEquationRelaxation: 4

I know that this is a common problem in physics engines, but I'm trying to find the best possible configuration to make the game more playable, because a force of 20 is not too much. And here are my questions:

  • Can I make any change to solve completely or at least partially the problem?
  • Am I doing this right?

Thank you very much for creating and supporting this library! 😄 👍

@ghost
Copy link

ghost commented Feb 24, 2016

@fjguerrero in cannon.js, there is no real support (from what I've seen in the source code) for Continous Collision Detection, or CCD. As you might already know, it basically prevents these kinds of situations from happening, where an object is moving too fast and "tunnels" through objects between simulation steps. Since the physics engine does not support it just yet, the only other way of minimizing the tunneling effects without too much hassle would be to increase the .maxSubSteps property of your world, but that leads to slower simulations (which I'm sure you don't want).

An alternative would be to implement CCD yourself. Fortunatly, since this is only 1 fast-moving ball, the code requirements are very small. All you need is a little raycasting knowledge:

var raycaster = new THREE.Raycaster();

// Must predict next position and check if the ray trajectory if it intersects anything!
function limitSphere(ball, objs){
  var arr;
  raycaster.set(ball.position.clone(), ball.velocity.clone().unit());
  raycaster.far = ball.velocity.length();
  arr = raycaster.intersectObjects(objs);

  if(arr.length){
    ball.position.copy(arr[0].point);
  }
}

This function will only work properly if the velocity already has a direction (it should not be [0,0,0]).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant