Page 1 of 2

Top-down liquid (water) physics simulation

Posted: Tue Apr 04, 2023 11:02 am
by dusoft
Hello,

can anyone point me to a good lib for simulating liquid, but top down? I have checked the wiki, there are some outdated libs that even throw 404s. I have checked the awesome love2d list, but that does not really have this kind of physics.

Also, I need to work with polygons (probably going to use either older v of HardonCollider or Strike lib, both work well), but I still need help with physics.

Basically I just need to simulate very simple physics for entities moving in water (such as boats etc.).

(PS: I know can probably simulate this via love.physics, but then I would start from the beginning. If there was an existing library, I would prefer using that.)

UPDATE: I am trying to use love.physics and got some primitive concepts working. I do not understand what I should do regarding angles. E.g. boat is always moving to front (if LinearVelocity goes negative). But front means different directions based on different angle. So how do I calculate LinearVelocity X,Y based on this angle? If I don't do it, such boat will always just move up (negative LinearVelocity y), even if it faces down (e.g. decimal angle 180).

Re: Top-down liquid (water) physics simulation

Posted: Tue Apr 04, 2023 5:46 pm
by pgimeno
dusoft wrote: Tue Apr 04, 2023 11:02 amSo how do I calculate LinearVelocity X,Y based on this angle? If I don't do it, such boat will always just move up (negative LinearVelocity y), even if it faces down (e.g. decimal angle 180).
Unless I'm misunderstanding something:

Code: Select all

body:setLinearVelocity(math.cos(angle)*speed, math.sin(angle)*speed)

Re: Top-down liquid (water) physics simulation

Posted: Tue Apr 04, 2023 5:49 pm
by dusoft
pgimeno wrote: Tue Apr 04, 2023 5:46 pm
dusoft wrote: Tue Apr 04, 2023 11:02 amSo how do I calculate LinearVelocity X,Y based on this angle? If I don't do it, such boat will always just move up (negative LinearVelocity y), even if it faces down (e.g. decimal angle 180).
Unless I'm misunderstanding something:

Code: Select all

body:setLinearVelocity(math.cos(angle)*speed, math.sin(angle)*speed)
Unfortunately, no, that does not work. It works, but the polygon "front" has to face right.

I would prefer using body:applyForce(xf, yf) instead as setting Velocity directly defeats the purpose, but I guess I could work with this. Thanks!

Re: Top-down liquid (water) physics simulation

Posted: Tue Apr 04, 2023 6:00 pm
by pgimeno
Hard to give any further advice without any code to look at, sorry.

Re: Top-down liquid (water) physics simulation

Posted: Tue Apr 04, 2023 8:26 pm
by darkfrei
Are you need the physics of hovercraft? It goes not where it's directed, but where it has the speed as rest on inertia.

Re: Top-down liquid (water) physics simulation

Posted: Wed Apr 05, 2023 5:32 am
by dusoft
darkfrei wrote: Tue Apr 04, 2023 8:26 pm Are you need the physics of hovercraft? It goes not where it's directed, but where it has the speed as rest on inertia.
No, I would like to use body:applyForce for speeding up a boat. E.g. holding up key applies force continuously by the physics class of LOVE. I am now a bit stuck how to represent this using this function.

Re: Top-down liquid (water) physics simulation

Posted: Thu Apr 06, 2023 12:11 am
by knorke
Use the same principle.
You have a vector expressed as the boat's direction and the boat's speed.
You want to express the vector as two linear components.
To do this you use sin and cos.

Code: Select all

body:applyForce(math.cos(angle)*speed, math.sin(angle)*speed)
This will not give you boat physics more spaceship physics but you can tweak it from there.
For example adding friction and maybe the ship's rudder should only turn the boat when it is moving forward etc

Re: Top-down liquid (water) physics simulation

Posted: Thu Apr 06, 2023 7:32 am
by dusoft
knorke wrote: Thu Apr 06, 2023 12:11 am Use the same principle.
You have a vector expressed as the boat's direction and the boat's speed.
You want to express the vector as two linear components.
To do this you use sin and cos.

Code: Select all

body:applyForce(math.cos(angle)*speed, math.sin(angle)*speed)
This will not give you boat physics more spaceship physics but you can tweak it from there.
For example adding friction and maybe the ship's rudder should only turn the boat when it is moving forward etc
OK, I am going to try that. I though I wouldn't have to keep speed variable manually as the physics module takes care of that (getLinearVelocity), but alright. Regarding friction - I have already solved this by damping.

On an unrelated note: I really think the physics module should be simplified, all these concepts (separate body, shape etc.) are hard to grasp first even for a programmer with 25+ years of experience (not in games, though). I am getting through, though.

Re: Top-down liquid (water) physics simulation

Posted: Thu Apr 06, 2023 9:21 am
by knorke
ah, "speed" is just bad naming because I copypasted.
It is actually a constant, a better name would be "thrustPower".

Re: Top-down liquid (water) physics simulation

Posted: Thu Apr 06, 2023 10:47 am
by dusoft
knorke wrote: Thu Apr 06, 2023 9:21 am ah, "speed" is just bad naming because I copypasted.
It is actually a constant, a better name would be "thrustPower".
Well noted and makes more sense.