Hello World!
I am going to try to make a 2d water (with physics) script. But I’m not sure how to do this or where to start.
And i don’t want to re-invent the wheel, of course
Any ideas/tips will be greatly appreciated.
Hello World!
I am going to try to make a 2d water (with physics) script. But I’m not sure how to do this or where to start.
And i don’t want to re-invent the wheel, of course
Any ideas/tips will be greatly appreciated.
I suppose it depends on how realistic you want it to look. I don’t know if this would be the best approach, but you could make it particle based with each particle having certain behaviour, you could even use the unity physics for that. The downside of this is it might be a performance killer, but you could overcome that by only making the top 1/4 layer of the water dynamic and the bottom 3/4 a solid, animated material. I will be keen to see what you come up with and what route you choose to follow.
hmmm… thanks! very helpful. Although I still don’t know where to start. I want it to have good performance, so particles with physics wouldn’t work very well.
The basics of a buoyancy script is to add force equal to the weight of the object * gravity * depth (if less than the water plane) at the point of neutral buoyancy. Like so:
var buoyancyFactor = 1.0;
var waterPlane=0.0;
rigidbody.AddForce(Vector3.up * (rigidbody.mass * Physics.gravity * (transform.position.y < waterPlane ? 1 + Mathf.Abs(transform.position.y - waterPlane) * buoyancyFactor : 0.0);
That’s the easy part and it’s not all that accurate, according to archimedes’ principle. The hard part is deforming the mesh (or particles) according to gravity, force, etc.
I found a way to deform the mesh just like a wave! Using the sinus curve modifier along the x and y axises. But what i need to change is to make it only deform the top vertices. I have an array of the mesh’s vertices: mesh.vertices does anybody know of a way to get only the top vertices?
What you have stumbled on is a basic wave. You build a mesh plane using this and then animate the plane (3d) using a sine wave. Looks kind of wave like. You further that wave by using a cosine wave to develop X movement. This means that as a wave goes up, it moves forward, and as it goes down, it moves back. This gives a pretty realistic wave movement. You can get the wave depth by functionalizing the wave movement and getting a point on it where it should be at any givin time.
This is not, however a true physics water simulation.
I saw where someone else had asked about this and was pointed to a MetaBalls for the water look. I have not tried it and don’t know how well it all works.
thanks MisterB!
Update: I decided to use the mesh deformation instead of particles. But I will use particles for splashes and etc.
Right now I am going to work on the buoyancy. Does anybody know how to get the volume of a mesh?
Volume? Aren’t you going 2D? Are you looking for Area?
Water is not easy to simulate because it depends on its interactions with boundaries. What would a ‘water script’ even do? Is it water in a container of some sort? What if the container has holes? What kind of behavior do you want in the water… just buoyancy or also flows?
No it’s just 2.5d waves and buoyancy. the water is just a mesh. It’s not going to be very dynamic.