Tuesday, 5 June 2012

new Android tablet

Microscopic anatomy images on my new Android tablet
My new Android tablet has been fun to program with Processing for Android, once I'd set it up to run via Eclipse it has been straightforward to create the APK files and transfer them to the tablet. There's a good sound hack with apwidgets, solved a lot of problems I'd been having with sound files:

import edu.uic.ketai.inputService.*;
import android.content.pm.ActivityInfo;
import apwidgets.*;

PMediaPlayer player;
KetaiSensorManager sensorManager;
PVector  accelerometer;

int numBalls = 12;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.5;
Ball[] balls = new Ball[numBalls];

void setup()
{
  size(screenWidth, screenHeight);
  orientation(PORTRAIT);
  player = new PMediaPlayer(this); //create new PMediaPlayer
  player.setMediaFile("groupe2_osj.mp3"); //set the file (files are in data folder)

  sensorManager = new KetaiSensorManager(this);
  sensorManager.start();
  smooth();
  noStroke();
  accelerometer = new PVector();

  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls);
  }
}

void draw()
{
  background(0);
  //
  //   text("Accelerometer data:" 
  //  + nf(accelerometer.x, 2, 2) + "/" 
  //  + nf(accelerometer.y, 2, 2) + "/" 
  //  + nf(accelerometer.z, 2, 2), 5, 20);

  for (int i = 0; i < numBalls; i++) {
    balls[i].collide();
    balls[i].move();
    balls[i].display();
  } 
  if (balls[1].x==width) {

    player.start(); //start play back
    player.start(); //start play back
    player.setLooping(true); //restart playback end reached
    player.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0
  }
}


void onAccelerometerSensorEvent(long time, int accuracy, float x, float y, float z)
{
  accelerometer.set(x, y, z);
}

public void mousePressed() { 
  if (sensorManager.isStarted())
    sensorManager.stop(); 
  else
    sensorManager.start(); 
  println("SensorManager isStarted: " + sensorManager.isStarted());
}


class Ball {
  float x, y;
  float diameter;
  float vx = 0;
  float vy = 0;
  int id;
  Ball[] others;

  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
  } 

  void collide() {
    for (int i = id + 1; i < numBalls; i++) {
      float dx = others[i].x - x;
      float dy = others[i].y - y;
      float distance = sqrt(dx*dx + dy*dy);
      float minDist = others[i].diameter/2 + diameter/2;
      if (distance < minDist) { 
        float angle = atan2(dy, dx);
        float targetX = x + cos(angle) * minDist;
        float targetY = y + sin(angle) * minDist;
        float ax = (targetX - others[i].x) * spring;
        float ay = (targetY - others[i].y) * spring;
        vx -= ax;
        vy -= ay;
        others[i].vx += ax;
        others[i].vy += ay;
      }
    }
  }

  void move() {

    float Acc_gravityX = map(accelerometer.x, -10, 10, 0.5, -0.5);
    float Acc_gravityY = map(accelerometer.y, -10, 10, -0.5, 0.5);

    float gravityX = Acc_gravityX;
    float gravityY = Acc_gravityY;

    vy += gravityY;
    vx += gravityX;
    x += vx;
    y += vy;

    if (x + diameter/2 > width) {
      x = width - diameter/2;
      vx *= friction;
    }
    else if (x - diameter/2 < 0) {
      x = diameter/2;
      vx *= friction;
    }
    if (y + diameter/2 > height) {
      y = height - diameter/2;
      vy *= friction;
    } 
    else if (y - diameter/2 < 0) {
      y = diameter/2;
      vy *= friction;
    }
    //println(nf(x, 2, 2)+"   "+nf(y, 2, 2)+"  "+nf(gravityX, 2, 2)+"  "+nf(gravityY, 2, 2)+"  __"+nf(accelerometer.x, 2, 2)+"  ___"+nf(accelerometer.y, 2, 2));
  }

  void display() {
    fill(255, 204);
    ellipse(x, y, diameter, diameter);
  }
}



No comments:

Post a Comment