Greenfoot & Kinect (Parte 3)
Click here to load reader
-
Upload
eduardo-calvillo -
Category
Education
-
view
757 -
download
1
description
Transcript of Greenfoot & Kinect (Parte 3)
Programando el
Kinect con Greenfoot
(3)Eduardo H. Calvillo Gámez
Calibrando Usuarios Clase UserData
Métodos
isTracking();
Booleano, nos indica si esta haciendo “tracking” pues ya termino de calibrar.
isCalibrating();
Booleano, nos indica si esta calibrando.
Clase KinectWorld
getAllUsers();
Arreglo de UserData, todos los usuarios frente al Kinect.
Ejemplo:
Instrucciones.javaMétodo act() & addedToWorld(…)
//Método act()
KinectWorld world = (KinectWorld)getWorld();
boolean trackingAny = false;
boolean calibratingAny = false;
UserData[] us = world.getAllUsers();
for (UserData u : us)
{
trackingAny = trackingAny || u.isTracking();
calibratingAny = calibratingAny || u.isCalibrating();
}
if (us.length > 0 && !trackingAny && ! calibratingAny)
{
setImage(begin);
}
else if (us.length > 0 && !trackingAny && calibratingAny)
{
setImage(calibrating);
}
else
{
setImage(blank);
}
}
protected void addedToWorld(World world)
{
begin = new GreenfootImage("begin.png");
begin.scale(world.getWidth(), world.getHeight());
calibrating = new GreenfootImage("calibrating.png");
calibrating.scale(world.getWidth(), world.getHeight());
blank = new GreenfootImage(1, 1);
setImage(blank);
}
Usuarios Clase UserData
drawStickFigure(…)
drawStickFigure(getBackground(), 60);
getJoint
Clase Joint
Constantes para cada parte del cuerpo
getJoint(Joint.LEFT_HAND) nos regresa la parte
que indicamos (objeto de tipo Joint)
Ejemplo Para checar si alguien levanto la mano izquierda
u.getJoint(Joint.LEFT_HAND).getY() <
u.getJoint(Joint.HEAD).getY()
EjemploDibujando con la mano derecha
import greenfoot.*;
public class Brocha extends Actor
{
public Brocha(int width, int height)
{
setImage(new GreenfootImage(width, height));
getImage().setColor(java.awt.Color.GREEN);
}
public void act()
{
PaintWorld world = (PaintWorld)getWorld();
UserData[] users = world.getTrackedUsers();
for (UserData user : users)
{
Joint rightHand = user.getJoint(Joint.RIGHT_HAND);
getImage().fillOval(rightHand.getX(), rightHand.getY(), 20, 20);
if (user.getJoint(Joint.LEFT_HAND).getY() < user.getJoint(Joint.HEAD).getY())
{
getImage().clear();
}
}
}
}
EjemploPintarMundo
public class PintarMundo extends KinectWorld{
private static final int THUMBNAIL_WIDTH = 80;
private static final int THUMBNAIL_HEIGHT = 60;
…
public PintarMundo()
{
super(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, 1.0, false);
final int width = getWidth();
final int height = getHeight();
addObject(new Brocha(width, height), width/2, height/2);
addObject(new Instructiones(), width/2, height/2);
addObject(new Foto(), width - THUMBNAIL_WIDTH/2, height -
THUMBNAIL_HEIGHT/2);
Label instr = new Label(”Usa la mano derecha para pintar, la izquierda
para borrar", 20);
addObject(instr, instr.getImage().getWidth() /
2, getBackground().getHeight() - (instr.getImage().getHeight() / 2));
}
public void act()
{
super.act();
if (!isConnected())
return;
UserData[] us = getTrackedUsers();
getBackground().setColor(java.awt.Color.WHITE);
getBackground().fill();
for (UserData u: us)
{
//Draws their stick figure:
u.drawStickFigure(getBackground(), 60);
}