surfguy66
YaBB Newbies
Offline
Posts: 4
help with motion and detection for shapes
May 24th , 2009, 9:07am
Hello, I'm trying to make a sort of game where you move around a box on the screen with the arrow keys and whenever any part of the box (specifically the borders since as soon as the shapes touch, i want action to happen) touches any part (again, the border) of a randomly generated circle, the box will speed up and a new circle will appear. My problem is jumping into that if statement; how do i set up an if statement to check if any part of the box comes in contact with the circle. Here is my code thus far, thanks for any and all help. float xelip, yelip; boolean wFlag; float xvel = 0; float yvel = 0; float xpos = 400; float ypos = 400; //*************************************************** void setup() { size(800, 800); background(102); smooth(); rectMode(CENTER); rect(xpos,ypos,20,20); xelip=random(5, width-5); yelip=random(5, height-5); } //************************************************* void draw() { if (wFlag == true) { if (keyCode == UP) { xvel=0; yvel=-2; move(); } if (keyCode == LEFT) { xvel=-2; yvel=0; move(); } if (keyCode == DOWN) { xvel=0; yvel=2; move(); } if (keyCode == RIGHT) { xvel=2; yvel=0; move(); } } display(); } //*************************************************** void move() { xpos += xvel; ypos += yvel; if ((xpos+10 >= width) || (xpos-10 <= 0) || (ypos+10 >= height) || (ypos-10 <= 0)) { wFlag=false; xpos=400; ypos=400; display(); } } //************************************************ void display() { background(102); rectMode(CENTER); rect(xpos,ypos,20,20); ellipse(xelip, yelip, 20, 20); } //************************************************** void keyPressed() { if (key == CODED) { if ((keyCode == UP) || (keyCode == LEFT) || (keyCode == DOWN) || (keyCode == RIGHT)) { wFlag = true; } } }