Bouncing a sphere inside a box

I want to make a sphere randomly bouncing INSIDE a box. I’m using physijs but I can’t make it work.
The problem is that I don’t really know what the right way to approach this problem is.

At the beginning I have used a BoxMesh and I tried positioning the sphere inside it but it always went out of the box. It looks like there is no way to keep a sphere inside a box.

So I have built 6 walls. They are basically 6 BoxMesh with size (50, 1, 50) and I positioned them so that they create a cube. Then I tried to keep the sphere inside these walls in the following ways:

  1. I tried to detect collisions and change the sphere’s position in the render function, but it turned out to be too complicated.

  2. Then I tried “applyCentralImpulse” but it was still too complicated because I had to manage the positions.

  3. I know there are “constraints” in physijs, but I am not sure which one I should use.

Is there something in physijs that automatically handles such a scenario?

Hi!
Is it mandatory to use a physics engine?
Maybe using of .reflect() method of THREE.Vector3() is enough?

1 Like

Hi, I need to use a physics engine because I have to intercept collisions to perform other operations as well. But the .reflect method looks like a good idea so I’ll try it.

Thanks!!

It worked perfectly!!! Thanks again