# Trying to do light refraction and reflection

**URL:** https://discourse.processing.org/t/trying-to-do-light-refraction-and-reflection/33035
**Category:** Coding Questions
**Created:** [October 24, 2021, 5:42am UTC](https://discourse.processing.org/t/trying-to-do-light-refraction-and-reflection/33035 "2021-10-24T05:42:34Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ceresnia](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@ceresnia](https://discourse.processing.org/u/ceresnia)
#### Post date: [October 24, 2021, 5:42am UTC](https://discourse.processing.org/t/trying-to-do-light-refraction-and-reflection/33035/1 "2021-10-24T05:42:34Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

I am trying to do a light refraction simulation under the waves.  
I got the wave code so I can make wavey surface  
but I don’t know how to express light on that

this is the wave code

\<  
float particle;  
float particlesSpeed;

float particlesNew;  
float particlesSpeedNew;

float particelGain = 0.00999;  
float forceGain = 0.999; // Speed

int funberOfPArticles = 100;  
float CameraX = 800;  
float CameraY = 1500;  
float CameraZ = 300;

float CameraMovments = 0;  
int CameraMovInt = 0;

float zoom = 1000 / funberOfPArticles;  
float offset = 0;

float waveOrNot = -3;

void setup() {  
size(1280, 780, P3D);

pixelDensity(2);  
particle = new float[funberOfPArticles][funberOfPArticles];  
particlesSpeed = new float[funberOfPArticles][funberOfPArticles];  
particlesNew = new float[funberOfPArticles][funberOfPArticles];  
particlesSpeedNew = new float[funberOfPArticles][funberOfPArticles];

for (int x = 1; x\<funberOfPArticles-1; x++) {  
for (int y = 1; y\<funberOfPArticles-1; y++) {

```
  particle[x][y] = 0.0;
  particlesNew[x][y] = 0.0;
  particlesSpeed[x][y] = 0.0;
  particlesSpeedNew[x][y] = 0.0;
}

```

}

noFill();  
stroke(255);  
}

void draw() {  
camera(CameraX, CameraY, CameraZ, 500, 500, 0.0,  
0.0, 0.0, -1.0);

directionalLight(126, 126, 126, 0, 0.5, -1);

updateMesh();

background(0);  
drawCoorinats();  
drawMesh();  
println(frameRate);  
}

void updateMesh() {

for (int x = 1; x\<funberOfPArticles-2; x++) {  
for (int y = 1; y\<funberOfPArticles-2; y++) {  
//under  
float force1 = 0.0;  
force1 += particle[x-1][y-1] - particle[y];  
force1 += particle[x-1][y] - particle[y];  
force1 += particle[x-1][y+1] - particle[y];  
//over  
force1 += particle[x+1][y-1] - particle[y];  
force1 += particle[x+1][y] - particle[y];  
force1 += particle[x+1][y+1] - particle[y];  
//sidene  
force1 += particle[y-1] - particle[y];  
force1 += particle[y+1] - particle[y];

```
  force1 -= particle[x][y+1] / 8;
  
  //force1 = constrain(force1, -1, 1);
  
  particlesSpeedNew[x][y] = 0.995 * particlesSpeedNew[x][y] + force1/100;

  particlesNew[x][y] = particle[x][y] + particlesSpeedNew[x][y];
}

```

}

for (int x = 1; x\<funberOfPArticles-1; x++) {  
for (int y = 1; y\<funberOfPArticles-1; y++) {

```
  particle[x][y] = particlesNew[x][y];
}

```

}  
if(waveOrNot != 0) {  
offset += .1;  
int MouseXIndex = 2 + (funberOfPArticles-4) \* mouseX / width;  
int MouseYIndex = 2 + (funberOfPArticles-4) \* mouseY / height;  
particlesSpeedNew[MouseXIndex][MouseYIndex] = waveOrNot;  
particlesSpeedNew[MouseXIndex+1][MouseYIndex+1] = waveOrNot;  
particlesSpeedNew[MouseXIndex+1][MouseYIndex] = waveOrNot;  
particlesSpeedNew[MouseXIndex+1][MouseYIndex-1] = waveOrNot;  
particlesSpeedNew[MouseXIndex][MouseYIndex-1] = waveOrNot;  
particlesSpeedNew[MouseXIndex-1][MouseYIndex+1] = waveOrNot;  
particlesSpeedNew[MouseXIndex-1][MouseYIndex] = waveOrNot;  
particlesSpeedNew[MouseXIndex-1][MouseYIndex-1] = waveOrNot;

particle[MouseXIndex][MouseYIndex] = waveOrNot_10;  
particle[MouseXIndex+1][MouseYIndex+1] = waveOrNot_5;  
particle[MouseXIndex+1][MouseYIndex] = waveOrNot_10;  
particle[MouseXIndex+1][MouseYIndex-1] = waveOrNot_5;  
particle[MouseXIndex][MouseYIndex-1] = waveOrNot_10;  
particle[MouseXIndex-1][MouseYIndex+1] = waveOrNot_5;  
particle[MouseXIndex-1][MouseYIndex] = waveOrNot_10;  
particle[MouseXIndex-1][MouseYIndex-1] = waveOrNot_5;

//particlesSpeedNew[40][40] = 100.0 \* sin( offset);  
}  
}

void drawCoorinats() {  
strokeWeight(3);  
stroke(255, 0, 0);  
line(0, 0, 0, 100, 0, 0);

stroke(0, 255, 0);  
line(0, 0, 0, 0, 100, 0);  
}

void drawMesh() {

strokeWeight(1);  
stroke(0, 255, 255);  
for (int x = 0; x\<funberOfPArticles; x++) {  
beginShape();  
for (int y = 0; y\<funberOfPArticles; y++) {  
vertex( (x)\*zoom, (y)\*zoom, particle[y] );  
}  
endShape();  
}  
for (int y = 0; y\<funberOfPArticles; y++) {  
beginShape();  
for (int x = 0; x\<funberOfPArticles; x++) {  
vertex( (x)\*zoom, (y)\*zoom, particle[y] );  
}  
endShape();  
}  
fill(200);  
for (int x = 0; x\<funberOfPArticles-1; x++) {

```
for (int y = 0; y<funberOfPArticles-1; y++) {
  beginShape();
  vertex( (x)*zoom, (y)*zoom, particle[x][y] );
  vertex( (x+1)*zoom, (y)*zoom, particle[x+1][y] );
  vertex( (x+1)*zoom, (y+1)*zoom, particle[x+1][y+1] );
  vertex( (x)*zoom, (y+1)*zoom, particle[x][y+1] );
  endShape();
}

```

}  
}

void keyPressed() {  
if (keyCode == UP) {  
CameraY -= 100;  
} else if (keyCode == DOWN) {  
CameraY += 100;  
}  
if (keyCode == RIGHT) {  
CameraX += 100;  
} if (keyCode == LEFT) {  
CameraX += -100;  
}

if (key==’ ') {  
if(waveOrNot == 0) {  
waveOrNot = -5;  
}else{  
waveOrNot = 0;  
}  
}

if (key == ‘a’) {  
CameraZ += 100;  
} if (key == ‘z’) {  
CameraZ += -100;  
}  
println(" " + CameraX + ", " + CameraY + ", " + CameraZ);  
}

>
