Skip to content

Commit a32ede5

Browse files
committed
update VR ray casting examples
1 parent 9459600 commit a32ede5

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

mode/libraries/vr/examples/GenerateRay/GenerateRay.pde

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import processing.vr.*;
22

3+
PVector origin, direction;
34
float[] randomx = new float[5];
45
float[] randomy = new float[5];
6+
float[] randomz = new float[5];
57
VRCamera cam;
68

79
void setup() {
@@ -10,30 +12,45 @@ void setup() {
1012
cam = new VRCamera(this);
1113

1214
for (int i = 0; i < 5; ++i) {
13-
randomx[i] = random(-500, 500);
14-
randomy[i] = random(-500, 500);
15+
randomx[i] = random(-500, 500);
16+
randomy[i] = random(-500, 500);
17+
randomz[i] = random(-100, 100);
1518
}
19+
noStroke();
20+
21+
origin = new PVector(randomx[0], randomy[0], randomz[0]);
22+
direction = new PVector();
1623
}
1724

1825
void draw() {
1926
background(200, 0, 150);
27+
lights();
2028

2129
cam.setPosition(0, 0, 400);
2230
push();
23-
translate(randomx[0], randomy[0]);
31+
translate(randomx[0], randomy[0], randomz[0]);
2432
fill(0, 255, 0);
2533
sphere(30);
26-
rotateZ(millis() / 1000);
27-
line(0, 0, 0, 1000, 1000, 0);
28-
PVector origin = new PVector(0, 0, 0);
29-
PVector dest = new PVector(1000, 1000, 0);
34+
35+
int r = floor(random(1, 5));
36+
float rx = randomx[r] - randomx[0];
37+
float ry = randomy[r] - randomy[0];
38+
float rz = randomz[r] - randomz[0];
39+
stroke(0);
40+
strokeWeight(1 * displayDensity);
41+
line(0, 0, 0, rx, ry, rz);
42+
noStroke();
43+
44+
direction.set(rx, ry, rz);
45+
direction.normalize();
3046

3147
pop();
48+
3249
for (int i = 1; i < 5; ++i) {
3350
push();
34-
translate(randomx[i], randomy[i]);
51+
translate(randomx[i], randomy[i], randomz[i]);
3552
fill(255, 0, 0);
36-
if (intersectsSphere(70, origin, dest)) {
53+
if (intersectsSphere(70, origin, direction)) {
3754
fill(0, 0, 255);
3855
}
3956
sphere(70);

mode/libraries/vr/src/processing/vr/VRCamera.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
package processing.vr;
2424

2525
import processing.core.PApplet;
26+
import processing.core.PVector;
2627

2728
public class VRCamera {
2829
protected PApplet parent;
2930
protected VRGraphics graphics;
31+
protected PVector pos = new PVector();
3032

3133
public VRCamera(PApplet parent) {
3234
if (parent.g instanceof VRGraphics) {
@@ -47,7 +49,14 @@ public void noSticky() {
4749
}
4850

4951
public void setPosition(float x, float y, float z) {
50-
graphics.translate(-x, -y, -z);
52+
if (pos.x != x || pos.y != y || pos.z != z) {
53+
graphics.beginCamera();
54+
// Eliminate previous position from matrix stack by applying inverse transformation.
55+
graphics.translate(-pos.x, -pos.y, -pos.z);
56+
pos.set(-x, -y, -z);
57+
graphics.translate(pos.x, pos.y, pos.z);
58+
graphics.endCamera();
59+
}
5160
}
5261

5362
public void setNear(float near) {

0 commit comments

Comments
 (0)