We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Object Colour Swap and Poor Collision Detection
Page Index Toggle Pages: 1
Object Colour Swap and Poor Collision Detection (Read 789 times)
Object Colour Swap and Poor Collision Detection
May 21st, 2005, 12:42pm
 
Hi there!

Been working on this program using simple a-life to send osc messages from processing - have two specific problems I have been stuck with for a week now and can't figure out.

I'm using an object class to represent an ellipse that moves around the screen occasionally sending an osc message and sending a different message when it collides with another ellipse.

Prob no#1:
When I spawn more than 1 ellipse the new ellipse steals the colour from the original ellipse - it replaces it when it is gone.  I figured it might be something to do with private variables not being private but it seems to only swap with the first ellipse - the rest are OK.

Prob no#2:
Collision detection seems to be off - am not too sure about how nested "for" loops work in the draw method but it seems to be registering a spawning (i.e. a collision) at both correct and incorrect instances.

code is below - any help is super appreciated!
][oyd

import osc.*;
import oscP5.*;

/*
* Sound Creatures test code
* by Lloyd Barrett
* 2005
*/

int channel = 5; //population setting fixed at this stage
Life[] bugs = new Life[channel];
int drawCounter;
OscP5 oscP5;
int receiveAtPort;
int sendToPort;
String host;
String oscP5event;

void setup() {
 size(300, 300);
 framerate(10);
 initOsc();
}

void initOsc() {
 receiveAtPort = 12000;
 sendToPort = 57120;
 host = "127.0.0.1";
 oscP5event = "oscEvent";

 oscP5 = new OscP5( this, host, sendToPort, receiveAtPort, oscP5event);
}

void oscEvent(OscIn oscIn) {
 println("received ...");
}

void draw()
{
 background(0);
 //if the bugs are active then update their position and draw them
 for(int i=0; i<bugs.length; i++) {
   if(bugs[i] != null) {
     bugs[i].update();
     if (drawCounter > 100) {
       bugs[i].sendOSCmsg();
       //println("sent");
       drawCounter = 0;
     }
     for(int n = i + 1; n<bugs.length; n++) {
       if (bugs[n] != null) {
         if ((bugs[i].xPos == bugs[n].xPos) || (bugs[i].yPos == bugs[n].yPos)){
           bugs[i].spawn();
           bugs[n].spawn();
           println("spawning " + n + " & " + i);
         }
       }
     }
     //i++;

   }
   drawCounter++;
 }
}

void keyPressed()
{
 //turns the bugs on and off if specific keys are pressed
 if (keyPressed) {
   if(key == 'q') {
     if ( bugs[0] == null) {
       bugs[0] = new Life(0);
     } else {
       bugs[0] = null;
     }
   }
   if(key == 'w') {
     if ( bugs[1] == null) {
       bugs[1] = new Life(1);
     } else {
       bugs[1] = null;
     }
   }
   if(key == 'e') {
     if ( bugs[2] == null) {
       bugs[2] = new Life(2);
     } else {
       bugs[2] = null;
     }
   }
   if(key == 'r') {
     if ( bugs[3] == null) {
       bugs[3] = new Life(3);
     } else {
       bugs[3] = null;
     }
   }
   if(key == 't') {
     if ( bugs[4] == null) {
       bugs[4] = new Life(4);
     } else {
       bugs[4] = null;
     }
   }
 }
}

//========================================================

class Life {
 private int r1a, r1b;
 private float clrB = (random(225) + 25);
 private float clrG = (random(225) + 25);
 private float clrR = (random(225) + 525);
 private int xPos = (int)(random(400));
 private int yPos = (int)(random(400));
 private boolean active = true;
 private int randAmt = 4;
 private int id;

 public Life(int id) {
   this.id = id;
 }

 //is the bug active or not?
 void setActive(boolean val) {
   active = val;
 }

 //updates the position
 void update() {
   r1a = (int)random(randAmt*2) - randAmt;
   r1b = (int)random(randAmt*2) - randAmt;
   xPos = xPos - r1a;
   yPos = yPos - r1b;
   if (xPos < 0) {
     xPos = height;
   }

   if(xPos > height) {
     xPos = 0;
   }

   if (yPos  < 0) {
     yPos = width;
   }

   if(yPos > width) {
     yPos = 0;
   }

   this.draw();
 }

 //draws the bug

 void draw() {
   ellipse(xPos, yPos, 20, 20);
   fill(clrR, clrG, clrB, 200);
 }

 //sends OSCmsg based on random 10% variance
 void sendOSCmsg() {
   int callVariance = (int)(random(100));
   if (callVariance < 50) {
     OscMessage oscMsg = oscP5.newMsg("/");
     oscMsg.add(this.id);
     oscMsg.add(this.xPos);
     oscMsg.add(this.yPos);
     oscP5.sendMsg(oscMsg);
     println("OSC sent on channel " + this.id);
   }
 }
 //not spawning yet
 void spawn() {
   //println("spawning " + this.id);
   sendOSCmsg();
 }
}


Page Index Toggle Pages: 1