oculus rift experiment 3 - labyrinth

Nikolaus Gradwohl2015-01-02T05:49:22+00:00

For this oculus rift experiment I used the labyrinth generator I wrote a while ago and turned it into a little game. You have to run/drive through a labyrinth and find a yellow post. If you hit it a new labyrinth is generated. Originally I wanted to use the WASD keys to run through the labyrinth but I switched to a usb-gamepad, because its easier for the players to navigate the labyrinth when they hold the controller in their hands instead of having to search for the keyboard.

oculus labyrinth

If you want to run the code make sure you have the gamecontrolplus library installed and change the gamepad name to match your controller

import java.util.Map;
import java.util.HashMap;
import SimpleOculusRift.*;
import org.gamecontrolplus.*;
import net.java.games.input.*;

ControlIO control;
Configuration config;
ControlDevice gpad;

color wallcolor = color(255);

int[][] d;

int N=1;
int S=2;
int E=4;
int W=8;

int targetx, targety;
int WIDTH = 10;
int HEIGHT = 10;

SimpleOculusRift o;
void setup() {
  control = ControlIO.getInstance(this);
  // Find a device - adjust to your controller name
  gpad = control.getDevice("USB 2-Axis 8-Button Gamepad");
  size(1280, 800, OPENGL);
  o = new SimpleOculusRift( this, SimpleOculusRift.RenderQuality_Middle );
  o.setBknColor(200, 200, 255);
  d = generate(WIDTH, HEIGHT);
  targetx = int(random(WIDTH));
  targety = int(random(HEIGHT));
}

int lx;
int ly;

int lx1;
int ly1;

void draw() {
  o.draw();

  float vx = gpad.getSlider("x").getValue();
  float vy = gpad.getSlider("y").getValue();

  if ( abs(vy)>0.1 ) { 
    lx = int(-1*(pos.x)/10.0);
    ly = int(-1*(pos.y)/10.0);    

    if ( lx == targetx && ly ==  targety ) {
      wallcolor= color( random(255));
      d = generate(WIDTH, HEIGHT);
      targetx = int(random(WIDTH));
      targety = int(random(HEIGHT));
    }

    PVector tmp2 = pos.get();    
    tmp2.add( PVector.mult( dir, vy ));
    boolean hit = false;

    lx1 = int(-1.0*(tmp2.x)/10.0);
    ly1 = int(-1.0*(tmp2.y)/10.0);   

    if ( -tmp2.x < 0 ) { hit = true; };
    if ( -tmp2.x > d.length*10 ) { hit = true; };
    if ( -tmp2.y < 0 ) { hit = true; };
    if ( -tmp2.y > d[0].length*10 ) { hit = true; };

    if ((d[lx][ly] & N) == 0 && ly1 < ly) { hit = true; } 
    if ((d[lx][ly] & S) == 0 && ly1 > ly ) { hit = true; }
    if ((d[lx][ly] & E) == 0 && lx1 > lx ) { hit = true; }
    if ((d[lx][ly] & W) == 0 && lx1 < lx ) { hit = true; }

    if ( !hit ) { 
      pos = tmp2;
    }
  }
  if ( abs( vx ) > 0.1 ) {
    dir.rotate( vx/20.0 );
  }
}

PVector pos = new PVector( -5, -5 );
PVector dir = new PVector( .5, 0 );

void onDrawScene(int eye) { 
  beginShape();
  fill(255, 0, 0);
  vertex(0, -1, -2 );
  vertex(1, -1, 1);
  vertex(-1, -1, 1);
  endShape(CLOSE);

  pushMatrix();
  rotateY( dir.heading() + PI/2 );
  translate( pos.x, -2, pos.y );
  float w = 10;
  float h = 10;

  beginShape();
  fill(0, 255, 0);
  vertex(0, 0, 0);
  vertex(d.length * w, 0, 0);
  vertex(d.length * w, 0, d[0].length*w);
  vertex(0, 0, d[0].length*w);
  endShape(CLOSE);
  stroke(0);

  for ( int x = 0; x < d.length; x++) {
    for ( int y = 0; y < d[0].length; y++) {
      if ((d[x][y] & N) == 0) { wall(x * w, y * h, (x+1)*w, y *h ); }
      if ((d[x][y] & S) == 0) { wall(x * w, (y+1) * h, (x+1)*w, (y+1) *h ); }
      if ((d[x][y] & E) == 0) { wall((x+1) * w, y * h, (x+1)*w, (y+1) *h ); } 
      if ((d[x][y] & W) == 0) { wall(x * w, y * h, x*w, (y+1) *h ); }
    }
  }

  pushMatrix();
  translate( targetx * w+5, 5, targety*h +5);
  fill(255, 255, 0);
  box(1, 10, 1);
  popMatrix();

  popMatrix();
}

int[][] generate( int w, int h ) {
  d = new int[w][h];
  for (int i=0; i< w; i++) {
    for ( int j=0; j<h; j++) {
      d[i][j] = 0;
    }
  }
  carve(0, 0, d);
  return d;
}

void carve( int x, int y, int[][] d) {
  int[] dirs = shuffle();
  for ( int i=0; i < 4; i++) {
    int dir = dirs[i];
    int nx = x + (( dir == N || dir == S ) ? 0 : ( dir == E ? 1 : -1 ));
    int ny = y + (( dir == E || dir == W ) ? 0 : ( dir == S ? 1 : -1 ));
    if ( nx >= 0 && nx < d.length && ny >= 0 && ny < d[0].length && d[nx][ny] == 0 ) {
      d[x][y] = d[x][y] | dir; 
      d[nx][ny] = d[nx][ny] | odir(dir);
      carve(nx, ny, d);
    }
  }
}

int[] shuffle() {
  int[] o = new int[] { 
    N, S, E, W
  };
  int res[] = new int[4];
  for ( int i =0; i < 4; i++) {
    int ri = int(random(4)); 
    while ( o[ri] == 0 ) {
      ri = int(random(4));
    } 
    res[i] = o[ri];
    o[ri] = 0;
  }
  return res;
}

int odir( int dir ) {
  if (dir == N) return S;
  if (dir == S) return N;
  if (dir == E) return W;
  if (dir == W) return E;
  return 0;
}


void wall( float x1, float y1, float x2, float y2 ) {
  beginShape();
  fill(wallcolor);
  vertex( x1, 0, y1 );
  vertex( x2, 0, y2 );
  vertex (x2, 4, y2 );
  vertex( x1, 4, y1 );
  endShape(CLOSE);
}
void keyPressed() {
  println("reset head orientation");
  o.resetOrientation();
}
Tweet This! submit to reddit Digg! Tags: | no comments | no trackbacks

See also:

oculus experiment 2 - cubes
oculus experiment - processing sphere
isometric labyrinth
Printed Hexagonal Labyrinth
hexagonal labyrinths in processing

Trackbacks

Comments

Leave a response

Leave a comment