Make a billiards game

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
conant
Posts: 7
Joined: Wed Jul 29, 2020 7:31 am

Make a billiards game

Post by conant » Wed Jul 29, 2020 8:04 am

Hello team,

Recently, I have chosen BEPUphysics1 to simulate physics for my billiards game. I have set it up to run but not success in making it realistic. The balls are bouncing unexpectedly and spinning infinitively.

This is my settings:
- Use Entity.ApplyImpulse(spin, force) to strike the cue ball
- Set CollisionResponseSettings.BouncinessVelocityThreshold = 0
- Ball's mass is 0.1, linearDamping is 0.12, angularDamping is 0.45
- Bounciness of ball and bumper is set to 1, table's friction is 1 too

I'm very appreciated if anyone give me some idea about that.

Thanks,
conant

Norbo
Site Admin
Posts: 4910
Joined: Tue Jul 04, 2006 4:45 am

Re: Make a billiards game

Post by Norbo » Wed Jul 29, 2020 6:10 pm

BouncinessVelocityThreshold should be nonzero. A threshold that is too low can introduce excessive tiny bounces, especially when the bounces become too small to properly represent for the time step duration (imagine something that would bounce 120 times a second in reality- a time step duration of 1/60f is too long to capture that motion).

With angular damping that high, the ball should not continue spinning very long at all. Either something is resetting the damping, or something is adding momentum to the ball. Hard to say without more information.

conant
Posts: 7
Joined: Wed Jul 29, 2020 7:31 am

Re: Make a billiards game

Post by conant » Thu Jul 30, 2020 4:19 am

Thanks for your fast replying.

Actually, I need more bounciness for my balls. Currently, I see the balls move like backward after bouncing against the table cushions. If a ball's velocity is low, it even sticks to the cushions although the ball and cushions bounciness is all set to 1. Can I set the balls up to move naturally?

About spinning, it's a my bad expression. It's not spinning infinitively but exceedingly. Is there any parameter to limit that angular velocity? (like maxAngularVelocity in Unity)

Norbo
Site Admin
Posts: 4910
Joined: Tue Jul 04, 2006 4:45 am

Re: Make a billiards game

Post by Norbo » Thu Jul 30, 2020 5:30 pm

Actually, I need more bounciness for my balls. Currently, I see the balls move like backward after bouncing against the table cushions. If a ball's velocity is low, it even sticks to the cushions although the ball and cushions bounciness is all set to 1. Can I set the balls up to move naturally?
Not sure I understand the behavior here. Bounciness set to 1 for all involved objects is the highest physically valid value. There might be other things going on- if you can reproduce the behavior in the demos in a stripped down repro, I could provide more information.
About spinning, it's a my bad expression. It's not spinning infinitively but exceedingly. Is there any parameter to limit that angular velocity? (like maxAngularVelocity in Unity)
There is not, but a maximum angular velocity is physically incorrect and so the lack of it should not make the simulation less realistic. Is something incorrectly applying too much angular velocity? For example, earlier you mentioned 'Entity.ApplyImpulse(spin, force)'- the first parameter is the location at which to apply the impulse, and the second parameter is the impulse itself. If the impulse is applied at a very distant location (for example, from an incorrect 'spin' parameter), that would introduce far more angular velocity due to the long lever arm.

conant
Posts: 7
Joined: Wed Jul 29, 2020 7:31 am

Re: Make a billiards game

Post by conant » Fri Jul 31, 2020 9:31 am

