Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all 2896 articles
Browse latest View live

How to set up a menu that disappears and goes to game when clicking with mouse?

$
0
0

Hi,

I'm trying to implement a menu into my simple game. All I am trying to do is to have this rectangle displayed with some text, and when the mouse is pressed, the rectangle disappears and the program goes to the game.

I tried a few different methods. First off, I suspended the loop using noLoop(), because having void loop going and doing all of the different things for the game disrupted my menu. So I thought I would suspend, and so an if statement like this: (menu_condition is initialized to true);

if (menu_condition)
{
 menu();//draw the menu
 noLoop();//pause the loop


 if(mousePressed)
    {
//if we pressed the mouse, than we resume the loop and we no longer enter into menu for the rest of the game
//because menu_condition is set to false
     menu_condition = false;
     loop();
    }
}

However, the problem with this is that we are not looping, so even though we are clicking the mouse, the void loop is paused so the mousePressed is not being registered. I tried doing a while loop instead of "if", and that would freeze. I tried implementing this condition during the loop, but this caused a lot of problems, so it seems to me that keeping the loop static is one of the only ways of effectively doing this (I'm most likely wrong)...

The menu() function just draws a rect with some text...nothing else.

How can I successfully incorporate this menu?

Here is my full void and setup functions:

void setup ()
{
  minim = new Minim(this);
  cue_stick_hit = minim.loadSnippet("stick_cue_hit.wav");
  ball_wall_collision = minim.loadSnippet("ball_wall_collision.wav");
  size(900, 600);//size of processing display window
  frameRate(60); //Sets refresh rate of processing window
  cue.rad = 31.0;//diameter
  cue.col = #e2e9f0;//colour of cue ball


}

void draw ()
{

  table ();
  cue();
  stick();
  position.add(velocity_cue);
 checkBoundaryCollision();
 velocity_cue.mult(friction_factor);
 velocity_check();
 in_the_pocket();

if(boolean_condition == 1)
{
 noLoop();
}
 textSize(14);
 fill(text_color_r,text_color_g,text_color_b);
 text("POWER:"+power_percentage+"%", 100,100);

if (menu_condition)
{
 menu();
 noLoop();
 if(mousePressed)
    {
     menu_condition = false;
     loop();
    }
}
}

Thank you!


Senior Project. Help!! Making Rollovers Stay After being Rolled Over

$
0
0

Hi!

We're working on our senior project and need help making rollovers stay on the screen once they've been rolled over. We're using basic mouseX and mouseY to define where the rollovers appear. The rollovers appear on top of a live camera feed taken from a webcam. We aren't too advanced and aren't using object oriented programming. We're not sure if the answer is a boolean or maybe a function? We just need a small example to put us in the right direction.

If there's any one who can help we'd greatly appreciate it :)

Here's a look at our code if it helps:

//-----------------------------------------------video stuff

import processing.video.*;
Capture video;

//-----------------------------------------------variables
//Instagram
PImage startscreen;
PImage bellatemp;
PImage celestetemp;
//Ccomments
PImage Ccomment1;
PImage Ccomment2;
PImage Ccomment3;
PImage Ccomment4;
PImage Ccomment5;
PImage Ccomment6;
PImage Ccomment7;
PImage Ccomment8;
PImage Ccomment9;
PImage Ccomment10;
PImage Ccomment11;
PImage Ccomment12;
PImage Ccomment13;


//Bcomments
PImage Bcomment1;
PImage Bcomment2;
PImage Bcomment3;
PImage Bcomment4;
PImage Bcomment5;
PImage Bcomment6;
PImage Bcomment7;
PImage Bcomment8;
//Directions
PImage pressB;
PImage pressC;
boolean clicked = false;//mouse clicked
boolean rPressed = false;//r key
PImage restart;
int rollover = 0;


//-----------------------------------------------setup
void setup() {

  size(1152, 864);
  smooth();
  //Video Camera
  video = new Capture (this, Capture.list()[0]);
  video.start();
  printArray(Capture.list());

  //load ALL images in setup
  startscreen = loadImage ("startscreen.png");
  //Templates
  bellatemp = loadImage ("bellatemp.png");
  celestetemp = loadImage ("celestetemp.png");
  //Ccomments
  Ccomment1 = loadImage ("Ccomment1.png");
  Ccomment2 = loadImage ("Ccomment2.png");
  Ccomment3 = loadImage ("Ccomment3.png");
  Ccomment4 = loadImage ("Ccomment4.png");
  Ccomment5 = loadImage ("Ccomment5.png");
  Ccomment6 = loadImage ("Ccomment6.png");
  Ccomment7 = loadImage ("Ccomment7.png");
  Ccomment8 = loadImage ("Ccomment8.png");
  Ccomment9 = loadImage ("Ccomment9.png");
  // Ccomment10 = loadImage ("Ccomment10.png");
  Ccomment11 = loadImage ("Ccomment11.png");
  Ccomment12 = loadImage ("Ccomment12.png");
  Ccomment13 = loadImage ("Ccomment13.png");

  //Bcommnets
  Bcomment1 = loadImage ("Bcomment1.png");
  Bcomment2 = loadImage ("Bcomment2.png");
  Bcomment3 = loadImage ("Bcomment3.png");
  Bcomment4 = loadImage ("Bcomment4.png");
  Bcomment5 = loadImage ("Bcomment5.png");
  Bcomment6 = loadImage ("Bcomment6.png");
  Bcomment7 = loadImage ("Bcomment7.png");
  Bcomment8 = loadImage ("Bcomment8.png");
  pressC = loadImage ("pressC.png");
  pressB = loadImage ("pressB.png");
  restart = loadImage ("restart.png");
}

