Google Cardboard: Changing colour of cube based on where I am looking

Hi all,

I have drawn a cube in the scene and I am trying to change the colour of the cube based on where I am looking. Currently I am just trying to measure the distance from the reticle position and the position of the cube. It appears to change the colour once but then never changes it again. Am I using the calculate function incorrectly? Or is there some other facepalm I have missed? Thanks.

import processing.vr.*;
PGraphicsVR p = new PGraphicsVR();
float retX, retY, x, y, z;
color col = color(0, 150, 255);
float d;

void setup() {
  fullScreen(MONO);
  x = width/2;
  y = height/2;
  z = -650;
}

void calculate() {
  //this function is called once per frame immediately before draw
  //use it to update variable values
  retX = p.cameraX;
  retY = p.cameraY;
}

void draw() {
  //this is called twice per frame, once per eye
  background(185);
  lights();

  d = dist(retX, retY, x, y);
  if (d > 150) col = color(255, 0, 255);
  else if (d < 150) col = color(0, 150, 255);
  println(d);

  fill(col);
  translate(x, y, z);
  box(475);

  drawReticule();
}

void drawReticule() {
  eye();
  fill(255, 200, 0);
  stroke(255, 200);
  strokeWeight(5);
  point(retX, retY, 100);
}

Answers

  • are you sure this gets called automatically: calculate()

  • Sounds like it to me: [Link]

  • So I tested this by placing another set of variables in the 3d space and moved it away, the measurement against those x, y variables works.

    So it would appear that my problem is that the eye(); method is returning a position that is not anything to do with the head position, just the x, y world co-ordinates.

    Is there a way to get the current head position (as in which direction the user is looking) in android mode?

    Thanks.

  • edited May 6

    I have a similar problem and just need the rotation and tilt when moving the head. There is a class called HeadTransform which should do exactly that, but I have no idea how to initiate and use it. Did not found any examples yet. Can anyone help?

    https://developers.google.com/vr/reference/android/com/google/vr/sdk/base/HeadTransform#getEulerAngles%28float%5B%5D

  • @codeanticode, @kfrajer could you shed some light on this?

  • Sorry I can help atm. I suggest you contact the owner of the VR repo. I believe she is in the forum but I am not sure what is his name ID.

    Kf

  • edited May 7

    @himi This page by Andres Colubri might help. He wrote an entire book about Android VR. Click the code examples, those might help you. I intend to study how he's done it to solve my own technological issues.

    Thanks Andres.

Sign In or Register to comment.