Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

A Kinect Game

März 7, 2012

In einer kleinen Zusammenarbeit entwickelten wir ein Spielt welches per Kinect gestuert werden kann. Das Konzept is Simpel: Der Spieler muss per Hand die fallenden Blöcke treffen um so Punkte zu sammeln. [Bilder]
// Imports
import java.awt.Rectangle;
import fullscreen.*; 

// Settings
int breite = 40;
int hit = 20;
color[] playerColors = {color(255,0,0),color(0,0,255),color(0,255,0),color(255,255,0),color(0,255,255),color(255,0,255)};

// Properties
FullScreen fs;
ArrayList drops;
HandDetector handDetector;
HashMap players;
int step;
int time;
boolean isplay = false;

void setup()
{
  players = new HashMap();
  size(1200,1000);
  fs = new FullScreen(this);
  fs.enter();
  frameRate(30);
  smooth();
  handDetector = new HandDetector();
  handDetector.initialize(this);
}

void draw()
{
  if (isplay)
  {
    // Update Detector
    handDetector.update();
    background(255);
    
    // Draw Drops
    noStroke();
    for (int i = 0; drops.size() > i ; i++)
    {
      Drop drop = (Drop) drops.get(i);
      drop.update();
      fill(0,drop.alpha);
      rect(drop.x,drop.y,drop.width,drop.height);
    }
    // Get List
    HashMap handList = handDetector.getHandList(); 
    
    // Get Iterator
    Iterator<Map.Entry> handIterator = handList.entrySet().iterator();
    
    // Loop
    int i = 1;
    while(handIterator.hasNext())
    {
      // The Player
      Player player;
      
      // Add Player
      if (!players.containsKey(i))
      {
         player = new Player(i,playerColors[i]);
         players.put(i,player);
      } else {
         player = (Player) players.get(i);
      }
      
      // Point Color
      noFill();
      strokeWeight(hit);
      stroke(player.playerColor);
      
      // Get Points
      ArrayList handPoints = (ArrayList) handIterator.next().getValue();
      
      // Get First Vector
      PVector pos = handDetector.convertVector((PVector) handPoints.get(0));
      pos = new PVector(map(pos.x,0,handDetector.getWidth(),0,width),map(pos.y,0,handDetector.getHeight(),0,height));
      
      // Draw Point
      point(pos.x,pos.y);
      
      // Create Hitbox
      Rectangle hitbox = new Rectangle(int(pos.x-hit/2),int(pos.y-hit/2),hit,hit);
      
      // Draw Drops
      for (int ii = 0; drops.size() > ii; ii++)
      {
        Drop drop = (Drop) drops.get(ii);
        if(drop.active && drop.intersects(hitbox))
        {
          player.playerScore += drop.speed;
          drop.die();
        }
      }
      
      i++;
    }
    
    // Player Iterator
    Iterator<Map.Entry> playerIterator = players.entrySet().iterator();
    
    // Get Players
    while(playerIterator.hasNext())
    {
      Player player = (Player) playerIterator.next().getValue();
      
      // Draw
      noStroke();
      fill(player.playerColor);
      textSize(22);
      text("PLAYER "+ player.playerNumber + ": " + player.playerScore, (200*player.playerNumber-100),100);
    }
    
    fill(0);
    text("TIME: "+time,width-200,100);
    
    if(step >= 150) {
      randomDrop();
      step = 1;
    }
    
    // Check Drops
    for (int iii = 0; drops.size() > iii ; iii++)
    {
      Drop drop = (Drop) drops.get(iii);
      if(drop.y > height || drop.died) {
        drops.remove(iii);
        randomDrop();
      }
    }
    
    if(time == 0) {
      isplay = false;
    }
    
    step++;
    time--;
  }
  else
  {
    background(255);
    textAlign(CENTER,CENTER);
    textSize(32);
    fill(0);
    text("HIT ENTER TO START THE MOTHEREFFIN GAME!",width/2,height/2);
    
    // Player Iterator
    Iterator<Map.Entry> playerIterator = players.entrySet().iterator();
    
    int y = height/2+100;
    
    // Get Players
    while(playerIterator.hasNext())
    {
      Player player = (Player) playerIterator.next().getValue();
      text("PLAYER "+ player.playerNumber + ": " + player.playerScore, width/2,y);
      y += 50;
    }
  }
}

// Set Default Settings
void startGame()
{
  drops = new ArrayList();
  randomDrop();
  players = new HashMap();
  step = 1;
  time = 30*60*1;
  isplay = true;
}