void captureEvent(Capture video) {
  video.read();
}


//-----------------------------------------------draw
void draw() {

  image(bellatemp, 0, 0);
  image(celestetemp, 0, 0);
  image(restart, 0, 0);

  //Instagram
  image(pressC, 90, 750);
  image(pressB, 600, 750);
  image(video, 0, 0);
  image(startscreen, 0, 0);

  //boolean



  if (clicked)//bella or celeste
  {
    //background rect
    image(video, 0, 0);
    image(pressC, 90, 750);
    image(pressB, 600, 750);

    //line
    strokeWeight(15);
    stroke(#325d80);
    line(width/2, 0, width/2, height);
  }

  if (rPressed)
  {
    image(startscreen, 0, 0);
  }
  if (key == 'c')
  {
    //CAMERA
    image(video, 0, 0);
    image(celestetemp, 0, 0);
    image(restart, 0, 0);

    //ROLLOVERS

    if (mouseY>620 && mouseY<680)
    {
      image(Ccomment2, 0, 0);
    }
    if (mouseY>600 && mouseY<630)
    {
      image(Ccomment1, 0, 0);
    }
    if (mouseY>500 && mouseY<520)
    {
      image(Ccomment4, 0, 0);
    }
    if (mouseY>183 && mouseY<210)
    {
      image(Ccomment6, 0, 0);
    }
    if (mouseY>352 && mouseY<390)
    {
      image(Ccomment7, 0, 0);
    }
    if (mouseY>407 && mouseY<430)
    {
      image(Ccomment8, 0, 0);
    }
    if (mouseY>275 && mouseY<330)
    {
      image(Ccomment9, 0, 0);
    }
    if (mouseY>370 && mouseY<390)
    {
      image(Ccomment11, 0, 0);
    }
  }

  if (key == 'b')
  {
    image(video, 0, 0);
    image(bellatemp, 0, 0);
    image(restart, 0, 0);

    if (mouseY>250 && mouseY<275)

    {
      image(Bcomment1, 0, 0);
    }
    if (mouseY>285 && mouseY<340)
    {
      image(Bcomment2, 0, 0);
    }
    if (mouseY>426 && mouseY<460)
    {
      image(Bcomment4, 0, 0);
    }
    if (mouseY>480 && mouseY<510)
    {
      image(Bcomment5, 0, 0);
    }
    if (mouseY>500 && mouseY<525)
    {
      image(Bcomment6, 0, 0);
    }
    if (mouseY>550 && mouseY<578)
    {
      image(Bcomment7, 0, 0);
    }
    if (mouseY>618 && mouseY<655)
    {
      image(Bcomment8, 0, 0);
    }
  }
}


//-----------------------------------------------mouse pressed
void mousePressed()
{
  if (clicked==false) {
    clicked=true;
  }
  else
  {
    clicked = false;
    rPressed = false;
  }
}
//-----------------------------------------------key pressed

void keyPressed()
{

  if (key=='r') {
    rPressed=true;
  }
}

Sometimes my character stops collecting the "food"

$
0
0

Hello everyone!

I'm making a snake game where an individual icon collecting "food" but sometimes the icon wont pick up the food and sometimes it will and then will stop at some random point. Is there an obvious part of my code causing this that im missing? Also, I cannot for the life of me figure out how to get the game to have a game over and restart setting when the icon hits the wall. Also, how can I call the "Welcome" screen into the main code to pop up before the game? I'm sorry for all these questions. I've been working on this for 10+ hours and I'm not seeing everything clearly anymore.

_________________________Main Body__________________________

    import ddf.minim.*;
    import ddf.minim.signals.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;

Minim minim;
AudioPlayer player;

Logo B;
Welcome W;
int grid = 35;
int Time;
PImage BH;
PImage SC;
PImage Rink;


PVector Cup;


void setup() {

  size(800, 422);
  B = new Logo();
  W= new Welcome();
  frameRate(8);
  pickLocation();
  BH = loadImage("BH.png");
  SC = loadImage("SC.png");
  Rink = loadImage("Rink.jpg");

  minim = new Minim( this );

  player = minim.loadFile("CD.mp3");
  player.play();
}

void pickLocation() {
  int across = width/grid;
  int down = height/grid;
  Cup = new PVector(floor(random(across)), floor(random(down)));
  Cup.mult(grid);
}

void draw() {
  background(Rink);


  if (B.capture(Cup)) {
    pickLocation();
  }
  B.death();
  B.update();
  B.show();


  image(SC, Cup.x, Cup.y, grid, grid);
}

void keyPressed() {

  if (keyCode == UP) {
    B.dir(0, -1);
  } else if (keyCode == DOWN) {
    B.dir(0, 1);
  } else if (keyCode == RIGHT) {
    B.dir(1, 0);
  } else if (keyCode == LEFT) {
    B.dir(-1, 0);
  }
}

_________________Logo__________________

class Logo {
  float x = 0;
  float y = 0;
  float xspeed = 1;
  float yspeed = 0;
  int total = 0;
  boolean endgame=false;
  ArrayList<PVector> hawk = new ArrayList<PVector>();

  Logo() {
  }

  boolean capture(PVector pos) {
    float d = dist(x, y, pos.x, pos.y);
    if (d < 1) {
      return true;
    } else {
      return false;
    }
  }

  void dir(float x, float y) {
    xspeed = x;
    yspeed = y;
  }

  void death() {
    for (int i = 0; i < hawk.size(); i++) {
      PVector pos = hawk.get(i);
      float d = dist(x, y, pos.x, pos.y);
      if (d < 1) {
        text("starting over",100,100);
        total = 0;
        hawk.clear();
      }
    }
  }

  void update() {

    x = x + xspeed*grid;
    y = y + yspeed*grid;

    x = constrain(x, 0, width-grid);
    y = constrain(y, 0, height-grid);
  }

  void show() {
    image(BH, x, y, grid, grid);
  }
}

____________________Welcome______________________

class Welcome {
  PFont Silom;
  boolean buttonpressed;
  int buttonA, buttonB, buttonC, buttonD;

  Welcome()
  {
    Silom = loadFont("Silom.vlw");

    buttonpressed = false;
    buttonC = 215;
    buttonD = 100;
    buttonA = (width-510);
    buttonB = (height-410);


    textFont(Silom, 70);
    if (buttonpressed) {
      println("Begin");
    } else {
      fill(255);
      rect(buttonA, buttonB, buttonC, buttonD);
      fill (0);
      text("START", buttonA+5, buttonB+buttonD-25);
    }
    {

      if (buttonpressed) {
        fill(255, 255, 255, 0);
      } else {
        textFont(Silom, 20);
        text("Press the arrow keys to move around", 210, 235);
        textFont(Silom, 60);
        text("Catch the cup!", 180, 200);
      }
    }
  }

  void mousePressed() {
    if (mouseX>buttonA && mouseX<buttonA+buttonC && mouseY>buttonB && mouseY<buttonB+buttonC)
      buttonpressed = true;
  }
}

Project problems (ParticleSystem / Video)

$
0
0

Hello all I am new to processing and have a question with regards to how I would fix my code.

import processing.video.*;

Capture video;

PImage prevFrame;

float threshold = 50;

void setup() {
  size(600,400);
  video = new Capture(this, 640, 480,30);
  video.start();
  ps = new ParticleSystem(new PVector(width/2, 50));
  prevFrame = createImage(video.width, video.height, RGB);
}

void captureEvent(Capture video) {
  prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
  prevFrame.updatePixels();
  video.read();
}

void draw() {
  background(0);
  image(video, 0, 0);
  ps.addParticle();
  ps.run();
  loadPixels();
  video.loadPixels();
  prevFrame.loadPixels();

  float sumX = 0;
  float sumY = 0;
  int motionCount = 0;

  for (int x = 0; x < video.width; x++ ) {
    for (int y = 0; y < video.height; y++ ) {
      color current = video.pixels[x+y*video.width];

      color previous = prevFrame.pixels[x+y*video.width];

      float r1 = red(current);
      float g1 = green(current);
      float b1 = blue(current);
      float r2 = red(previous);
      float g2 = green(previous);
      float b2 = blue(previous);

      float diff = dist(r1, g1, b1, r2, g2, b2);

      if (diff > threshold){
        sumX += x;
        sumY += y;
        motionCount ++;
      }
    }
  }

  float avgX = sumX / motionCount;
  float avgY = sumY / motionCount;


  smooth();
  noStroke();
  fill(0);
  ellipse(avgX, avgY, 16, 16);
}

  ParticleSystem ps;


class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;

  ParticleSystem(PVector position) {
    origin = position.copy();
    particles = new ArrayList<Particle>();
  }

  void addParticle() {
    particles.add(new Particle(origin));
  }

  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}

