I want these boxes to float on water without them going crazy!
My ideal desires for the boxes:
Here's where I'm at:
As you can see, some of the boxes aren't even resting at the top of the water!
I don't require authentic 100% accurate water physics.
Thanks so much for helping me! 😃
How about placing an invisible object with physics ( immovable ) slightly under the water surface, so boxes will come to rest on top of that and half will still be sticking out. Then apply some minimal physics impulse or force ( at angle of -90 because that is up ) to make them move around a bit, so it looks like they are floating
That's an interesting idea! However, I think it may feel too much like hitting ground ground, and not liquidy?
Perhaps with the provided example file, I need to alter the amount of force that's applied to suit the size? Since it seems that larger boxes need more force applied, and smaller ones less? Perhaps a clamp on the maximum speed is needed too so it doesn't fly out of the water.
maybe put a small invisible line attached/pinned to each box that acts as the point that floats. then just make it so that line cannot go past the water line, even if the box does, that way the bottom half would float and the top half would remain out, then add the same thing at the top to represent the line that they cannot go past upwards, this may give you more control over the amount of movement each box possesses. I mean essentially make invisible, more controllable, objects the things that "float" and set boundaries for their movement, then just pin the boxes to them. I also think that BadMario's suggestion could be combined with this one potentially, maybe? iunno just spitballin' here. If I combined them though I would make it so only the floating box objects and those related to it were affected by the invisible, immovable object under the water so it doesn't affect the player's physics.
It never hurts to go a bit more realistic. Buoyancy force is proportional to the area of the object underwater.
So a very rough calculation utilizing the sprite's bounding box could be:
apply force 0, -k*(sprite.bboxright-sprite.bboxleft)*max(0, sprite.bboxbottom-waterY)
where waterY is the y of the water level and "k" is some number to tune it. You could probably start by setting k to 1 and go from there. I had to fiddle with it quite a bit to get something i was happy with.
The only drawback is no torque is applied so I utilized imagepoints to be the centers of the box divided into four, and doing the approximate force for each. It actually gave very pleasing results and long planks would level out. To avoid bouning out of the water I cranked up the damping when in contact with water.
Here's an old topic with a few more ideas:
One was to use sine which may work depending on what you're looking for.
The second was to calculate the actual area under the water, and the Center of mass of that area. It's much more realistic. The box2d example bounces a bit much, but turning up the angular damping helps.
R0J0hound, that was awesome (as always!)... but what fun is it if you can't throw the boxes around?
I added drag and drop, some waves (and walls to keep the boxes from floating away)... oh, and a little nudge occasionally to keep them bobbing.
you can get my tweaked version of R0J0hound's capx here: https://www.rieperts.com/games/forum/buoyancy.capx
Thanks for the help everyone!
R0J0hound thanks, that's very helpful, really appreciate the work you've done here!
I only have a small understanding of physics, but it seemed like an interesting approach to be applying the force even when it's not in water! The only thing I'm not sure about this method is that I may have different ponds of water at varying Y positions in the level, so I'm not sure if this method would still work in this situation because it's assuming there's only 1 piece of water?
I was still finding that boxes would bounce out of the water so one solution I did here was to slow the velocity down on the initial contact with water.
download modified capx
I updated your capx to work with multiple water sections... just had to move the "Repeat 4 times" line to be under the "is overlapping water" section so that it uses the correct water y value.
this way, (with the for each box) is probably slightly less efficient because it has to do each box independently - including the repeat 4 times code. R0j0hound's way did them all at the same time. As long as there are not too many boxes, it probably wont make much of a difference.
you can get my file here: https://www.rieperts.com/games/forum/waterfloat2.capx
oh, I added the drag and drop too, because it is so much fun! :-)
Sweet, this feels like it's working pretty well! 😃
Develop games in your browser. Powerful, performant & highly capable.
Very cool tweak!