There is not, but a maximum angular velocity is physically incorrect and so the lack of it should not make the simulation less realistic. Is something incorrectly applying too much angular velocity? For example, earlier you mentioned 'Entity.ApplyImpulse(spin, force)'- the first parameter is the location at which to apply the impulse, and the second parameter is the impulse itself. If the impulse is applied at a very distant location (for example, from an incorrect 'spin' parameter), that would introduce far more angular velocity due to the long lever arm.
You're right! I re-checked and saw the spin is provided with a too high value. So it's fixed by reducing it to a low reasonable value (not over the ball radius). Awesome :)
Not sure I understand the behavior here. Bounciness set to 1 for all involved objects is the highest physically valid value. There might be other things going on- if you can reproduce the behavior in the demos in a stripped down repro, I could provide more information.
I attach my physics folder source (can't share all sources with you) and a video demonstrating the problem (can't attach, so I share it here: https://vimeo.com/443317851). Actually, I'm using Unity for the project and is moving to another physics engine (don't use built-in Unity physics engine for some reason)
To make it easier for you to follow, I show my current settings here:
- Create a Sphere for each ball. radius = 0.5, friction = 0, bounciness = 1
- Create a Box for the table floor. width = 12, height = 6.5, length = 0.2, friction = 1, bounciness = 0
- Create a Box for each rail (cushion). width/height is optional, length = 0.1, friction = 0, bounciness = 1
Attachments
Billiards.zip
(939.01 KiB) Downloaded 18 times

Norbo
Site Admin
Posts: 4910
Joined: Tue Jul 04, 2006 4:45 am

Re: Make a billiards game

Post by Norbo » Fri Jul 31, 2020 5:53 pm

Looks like it's working as expected given the friction, inertia, and damping. The ball bounces away from the wall, but is still spinning. The ball's angular momentum is turned into linear momentum by friction with the table surface.

You may want to scale down the inertia tensor. There's a InertiaHelper.InertiaTensorScale field which scales all automatically computed inertia tensors; it defaults to 2.5 to help with stability in some more difficult cases. Try setting it to 1 before any entities are created.

The friction may also require some tuning- the higher it is, the more angular momentum will feed back into linear momentum.

conant
Posts: 7
Joined: Wed Jul 29, 2020 7:31 am

Re: Make a billiards game

Post by conant » Sat Aug 01, 2020 1:42 am

It works like a charm :) Thank you for your explanation!

conant
Posts: 7
Joined: Wed Jul 29, 2020 7:31 am

Re: Make a billiards game

Post by conant » Sun Aug 16, 2020 8:18 am

Hi Norbo,

I currently have a problem implementing an aiming system for the game. I found the ball is moving not exactly as the direction provided to ApplyImpulse(location, direction * force). What parameters cause this? Can you give me some suggestions?

Thanks,
conant

Norbo
Site Admin
Posts: 4910
Joined: Tue Jul 04, 2006 4:45 am

Re: Make a billiards game

Post by Norbo » Sun Aug 16, 2020 5:50 pm

If the location is offset from the center of mass, the impulse will introduce spin, and friction with the table will then turn that into linear momentum. Unless the spin was perfectly aligned, it would result in divergence from the direction.

To accurately predict the physics simulation, you could run a snapshot of the simulation forward. It's not guaranteed to be deterministic unless you jump through a bunch of hoops, but it'll be pretty darn close until high chaos events start happening.

conant
Posts: 7
Joined: Wed Jul 29, 2020 7:31 am

Re: Make a billiards game

Post by conant » Sat Aug 22, 2020 5:09 pm

Hi Norbo, I have fixed the above problem by trying a trick: setting the cue ball position to the correct contact point at the frame it reaches to a target ball. This may not seem one correct coding, but it works :)

Currently, I have another task that is making the physics deterministic across platforms. So I chose this one: https://github.com/sam-vdp/bepuphysics1int. After a lot of trial and error, I finally make it deterministic across 2 devices (one PC, one Android). But there are very strange things that break the deterministic (I don't know why). Suppose I have 2 spaces, it breaks when:
  • Change width/height/length of a box (of both spaces) to another value
  • Add a box in a space but not another space
Even though the boxes in the both cases are not related to a simulation (not contact with a ball)

Can you have some guess on this and give me some clue to debug?

Norbo
Site Admin
Posts: 4910
Joined: Tue Jul 04, 2006 4:45 am

Re: Make a billiards game

Post by Norbo » Sat Aug 22, 2020 6:04 pm

To ensure determinism, all interactions with the engine should be done in exactly the same order. Entity lists being in different orders can be enough to introduce nondeterminism. Assume everything has to be bitwise identical at each step of any process.

This is a result of order-dependent processes, like the solver. Constraint execution order matters, and constraint order can be affected by collision pair generation order, which can be affected by the broad phase topology, which can be affected by entity order, and so on.

conant
Posts: 7
Joined: Wed Jul 29, 2020 7:31 am

Re: Make a billiards game

Post by conant » Sun Aug 23, 2020 5:57 pm

After checking, it's actually that entity list order matters. The problem is solved. Thank you so much for these quick and exact replies!

Post Reply