class Particle {
  PVector position;
  PVector velocity;
  PVector acceleration;
  float lifespan;

  Particle(PVector l) {
    acceleration = new PVector(0, 0.05);
    velocity = new PVector(random(-1, 1), random(-2, 0));
    position = l.copy();
    lifespan = 255.0;
  }

  void run() {
    update();
    display();
  }

  void update() {
    velocity.add(acceleration);
    position.add(velocity);
    lifespan -= 1.0;
  }

  void display() {
    stroke(255, lifespan);
    fill(255, lifespan);
    ellipse(avgX, avgY, 8, 8);
  }

  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }
}

I have two pieces of code with one being able to detect motion and place a dot where it is detected and the other is a particle system and I am trying to place the particle system where movement is detected but I am unsure as to why it is not working, any help is appreciated!

Where is toxi.geom? I can not find it anyware.

$
0
0

I am trying to install Processing for the MPU6050 and MPUTeapot. MPUTeapot uses the library toxi.geom. But I can not even find the library.

How to create barriers in a code game?

$
0
0

I am new to processing and i currently have an ellipse that floats around the screen and its direction is controlled by the arrow keys. However, I want to make barriers in a maze like form that will block the path of the moving ellipse but, i don't know how to create multiple barriers that the ellipse will not pass through. If anyone can help that would be greatly appreciated. Thanks!

Why wont my game start??

$
0
0

