Skip to content

Language Settings

Non Orthogonal Reflection

This is a port by David Blitz of the "Reflection 1" example from processing.org/examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//Position of left hand side of floor
let base1;
//Position of right hand side of floor
let base2;
//Length of floor
//let baseLength;
// Variables related to moving ball
let position;
let velocity;
let r = 6;
let speed = 3.5;
function setup() {
  createCanvas(710, 400);
  fill(128);
  base1 = createVector(0, height - 150);
  base2 = createVector(width, height);
  //createGround();
  //start ellipse at middle top of screen
  position = createVector(width / 2, 0);
  //calculate initial random velocity
  velocity = p5.Vector.random2D();
  velocity.mult(speed);
}
function draw() {
  //draw background
  fill(0, 12);
  noStroke();
  rect(0, 0, width, height);
  //draw base
  fill(200);
  quad(base1.x, base1.y, base2.x, base2.y, base2.x, height, 0, height);
  //calculate base top normal
  let baseDelta = p5.Vector.sub(base2, base1);
  baseDelta.normalize();
  let normal = createVector(-baseDelta.y, baseDelta.x);
  let intercept = p5.Vector.dot(base1, normal);
  //draw ellipse
  noStroke();
  fill(255);
  ellipse(position.x, position.y, r * 2, r * 2);
  //move ellipse
  position.add(velocity);
  //normalized incidence vector
  incidence = p5.Vector.mult(velocity, -1);
  incidence.normalize();
  // detect and handle collision with base
  if (p5.Vector.dot(normal, position) > intercept) {
    //calculate dot product of incident vector and base top
    let dot = incidence.dot(normal);
    //calculate reflection vector
    //assign reflection vector to direction vector
    velocity.set(
      2 * normal.x * dot - incidence.x,
      2 * normal.y * dot - incidence.y,
      0
    );
    velocity.mult(speed);
    // draw base top normal at collision point
    stroke(255, 128, 0);
    line(
      position.x,
      position.y,
      position.x - normal.x * 100,
      position.y - normal.y * 100
    );
  }
  //}
  // detect boundary collision
  // right
  if (position.x > width - r) {
    position.x = width - r;
    velocity.x *= -1;
  }
  // left
  if (position.x < r) {
    position.x = r;
    velocity.x *= -1;
  }
  // top
  if (position.y < r) {
    position.y = r;
    velocity.y *= -1;
    //randomize base top
    base1.y = random(height - 100, height);
    base2.y = random(height - 100, height);
  }
}
X

creative commons license