2. März 2013
Ich wollte ein responsive Hundeli machen. Man zieht an einer Schnur und das Hundeli zieht weg. Die Idee hatte ich von den vielen immer kleiner werdenden Hundelis auf der Strasse. Das virtuelle Hundeli will ein Kätzli beissen. Das hundeli bewegt sich unkontrolliert herum. Wenn es das Kätzli packt schreit dieses auf.
![Druck](../../../../files/2013/03/hundeli_TWO.jpg)
_
code:
import processing.serial.*; // Import the Processing Serial Library for communicating with arduino
Serial myPort; // The used Serial Port
int firstValue, secondValue, thirdValue; // fourthValue, fifthValue, ... // add more if needed
float verschiebungX = width/2;
int verschiebungY = height;
float rotAngle = 0.0;
long lasttimepat;
int counter = 0;
int counterb = 0;
int richtung = 1;
int xRect = 0;
int xBall = 0;
int yBall = 0;
PShape rabbitok;
PShape rabbitko;
PShape doggy;
boolean kollision = false;
long letzteZeit;
int anzeigedauer = 1000;
void setup()
{
rabbitok = loadShape("rabbit_cool.svg");
rabbitko = loadShape("rabbit_whaaat.svg");
doggy = loadShape("dog.svg");
smooth();
size(600, 600);
println(Serial.list()); // Prints the list of serial available devices (Arduino should be on top of the list)
myPort = new Serial(this, Serial.list()[0], 9600); // Open a new port and connect with Arduino at 9600 baud
}
float rot =0;
void draw()
{
background(0);
chain();
if (secondValue>100) //&& millis()-lasttimepat>500)
{
counter+=5;
}
if (thirdValue>100) //&& millis()-lasttimepat>500)
{
counter-=5;
}
println(counter);
xRect = -50+counter;
if (abs(xRect-xBall)<50)
{
if (abs(250-yBall)<75)
{
fill(255, 0, 0);
// rect(-50+counter, 250, 50, 50);
kollision = true;
letzteZeit = millis();
println("Kollision");
}
}
if(kollision == true && millis()-letzteZeit < anzeigedauer)
{
shape(rabbitko, -50+counter, 250, 50, 50);
}
if(millis()-letzteZeit > anzeigedauer)
{
kollision = false;
}
if(kollision == false)
{
shape(rabbitok, xRect, 250, 50, 50);
}
}
void serialEvent(Serial myPort) // Is called everytime there is new data to read
{
if (myPort.available() > 0)
{
String completeString = myPort.readStringUntil(10); // Read the Serial port until there is a linefeed/carriage return
if (completeString != null) // If there is valid data insode the String
{
trim(completeString); // Remove whitespace characters at the beginning and end of the string
String seperateValues[] = split(completeString, ","); // Split the string everytime a delimiter is received
for (int i=0; i<seperateValues.length-1; i++)
{
if (i==0)firstValue = int(seperateValues[i]);
if (i==1)secondValue = int(seperateValues[i]);
if (i==2)thirdValue = int(seperateValues[i]);
}
}
}
}
void chain()
{
//rotation chain
pushMatrix();
translate(width/2, height);
if (rot>=100)
{
richtung = -1;
}
if (rot <= -100)
{
richtung = 1;
}
rot = rot+richtung;
pushMatrix();
rotate(radians(rot));
float chainSize= map(firstValue, 1023, 10, 0, -300);
float dog=map(firstValue, 1023, 0, 0, 300);
shape(doggy,0-25, -dog-50, 50, 50);
popMatrix();
xBall = int((sin(radians(rot))*dog)+width/2);
yBall = int((-(cos(radians(rot))*dog)) + height);
stroke(255);
noFill();
bezier(xBall-width/2, yBall-height, xBall-width/1.5, yBall-height-chainSize/4, 0, 0+chainSize/4, 0, 0);
popMatrix();
}