Hello again! I'm sorry for posting so much, but I want to keep updated with the progress I make. So I have my homescreen working. I have my game here in the code too, but why isn't it playing after the homescreen hits the spacebar and it disappears? Any help is very appreciated.

____________Main Body______________

import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer player;

Logo l;
final int stateGame  = 0;
final int stateHome = 1;
int state = stateHome;
int grid = 20;
int buttonA, buttonB, buttonC, buttonD;
int total = 0;
boolean startover = true;
boolean stopgame=false;
boolean buttonpressed;
boolean capture;
PFont Silom;
PImage Rink;
PImage BH;
PImage SC;
PVector Cup;
float x = 0;
float y = 0;
float xspeed = 1;
float yspeed = 0;
ArrayList<PVector> trail = new ArrayList<PVector>();

void setup()
{
  size(600, 631);
  l = new Logo();
  frameRate(8);
  pickLocation();
  Rink = loadImage("Rink.jpg");
  BH = loadImage("BH.png");
  SC = loadImage("SC.png");
  Silom = loadFont("Silom.vlw");
  background(Rink);

  minim = new Minim( this );

  player = minim.loadFile("CD.mp3");
  player.play();
  player.loop();
}

void draw()
{
  switch (state) {

  case stateHome:
    handleStateHome();
    break;

  case stateGame:
    handleStateGame();
    break;
  }
}
void handleStateGame() {
}
void pickLocation() {
  int across = width/grid;
  int down = height/grid;
  Cup = new PVector(floor(random(across)), floor(random(down)));
  Cup.mult(grid);
}
boolean capture(PVector pos) {
  float d = dist(x, y, pos.x, pos.y);
  if (d < 1) {
    return true;
  } else {
    return false;
  }
}
void show() {
  image(SC, x, y, grid, grid);
}

void dir(float x, float y) {
  xspeed = x;
  yspeed = y;
}

void death() {
  for (int i = 0; i < trail.size(); i++) {
    PVector pos = trail.get(i);
    float d = dist(x, y, pos.x, pos.y);
    if (d < 1) {
      state = stateHome;
      total = 0;
    }
  }
}

void update() {
  x = x + xspeed*grid;
  y = y + yspeed*grid;

  {
    x = constrain(x, 0, width-grid);
    y = constrain(y, 0, height-grid);
  }

  if (l.capture(Cup)) {
    pickLocation();
  }
  l.death();
  l.update();
  l.show();
}

void handleStateHome() {
  background(Rink);
  fill(0);
    if (buttonpressed) {
      fill(255, 255, 255, 0);
    } else {
      textFont(Silom, 20);
      text("Press the arrow keys to move around", 100, 330);
      textFont(Silom, 60);
      text("Catch the cup!", 70, 300);
      textFont( Silom, 20);
      text("Press Space to Begin", 180, 360);
    }
  }




void keyPressed() {

  switch (state) {

  case stateGame:
    keyPressedForStateGame();
    break;

  case stateHome:
    keyPressedForStateHome();
    break;
  }
}
void keyPressedForStateHome() {
  if (keyCode == ' ');
    buttonpressed = true;
}
void keyPressedForStateGame() {
  if (key == CODED)
  {
    if (keyCode == UP) {
      l.dir(0, -1);
    } else if (keyCode == DOWN) {
      l.dir(0, 1);
    } else if (keyCode == RIGHT) {
      l.dir(1, 0);
    } else if (keyCode == LEFT) {
      l.dir(-1, 0);
    }
  }
}

_____________________Logo________________________

 class Logo {
  float x = 0;
  float y = 0;
  float xspeed = 1;
  float yspeed = 0;
  int total = 0;
  ArrayList<PVector> tail = new ArrayList<PVector>();

  Logo() {
  }

  boolean capture(PVector pos) {
    float d = dist(x, y, pos.x, pos.y);
    if (d < 1) {
      total++;
      return true;
    } else {
      return false;
    }
  }
  void show() {
    fill(255);
    for (PVector v : tail) {
      rect(v.x, v.y, grid, grid);
    }
    rect(x, y, grid, grid);
  }

  void dir(float x, float y) {
    xspeed = x;
    yspeed = y;
  }

  void death() {
    for (int i = 0; i < tail.size(); i++) {
      PVector pos = tail.get(i);
      float d = dist(x, y, pos.x, pos.y);
      if (d < 1) {
        state = stateHome;
        total = 0;
      }
    }
  }

  void update() {
    x = x + xspeed*grid;
    y = y + yspeed*grid;

    x = constrain(x, 0, width-grid);
    y = constrain(y, 0, height-grid);
  }
}

Particles appear in the same place (Video / Particles)

$
0
0

Hello all I have working piece of motion detection that is supposed to place my particles where it is detected but I am having an error on line 67 ish with the argX and argY saying that they do not exist? Any help is appreciated!

__ import processing.video.*;

Capture video;

PImage prevFrame;

float threshold = 50;

float avgX;
float avgY;

void setup() {
  size(600,400);
  video = new Capture(this, 640, 480,30);
  video.start();
  ps = new ParticleSystem(new PVector(width/2, 50));
  prevFrame = createImage(video.width, video.height, RGB);
  minim = new Minim(this);
  player = minim.loadFile("sandstorm.mp3", 2048);
  player.play();
}

void captureEvent(Capture video) {
  prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
  prevFrame.updatePixels();
  video.read();
}

