Page 1 of 1

Pool(Billiards) physics (Need help)

Posted: Sun May 19, 2024 11:46 pm
by betobala
Hello everyone, I'm a beginner game developer, trying to make my first game. My idea is based on billiards physics, but I don't have a clue how to make it. Can I get any suggestions on what I should study or any library that could help me with that?

Re: Pool(Billiards) physics (Need help)

Posted: Mon May 20, 2024 10:15 am
by pgimeno
Well, it depends on the degree of sophistication you're after.

If you just want basic bounces, without considering aspects like friction with the cushions or during ball collisions, or whether the ball is hit off-centre to give it a spinning efect, then you could do with a simple bouncing-based scheme which only requires a bit of vector math and very little trigonometry, if any.

If you are after an accurate simulation of effects, frictions during impacts, etc. then you're better off studying dynamics in detail, because there's a lot involved there.

The middle compromise is probably to use the love.physics module, which will not give you a very accurate simulation, but it will be able to include bouncing, cushion friction and (2D) rotational momentum without you worrying about implementing them yourself.

Re: Pool(Billiards) physics (Need help)

Posted: Mon May 20, 2024 12:16 pm
by dusoft
Just use the love.physics module while setting gravitation to 0. That should work.

Re: Pool(Billiards) physics (Need help)

Posted: Mon May 20, 2024 4:52 pm
by pgimeno
Here's a PoC of the love.physics approach. Lots of magic numbers everywhere as I fine-tuned the parameters by trial-and-error, sorry.

As I feared, love.physics doesn't do bouncing too well, and balls sometimes stick to the cushions if they are very slow, even when setting restitution to 1.

Re: Pool(Billiards) physics (Need help)

Posted: Mon May 20, 2024 6:44 pm
by BrotSagtMist
This is already cooler than 90% of what you find in comercial pool games for old consoles.

The problem here is that this kind of game is all about physics, you gotta have to nail it or else its garbage.
So pool is not the smartest starting point to get into game dev.

Re: Pool(Billiards) physics (Need help)

Posted: Mon May 20, 2024 10:16 pm
by dusoft
BrotSagtMist wrote: Mon May 20, 2024 6:44 pm This is already cooler than 90% of what you find in comercial pool games for old consoles.

The problem here is that this kind of game is all about physics, you gotta have to nail it or else its garbage.
So pool is not the smartest starting point to get into game dev.
Anything with complex physics is not suitable as a beginner's project. Although having libraries (or API such love.physics), to make it easier, helps.

Re: Pool(Billiards) physics (Need help)

Posted: Thu May 23, 2024 10:12 pm
by betobala
Thanks all, that helped me a lot!

Re: Pool(Billiards) physics (Need help)

Posted: Fri May 24, 2024 9:24 am
by darkfrei
I've made small example with circles collision detection and static/dynamic resolving: viewtopic.php?p=240339#p240339

Two main collision functions:

Code: Select all

function pushback (collision)
	local particles = collision.particles
	local b1, b2 = particles[1], particles[2]
	local overlap = collision.overlap
	local nx, ny = collision.nx, collision.ny 
	b1.x = b1.x - nx*overlap
	b2.x = b2.x + nx*overlap
	b1.y = b1.y - ny*overlap
	b2.y = b2.y + ny*overlap
end

Code: Select all

function rollback (collision)
	local particles = collision.particles
	local b1, b2 = particles[1], particles[2]
	local nx, ny = collision.nx, collision.ny -- normal
	local tx, ty = -ny, nx -- tangent
	
	local dptan1 = b1.vx*tx + b1.vy*ty
	local dptan2 = b2.vx*tx + b2.vy*ty
	
	local dpnorm1 = b1.vx*nx + b1.vy*ny
	local dpnorm2 = b2.vx*nx + b2.vy*ny
	
	local m1 = (dpnorm1*(b1.mass-b2.mass)+2*b2.mass*dpnorm2)/(b1.mass+b2.mass)
	local m2 = (dpnorm2*(b2.mass-b1.mass)+2*b1.mass*dpnorm1)/(b1.mass+b2.mass)
	
	b1.vx = tx*dptan1 + nx*m1
	b1.vy = ty*dptan1 + ny*m1
	b2.vx = tx*dptan2 + nx*m2
	b2.vy = ty*dptan2 + ny*m2
end