- Home /
Is there a "fluffy" or "squishy" 2D collider?
Hi, I'm trying to simulate a boat in water in 2D. I want the boat to react to waves. I have colliders on the boat and on my water line, but its very ridged. I want the boat to go up when a wave comes, but not exactly in line with the wave.
Is there a way to make the wave colliders softer? so the boat will press into them and move up but not exactly at the same rate as the wave.
Also, is it possible to have mass affect the reaction. So a paper boat would basically ride the wave as is, but the Queen Mary wouldn't realy move at all?
Thanks!!
Answer by Kiwasi · Oct 15, 2014 at 10:26 PM
Buoyancy works by applying an upwards force that is proportional to the volume of liquid displaced by an object. In (very heavy) pseudo code that might work like this
if(collider.lowerbound < water.uperbound){
float percentSubmerged = (collider.boundsHight)/(water.uperbound - collider.lowerbound);
rigidbody.AddForce(Vector3.Up * percentSubmerged * volume);
}
Using this will allow the physics engine to account for the mass of the object and the impacting velocity. It won't do surface tension effects.
This is exactly the answer i was looking for. I knew there must be a great method already figured out.
Also, I'm assu$$anonymous$$g that boundsHeight is a keyword that returns how far the colliders are overlapping? or something along those lines?
thanks again
No, this is all pseudo code. boundsHeight is simply telling you to calculate the height of the collider.
lowerbount, uperbound and volume are all also pseudo code.
If I get bored I might put together some real code for you. I was simply demonstrating the principle.
Or you simply use one of those already out there ;)
Answer by Pyrian · Oct 15, 2014 at 09:51 PM
You could make them into trigger colliders, and have them apply an upward force at the point of contact in their OnTriggerEnter2D and OnTriggerStay2D events. (Be aware that lots of "OnTriggerStay2D" events can really bog a game's performance down.)
I like that idea!!, I suppose for performance you could create a mesh that mimics the waves, and do it that way maybe.