void draw() {
  background(0);
  image(video, 0, 0);
  loadPixels();
  video.loadPixels();
  prevFrame.loadPixels();

  float sumX = 0;
  float sumY = 0;
  int motionCount = 0;

  for (int x = 0; x < video.width; x++ ) {
    for (int y = 0; y < video.height; y++ ) {
      color current = video.pixels[x+y*video.width];

      color previous = prevFrame.pixels[x+y*video.width];

      float r1 = red(current);
      float g1 = green(current);
      float b1 = blue(current);
      float r2 = red(previous);
      float g2 = green(previous);
      float b2 = blue(previous);

      float diff = dist(r1, g1, b1, r2, g2, b2);

      if (diff > threshold){
        sumX += x;
        sumY += y;
        motionCount ++;
      }
    }
  }

  avgX = sumX / motionCount;
  avgY = sumY / motionCount;


  ps.addParticle(new PVector(argX, argY));
  ps.run();


  smooth();
  noStroke();
}

  ParticleSystem ps;


class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;

  ParticleSystem(PVector position) {
    origin = position.copy();
    particles = new ArrayList<Particle>();
  }

  void addParticle(PVector pos) {
   particles.add(new Particle(pos));
  }

  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}

class Particle {
  PVector position;
  PVector velocity;
  PVector acceleration;
  float lifespan;

  Particle(PVector l) {
    acceleration = new PVector(0, 0.05);
    velocity = new PVector(random(-1, 1), random(-2, 0));
    position = l.copy();
    lifespan = 255.0;
  }

  void run() {
    update();
    display();
  }

  void update() {
    velocity.add(acceleration);
    position.add(velocity);
    lifespan -= 1.0;
  }

  void display() {
    stroke(255, lifespan);
    fill(255, lifespan);
    ellipse(position.x, position.y, 8, 8);
  }

  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }
}_

unexpected token: void error????

$
0
0

Hi,

I've been running into this strange error: unexpected token: void. I've been double checking my code to a previous version of the code that was working properly, and there is very little change, and none of those changes should be causing a problem.

For some reason, processing is highlighting this function when it displays the error message:

    void checkBoundaryCollision ()
    {
      int i = 0;

      if (position.x+(cue.rad/2)+border_width>width)
      {
       velocity_cue.x*=-1;
       i=1;
      }
      else if (position.y+(cue.rad/2)+border_width>height)
      {
        velocity_cue.y*=-1;
        i=1;
      }
      else if (position.x-(cue.rad/2)-border_width<0)
      {
       velocity_cue.x*=-1;
       i=1;
      }
      else if (position.y-(cue.rad/2)-border_width<0)
      {
       velocity_cue.y*=-1;
       i=1;
      }
      if(i!=0)
      {
       ball_wall_collision.play();
       ball_wall_collision.rewind();
      }
    }

And in case it is required for further troubleshooting, this is my loop and setup function:

 void setup ()
    {
      minim = new Minim(this);
      cue_stick_hit = minim.loadSnippet("stick_cue_hit.wav");
      ball_wall_collision = minim.loadSnippet("ball_wall_collision.wav");
      game_over=minim.loadSnippet("game_over.wav");
      background_music = minim.loadSnippet("background_music.wav");
      //mono = loadFont("EdwardianScriptITC48.vlw");
      size(900, 600);//size of processing display window
      frameRate(60); //Sets refresh rate of processing window
      cue.rad = 31.0;//diameter
      cue.col = #e2e9f0;//colour of cue ball


    }

    void draw ()
    {


     if(menu_condition)
      {
        table ();
        menu();
        cue();
        if(mousePressed)
        {
         menu_condition = false;
         //cue_in_pocket = false;
        }
      }
       else if(pause)
        {
        table();
        cue();
        pause();
        textSize(40);
        fill(255);
        text("PAUSED",width/2.5,height/2);
        }

      else
      {
      table();
      cue();
      stick();
      position.add(velocity_cue);
     checkBoundaryCollision();
     velocity_cue.mult(friction_factor);
     velocity_check();
     cue_in_pocket = pocket();
     pause();



     if(cue_in_pocket)
     {
      text("GAME OVER",width/2.75,height/2);
      game_over.play();
      game_over.rewind();
     }
     textSize(14);
     fill(text_color_r,text_color_g,text_color_b);
     text("POWER:"+power_percentage+"%", 100,100);
      }
    }

It doesn't make any sense to me because this is very similar code to a working version, which had the exact same function and was not giving this error at the time... I checked my brackets and that isn't the problem... Is there a specific problem to which this error message points to?

Thanks!

Verlet Physics, Simulation of rigid body with complex geometry

$
0
0

Hello there, I am trying to make a body with complex geometry move with gravity. For this I created a grid of particles that all are "attached" to each other with springs and have gravity behaviour. However the result I am getting is a very 'jumpy' geometry that is malleable and jell-like, instead of the rigid body I need. I guess the way to solve this problem is by changing the parameters of my spring (in the ComplexBody class). Any ideas about what I can do?

Here's the code:

import toxi.physics2d.*;
import toxi.physics2d.behaviors.*;
import toxi.geom.*;

VerletPhysics2D physics;

MyPolygon mypolygon;
ComplexBody complexbody;

PVector anchorPoint;
PVector massCenter;

//POLYGON
PVector[] points;
float shapeScale=2;

int gridScale=12;
int rows=25;
int cols=25;

