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 & HelpSyntax Questions › how to make a crash
Page Index Toggle Pages: 1
how to make a crash (Read 674 times)
how to make a crash
Feb 6th, 2010, 4:52pm
 
Hi I've only been learning Processing for around a week now along with Arduino. Sorry for the ignorant question...for the sake of learning I'm trying to use a potentiometer to move a little ellipse up and down on the xaxis to dodge rectangles moving along the x axis. I can move the ball up and down and get random rectangles to move by but I can't get them to crash. I'm using the processing.serial* library and I have a Car class (taken from the Learning example on objects)
How do you make the two classes talk to each other? How can you share the variables outside of the methods? Am I close or would I have to totally rewrite this?

Below is my newbie code.
Thanks for any advice!

Code:

Car myCar1;
Car myCar2; // Two objects!

//Person myPerson1;

import processing.serial.*;
PFont font;

Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph


void setup () {
// set the window size:
size(400, 300);
print("game started");

myCar1 = new Car(color(255,0,0),0,100,2);
myCar2 = new Car(color(0,0,255),0,200,1);


myPort = new Serial(this, Serial.list()[1], 9600);

myPort.bufferUntil('\n');
// set inital background:
background(0);
font = loadFont("Aharoni-Bold-48.vlw");
textFont(font, 32);
text("race game", 15, 50);

}
void draw () {
// everything happens in the serialEvent()
myCar1.drive();
myCar1.display();
myCar2.drive();
myCar2.display();
// myCar1.crash();
// myCar2.crash();

}

class Car {
color c;
float xpos;
float ypos;
float xspeed;



// The Constructor is defined with arguments.
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
c = tempC;
xpos = tempXpos;
//ypos = tempYpos;
xspeed = tempXspeed;

}

void display() {
stroke(0);
fill(c);
rectMode(CENTER);

rect(xpos,ypos,20,10);
}

void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
ypos = random(height);

}
}
}


void serialEvent (Serial myPort) {
background(255);
// get the ASCII string:
String inString = myPort.readStringUntil('\n');

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);

// draw the line:
stroke(0);
fill(200, 100, 29);
ellipse(100, inByte, 20, 20);

}



}


//this won't work because it's outside of everything. how can I include it?
if ((inByte ==ypos)&&(xpos==100)) {
print("game over");
ellipse(100, 100, 100, 100);
background(0);
break;
}


Re: how to make a crash
Reply #1 - Feb 7th, 2010, 7:51am
 
This is a FAQ.  Search for 'collision detection' or have a look at the learning section.
Re: how to make a crash
Reply #2 - Feb 7th, 2010, 9:57am
 
Hi,

Thanks for your help. Searching for collision detection helped a lot. I think now I'm stuck, though, on how to do this with Arduino. How would I get the variables that are inside void serialEvent (Serial myPort) outside to interact with the other variables inside the class?

thanks so much for any advice
Re: how to make a crash
Reply #3 - Feb 7th, 2010, 2:50pm
 
Well you might start by making inByte global - i.e. declare it before setup:

Code:

float inByte;

void setup() {//...etc


And don't declare it again in serialEvent - i.e. remove the 'float' from line 88:

Code:
inByte = float(inString);  



Then you can access the value from within draw() and use it to test for collisions...
Page Index Toggle Pages: 1