// Generate a Random Drop
void randomDrop()
{
  drops.add(new Drop(int(random(0,width-breite)),0, breite));
}

// Key Press Listener
void keyPressed()
{
  switch(keyCode) {
    case ENTER: startGame(); break;
  }
}

class Drop extends Rectangle
{
  int speed;
  int alpha;
  boolean active;
  boolean died;
  
  Drop(int x, int y, int size)
  {
    super(x,y,size,size);
    this.active = true;
    this.speed = int(random(5,20));
    this.alpha = 255;
    this.died = false;
  }
  
  void update() {
    if(active) {
      this.accelerate();
    } else {
      this.alpha -= 20;
    }
    if(this.alpha <= 0) {
      this.died = true;
    }
  }
  
  void accelerate() {
    this.y += this.speed; 
  }
  
  void die() {
    this.speed = 0;
    this.active = false;
  }
}

import SimpleOpenNI.*;

class HandDetector extends PApplet
{
  // Objects
  SimpleOpenNI _context;
  XnVSessionManager _sessionManager;
  XnVFlowRouter _flowRouter;
  HandPointManager _handPointManager; 
 
  // Constructor
  void initialize(PApplet parent) 
  {
    // Initialize NI
    _context = new SimpleOpenNI(parent);
    _context.setMirror(true);
    
    // Start Capture  
    if(_context.enableDepth() == false) {
      println("Can't open the depthMap, maybe the camera is not connected!"); 
      exit();
    }
    
    // Enable Detection
    _context.enableGesture(this);
    _context.enableHands(this);
    _sessionManager = _context.createSessionManager("Click,Wave","RaiseHand");

    // Route
    _handPointManager = new HandPointManager(_context,30);
    _flowRouter = new XnVFlowRouter();
    _flowRouter.SetActive(_handPointManager);
    _sessionManager.AddListener(_flowRouter);
    
    // Update
    _context.update();
    _context.update(_sessionManager);
  }
  
  // Getter
  int getWidth() { return _context.depthWidth(); }
  int getHeight() { return _context.depthHeight(); }
  PImage getImage() { return _context.depthImage(); }
  HashMap getHandList() { return _handPointManager.getHandList(); }
  
  // Update NI
  void update()
  {
    _context.update();
    _context.update(_sessionManager);
  }
  
  // Convert Vector to Real Screen
  PVector convertVector(PVector vector)
  {
    PVector ret = new PVector();
    _context.convertRealWorldToProjective(vector,ret);
    return ret;
  }
  
}

class HandPointManager extends XnVPointControl
{
  // Objects
  SimpleOpenNI _context;
  HashMap _handList;
  int _maxPoints;
  
  // Constructor
  public HandPointManager(SimpleOpenNI context, int maxPoints)
  {
    _context = context;
    _maxPoints = maxPoints;
    _handList = new HashMap();
  }
  
  /* Callbacks */
  
  public void OnPointCreate(XnVHandPointContext cxt)
  {
    addPoint(cxt.getNID(),new PVector(cxt.getPtPosition().getX(),cxt.getPtPosition().getY(),cxt.getPtPosition().getZ()));    
  }
  
  public void OnPointUpdate(XnVHandPointContext cxt)
  {
    addPoint(cxt.getNID(),new PVector(cxt.getPtPosition().getX(),cxt.getPtPosition().getY(),cxt.getPtPosition().getZ()));
  }
  
  public void OnPointDestroy(long nID)
  {
    if(_handList.containsKey(nID)) {
      _handList.remove(nID);
    }
  }
  
  /* Methods */
  
  public ArrayList getHandList(long handId)
  {
    ArrayList curList;
    
    if(_handList.containsKey(handId)) {
      curList = (ArrayList) _handList.get(handId);
    } else {
      curList = new ArrayList();
      _handList.put(handId,curList);
    }
    
    return curList;
  }
  
  public void addPoint(long handId,PVector handPoint)
  {
    ArrayList curList = getHandList(handId);
    
    curList.add(0,handPoint);
    
    if(curList.size() > _maxPoints) {
      curList.remove(curList.size() - 1);
    }
  }
  
  public int getHandsCount()
  {
    return _handList.size();
  }
  
  public HashMap getHandList()
  {
    return _handList;
  }

}


class Player
{
  int playerNumber;
  int playerScore;
  color playerColor;
  
  Player(int n, color c)
  {
    this.playerNumber = n;
    this.playerColor = c;
    this.playerScore = 0;
  }
}