//RECT
int w=460;
int h=90;
int xRect=0;
int yRect=0;


void setup() {
  size(600, 400, P2D);
  smooth();
  //frameRate(30);

  //VERLET PHYSICS
  // Initialize the physics
  physics=new VerletPhysics2D();
  physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.1)));

  // This is the center of the world
  Vec2D center = new Vec2D(width/2, height/2);
  // these are the worlds dimensions (50%, a vector pointing out from the center in both directions)
  Vec2D extent = new Vec2D(width/4, height/4);

  // Set the world's bounding box
  physics.setWorldBounds(Rect.fromCenterExtent(center, extent));


  //POLYGON
  anchorPoint=new PVector(width/2, height/2);

  points=new PVector[8];
  points[0]= new PVector(0,  0);
  points[1]= new PVector(-50, 0);
  points[2]= new PVector(-50, -10);
  points[3]= new PVector(-75, -20);
  points[4]= new PVector(-75, -40);
  points[5]= new PVector(+25, -40);
  points[6]= new PVector(+25, -20);
  points[7]= new PVector( 0, -10);

  for(PVector a: points){
    a.mult(shapeScale);
    a.add(anchorPoint);
  }

  mypolygon=new MyPolygon(points);
  mypolygon.createGrid();
  massCenter= mypolygon.centerOfMass();

  complexbody= new ComplexBody(mypolygon.grid);
}



void draw() {
  background(0);
  physics.update();




  mypolygon.drawGrid();
 // mypolygon.drawGridTwoD();
  mypolygon.drawInitialShape(50);

  complexbody.display();
  complexbody.createSprings();

  fill(0, 100, 255);
  ellipse(massCenter.x, massCenter.y, 10, 10);

}


class ComplexBody {

  ArrayList<Particle> particles;
  ArrayList<PVector> p;

  ArrayList<VerletSpring2D> springs;

  AttractionBehavior repulsion;

  ComplexBody(ArrayList<PVector> p_) {
    p=p_;
    particles=new ArrayList<Particle>();
    springs=new ArrayList<VerletSpring2D>();
    for (int i=0; i<p.size(); i++) {

      PVector a=p.get(i);
      Particle k= new Particle(a.x, a.y);
      particles.add(k);
      physics.addParticle(k);
    }
  }


  void createSprings() {

    for (int i=0; i<particles.size(); i++) {

      Particle s1=particles.get(i);
      //stroke(255);
      //strokeWeight(1);
      //line(s1.x, s1.y, s1.x+10, s1.y+10);

      for (int j=0; j<particles.size(); j++) {

        if (j!=i) {
          Particle s2=particles.get(j);

          stroke(255,10);
          strokeWeight(1);
          line(s1.x, s1.y, s2.x, s2.y);

          float r= dist(s1.x, s1.y, s2.x, s2.y);
          VerletSpring2D spring=new VerletSpring2D(s1, s2, r, 1);
          physics.addSpring(spring);
        }
      }
    }
  }


  void display() {
    for (Particle p : particles) {

      p.display();
    }
  }
}

class MyPolygon {

  float xAverage;
  float yAverage;

  PVector origin;
  PVector location;

  java.awt.Polygon p;
  PVector[] pts;
  ArrayList <PVector> grid;

  MyPolygon(PVector[] pts_) {
    pts=pts_;
    grid=new ArrayList<PVector>();

    p = new java.awt.Polygon();
    for (PVector a : pts) {
      p.addPoint(int(a.x), int(a.y));
    }
  }

  void createGrid() {

    for (int i=-rows; i<=rows; i++) {
      for (int j=-cols; j<=cols; j++) {
        PVector k=new PVector(anchorPoint.x+ (i*gridScale), anchorPoint.y + (j*gridScale));
        if (p.contains(anchorPoint.x+ (i*gridScale), anchorPoint.y + (j*gridScale))) {
          grid.add(k);
        }
      }
    }
  }

  void drawGrid() {

    fill(0, 0, 200, 200);
    noStroke();
    for (PVector v : grid) {
      ellipse(v.x, v.y, 2, 2);
    }
  }


  PVector centerOfMass() {
    int xcount=0;
    int ycount=0;


    for (int i=0; i<grid.size(); i++) {
      xcount+=grid.get(i).x;
      ycount+=grid.get(i).y;
    }

    xAverage=xcount/grid.size();
    yAverage=ycount/grid.size();

    PVector r=new PVector (xAverage, yAverage);
    return r;
  }


  void drawInitialShape(float alpha) {

    fill(255, alpha);
    beginShape();
    for (PVector a : points) {

      vertex(a.x, a.y);
    }
    endShape();
  }
}

class Particle extends VerletParticle2D {

  Particle(float x, float y) {
    super(x,y);
  }

  void display() {
    fill(255);
    noStroke();
    ellipse(x,y,2,2);
  }
}

Create progress bar for song.

$
0
0

Hello,

I'm trying to create a code that will play a song and have a bar created across the campus in relation to the player's position in the song. Right now I am able to create a line that moves across the screen, but I would like to be able to have a new line created every time the player moves, so it will eventually draw a bar. However, the drawing the bar part has me stumped. Would someone be able to help in how to make it so the line does not disappear?

Here is the code I have so far:

import ddf.minim.*; import ddf.minim.signals.*; import ddf.minim.analysis.*; import ddf.minim.effects.*;

