I got a problem when trying to make water waves.I only got one jumping surfacepoint on a still water surface.
Here is the code.
lake lake;
void setup()
{
size(400, 500);
lake = new lake();
}
void draw()
{
background(255);
lake.run();
}
class lake{
water surfacePoints[];
int num = width;
PVector position;
lake(){
surfacePoints = new water[num];
for (int i = 0; i < surfacePoints.length; i++)
{
surfacePoints[i] = new water(1,0.1,0.2,new PVector(i,height/2));
}
}
void run(){
//surfacePoints[0].update();
//surfacePoints[0].display();
for (int i = 1; i < surfacePoints.length - 1; i++)
{
surfacePoints[i].neighbourForce(surfacePoints[i-1]);
surfacePoints[i].neighbourForce(surfacePoints[i+1]);
surfacePoints[i].update();
surfacePoints[i].display();
noStroke();
fill(59, 180, 250);
beginShape(QUAD);
vertex(surfacePoints[i].position.x, height);
vertex(surfacePoints[i].position.x, surfacePoints[i].position.y);
vertex(surfacePoints[i].position.x+1, surfacePoints[i+1].position.y);
vertex(surfacePoints[i].position.x+1, height);
endShape(CLOSE);
}
//surfacePoints[num].update();
//surfacePoints[num].display();
surfacePoints[100].updateForce(2);
}
}
class water{
float mass, springConstant, damping;
float positionchange,velocity, acceleration, force;
PVector position, origin;
water(float mass, float springConstant, float damping, PVector origin)
{
this.mass = mass;
this.springConstant = springConstant;
this.damping = damping;
this.origin = origin;
this.position = new PVector();
}
void updateForce(float _force)
{
force = _force - (velocity * damping) - (positionchange * springConstant);
acceleration = force/mass;
velocity += acceleration;
positionchange += velocity;
}
void update()
{
acceleration = springConstant*positionchange +damping*velocity;
acceleration = acceleration/mass;
velocity += acceleration;
positionchange += velocity;
}
void display()
{
this.position.set(0, positionchange);
this.position.add(origin);
}
void neighbourForce(water neighbour){
float displacement = neighbour.position.y-position.y;
force += displacement*springConstant*0.01;
}
}
edditedďź
Sorry for the bad format.
This is what I want to achieve finally.When a surface point is added force,the neighbours receive spring force.Each surfacepoint has damping effect and the result looks like waves.
My problem is that it seems I cannot pass the force through neighbours.