Minim minim; AudioPlayer player;

void setup() { size( 800, 600 );

minim = new Minim( this );

player = minim.loadFile("Little Secrets.mp3");
player.play();

}

void draw() { background( 255 );

float x = map( player.position(), 0, player.length(), 0, width );

stroke( 0 );
strokeWeight (5);
line( x, 0, x, height );

}

void stop() { player.close(); minim.stop(); super.stop(); }

Play sound at a URL?

$
0
0

Is there a way to play an audio file at a URL using the processing sound library? I tried just feeding the URL into the path but it didn't work, the console just complains that the file can't be found. I did try just putting the string into Chrome's URL bar and it changed it because there was a space (The link did work), so I tried putting the new link into the code to no avail. Any ideas/workarounds?

How do I use minim analyzer with an AudioPlayer[] ?

$
0
0

I'm currently working on an end of semester project and my project uses an AudioPlayer[] so that I can play multiple tracks at the press of a key. However, I'm trying to run an oscillator alongside the AudioPlayer[], but my current code for the oscillator lags my sketch to a snail's pace. Any suggestions on what I should do? My current code runs 70 different for loops to run the oscillator, I would like it to run easier.

    for(int i = 0; i < playlist[1].bufferSize() - 1; i++){
    float a1 = map(i,0,playlist[1].bufferSize(), 0, width);
    float a2 = map(i+1,0,playlist[1].bufferSize(),0,width);
    line(a1, height/2 + playlist[1].left.get(i)*100, a2, height/2 + playlist[1].left.get(i)*100);
    line(a1, height/2 + playlist[1].right.get(i)*100, a2, height/2 + playlist[1].right.get(i)*100);

  }

This is the current code I'm using with minim. There is 70 other of these loops in my sketch. Is there any way to make this code work for all 70 AudioPlayer[] arrays, in a shorter less laggy way?

I would post more of this code, but there are over 5000 lines of my sketch.

Problem With ListBox in g4p

Where can I edit contributed library source code?

$
0
0

I'm using Minim library and I need to do one of two things: I need to either extend a class in the library, or make some alterations to the minim source code. The problem is: I have no idea how to start a new project in the processing environment so I can extend the class, and I have no idea where the source code is located. I searched through the processing folder in my file system, but it found no instances of Minim. Help please?


Opening A Webpage Inside Sketch

$
0
0

Is there anyone who can give me idea on how to open webpage inside sketch and not outside the sketch(browsers) with the help of controlP5 ?

Turn audio input sound into color

$
0
0

Hello all,

I am currently having some major trouble attempting to turn input sound into color (using minim fft).

I am hoping to use bandpass filters to set three frequency ranges and then use the numbers that come out of the analysis of the audio input waves to determine the r,g,b fill values of a square.

(Also I am open to any other suggestions for ways to turn input audio into colors.)

Any help is MUCH appreciated! Thank you so much in advance! Cheers!

Katherine

Target VM failed to initialize?

$
0
0

Hi, I just tried to run the AmplitudeRMS sketch from the Processing Examples but Processing quits with: "Could not run the sketch. Target VM failed to initialize." Any ideas why and how to resolve this!?

Thanks

Fix Capture Bug and Game Over Screen. (Runnable Code!)

$
0
0

``Hello everyone!

So I have been making a game similar to the traditional snake game. The difference being that it is on individual shape catching the food rather than it growing. However, after awhile the icon wont pickup the food anymore and I cannot figure out why. I also cant figure out how to create a game over box when the icon hits the wall. Any help would be greatly appreciated! I adjusted the code to not use the images so it could be run by anyone willing to help. Thank you in advance!

_________Main Body____________

import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer player;

Logo B;
int grid = 35;
int Time;
//PImage BH;
//PImage SC;
//PImage Rink;
PFont Silom;

PVector Cup;


void setup() {

  size(600, 631);
  B = new Logo();
  frameRate(8);
  pickLocation();
  //BH = loadImage("BH.png");
  //SC = loadImage("SC.png");
  //Rink = loadImage("Rink.jpg");
  Silom = loadFont("Silom.vlw");


  minim = new Minim( this );

  player = minim.loadFile("CD.mp3");
  player.play();
}

void pickLocation() {
  int across = width/grid;
  int down = height/grid;
  Cup = new PVector(floor(random(across)), floor(random(down)));
  Cup.mult(grid);
}

void draw() {
  background(155);


  if (B.capture(Cup)) {
    pickLocation();
  }
  B.death();
  B.update();
  B.show();

  //image(SC, Cup.x, Cup.y, grid, grid);
  rect(Cup.x, Cup.y, grid, grid);
  fill (0);
  textFont(Silom, 20);
  text("Press the arrow keys", 10, 30);
  text("to move around", 40, 50);
}

void keyPressed() {

  if (keyCode == UP) {
    B.dir(0, -1);
  } else if (keyCode == DOWN) {
    B.dir(0, 1);
  } else if (keyCode == RIGHT) {
    B.dir(1, 0);
  } else if (keyCode == LEFT) {
    B.dir(-1, 0);
  }
  else if (keyCode == ' ');

}

__________Logo_____________

    class Logo {
      float x = 0;
      float y = 0;
      float xspeed = 1;
      float yspeed = 0;
      int total = 0;
      boolean endgame=false;
      ArrayList<PVector> hawk = new ArrayList<PVector>();

      Logo() {
      }

      boolean capture(PVector pos) {
        float d = dist(x, y, pos.x, pos.y);
        if (d < 1) {
          return true;
        } else {
          return false;
        }
      }

      void dir(float x, float y) {
        xspeed = x;
        yspeed = y;
      }

      void death() {
        for (int i = 0; i < hawk.size(); i++) {
          PVector pos = hawk.get(i);
          float d = dist(x, y, pos.x, pos.y);
          if (d < 1) {
            text("starting over",100,100);
            total = 0;
            hawk.clear();
          }
        }
      }

      void update() {

        x = x + xspeed*grid;
        y = y + yspeed*grid;

        x = constrain(x, 0, width-grid);
        y = constrain(y, 0, height-grid);
      }

      void show() {
        //image(BH, x, y, grid, grid);
        rect(x, y, grid, grid);
        fill(255, 150, 20);
      }
    }

setVisible

$
0
0

I have the following program creates cubes and changes them. I am trying to make the cubes invisible until they are activated in updatecells, but I want them part of my overall VBO that is created during setup to help performance. I have tried putting the "setVisible( false )" code in many places, but cannot seem to get it to work. I have tried making each side of the cubes invisible separately, and that did not work either. Any suggestions are welcome.

import processing.opengl.*;
import peasy.*;

PeasyCam cam;
PShape world;
float CS = .25;
int CPS = 10;
int totalcubes = CPS*CPS*CPS;
float offset = CPS/2;
color colors[]= {0x88FF0000,0x8800FF00,0x880000FF,0x88FFFF00,0x8800FFFF,0x88FF00FF};
int dice_orientations[][][]={{{1,6,2,4,5,3},{1,6,4,5,3,2},{1,6,5,3,2,4},{1,6,3,2,4,5}},
                             {{6,1,2,3,5,4},{6,1,3,5,4,2},{6,1,5,4,2,3},{1,6,4,2,3,5}},
                             {{3,4,5,6,2,1},{3,4,6,2,1,5},{3,4,2,1,5,6},{3,4,1,5,6,2}},
                             {{4,3,1,2,6,5},{4,3,2,6,5,1},{4,3,6,5,1,2},{4,3,5,1,2,6}},
                             {{2,5,4,6,3,1},{2,5,6,3,1,4},{2,5,3,1,4,6},{2,5,1,4,6,3}},
                             {{5,2,1,3,6,4},{5,2,3,6,4,1},{5,2,6,4,1,3},{5,2,4,1,3,6}}};
int rcolor;
void setup() {
  size(2100,2100, P3D);
  cam = new PeasyCam(this, 150*CPS);
  noStroke();

  print("Making World   ");
  world = createShape(GROUP);
  for (int i = 0; i < CPS; i++) {
    for (int j = 0; j < CPS; j++) {
      for (int k = 0; k < CPS; k++) {
        MyCube cube = new MyCube(i - offset, j - offset, k - offset);
        world.addChild(cube.output);
        }}print("-");}
  println("   done");} // end setup

 void draw() {
  background(127);
  scale(95);
  shape(world, 0, 0);
  updatecells();
} // end draw


 void updatecells(){
  PShape cube = world.getChild(int(random(totalcubes)));
  color sidecolor;
  int tempfront = int(random(6));
  int temptop = int(random(4));
  int tempside = 0;

  for (int side=0; side < 6; side++) {
    tempside=dice_orientations[tempfront][temptop][side];
    sidecolor=colors[tempside-1];
    for (int V=0; V < 4; V++) {
      cube.setFill(side*4+V ,sidecolor);
     } // end V
   } // end side
 } // end updatecells



// could just be a method, returning PShape
class MyCube {
  PShape output;

  MyCube(float x, float y, float z) { // <- new arguments
    int size = 1;
    output = createShape();
    output.translate(x, y, z); // <- new tanslate
    output.beginShape(QUADS);
    output.fill(colors[0]);
    output.vertex(-CS, CS, CS);output.vertex( CS, CS, CS);output.vertex( CS,-CS, CS);output.vertex(-CS,-CS, CS);
    output.fill(colors[1]);
    output.vertex( CS, CS, CS);output.vertex( CS, CS,-CS);output.vertex( CS,-CS,-CS);output.vertex( CS,-CS, CS);
    output.fill(colors[2]);
    output.vertex( CS, CS,-CS);output.vertex(-CS, CS,-CS);output.vertex(-CS,-CS,-CS);output.vertex( CS,-CS,-CS);
    output.fill(colors[3]);
    output.vertex(-CS, CS,-CS);output.vertex(-CS, CS, CS);output.vertex(-CS,-CS, CS);output.vertex(-CS,-CS,-CS);
    output.fill(colors[4]);
    output.vertex(-CS, CS,-CS);output.vertex( CS, CS,-CS);output.vertex( CS, CS, CS);output.vertex(-CS, CS, CS);
    output.fill(colors[5]);
    output.vertex(-CS,-CS,-CS);output.vertex( CS,-CS,-CS);output.vertex( CS,-CS, CS);output.vertex(-CS,-CS, CS);
    output.setVisible( false );
    output.endShape(CLOSE);
  } // end constructor
}// end MyCube
Viewing all 2896 articles
Browse latest View live