We are about to switch to a new forum software. Until then we have removed the registration on this forum.
i am making a game where you have to catch items as they fall, i managed to get the collision detection to work with the mouse function, but as i am making it to work with arduino i cant get the collision detection to work, code is below, thank you for any help.
import processing.serial.*;
import cc.arduino.*;
import com.tinkerkit.*;
Arduino arduino;
//declare the button
TKButton but;
TKButton but1;
{
arduino = new Arduino(this, Arduino.list()[2], 57600);
}
PFont font1;//font for game over
PFont font2;//font for score and timer
PImage F_icecream;//icecream image
PImage F_apple;//orange image
PImage F_cherry;//cherry image
PImage egg;//bad egg image
PImage ball;//catcher image
PImage cloud1;
PImage cloud2;
PImage cloud3;
PImage dayImg;
PImage nightImg;
float[] y_icecream = new float[2000];//totaly 2000 icecreams
float[] y_apple = new float[2000];//totaly 2000
float[] y_cherry = new float[1800];
float[] y_egg = new float[3000];
float[] x_cloud1 = new float[10];
float[] x_cloud2 = new float[10];
float[] x_cloud3 = new float[10];
float speed = 1;
int diameter =200;
float TUx;
float TUy;
float light=0;
float fade=120;
int time1=120000;
int seconds=120000;//60 seconds;
String secondsBox=nfc(seconds);// tansfer timer number in to text box
String clock = "Game Over!";//game over string
int score=0;//score beging as "0"
String scoreBox=nfc(score);//tansfer score number in to text box
int currentTime = millis();
ball ball1;
int gameState;
{
but = new TKButton(arduino, TK.I0);
but1 = new TKButton(arduino, TK.I1);
}
void setup() {
size(540, 960);
String scoreBox=nfc(score);//display score at first
gameState = 0;
ball1 = new ball (400, 720, 100);
ball = loadImage("ball.png");
cloud1 = loadImage("cloud1.png");
for (int i = 0; i < x_cloud1.length; i++) {
x_cloud1[i] = random(-4500, 790);
}
cloud2 = loadImage("cloud2.png");
for (int i = 0; i < x_cloud2.length; i++) {
x_cloud2[i] = random(-4500, 790);
}
cloud3 = loadImage("cloud3.png");
for (int i = 0; i < x_cloud3.length; i++) {
x_cloud3[i] = random(-4500, 790);
}
//display icecreams in different places
F_icecream = loadImage("icecream.gif");
for (int i = 0; i < y_icecream.length; i++) {
y_icecream[i] = random(-45000, 790);
}
//dispaly oranges in different places
F_apple = loadImage("apple.gif");
for (int i = 0; i < y_apple.length; i++) {
y_apple[i] = random(-60000, 790);
}
//display cherries in different places
F_cherry = loadImage("cherry.gif");
for (int i = 0; i < y_cherry.length; i++) {
y_cherry[i] = random(-60000, 790);
}
//display bad eggs in different places
egg = loadImage("egg.gif");
for (int i = 0; i < y_egg.length; i++) {
y_egg[i] = random(-60000, 790);
}
size(540,960);
println(Arduino.list());
dayImg = loadImage("pic_day.jpg");
nightImg = loadImage("pic_night.jpg");
}
void draw() {
light = arduino.analogRead(3);
println(light);
fade = map(light,50,950,0,255);
tint(255, fade);
image(dayImg, 0, 0, width, height);
tint(255, 255-fade);
image(nightImg, 0, 0, width, height);
int currentTime = millis();
//println(currentTime);
//scoreBox
String scoreBox=nfc(score);//update score box
fill(0);
text(scoreBox, 25, 40, 200, 200);//font for score box
cloud1();
cloud2();
cloud3();
if (currentTime< time1) { //boolean for checking the timer
String secondsBox=nfc(seconds);
fill(0);
text(secondsBox, 520, 40, 200, 200);
//display every fruit
icecream();
apple();
cherry();
egg();
fill(0);
text(scoreBox, 25, 40, 200, 200);
ball1.display();
ball1.keyPressed();
}
}
//cloud function
void cloud1() {
for (int i = 0; i < x_cloud1.length; i++) {
x_cloud1[i] += 0.4;
float y = i * 6;
image(cloud1, x_cloud1[i], y, y*5, y*2);
}
}
void cloud2() {
for (int i = 0; i < x_cloud2.length; i++) {
x_cloud2[i] += 0.3;
float y = i * 6;
image(cloud2, x_cloud2[i], y, i*30, i*15);
}
}
void cloud3() {
for (int i = 0; i < x_cloud3.length; i++) {
x_cloud3[i] += 0.2;
float y = i * 6;
image(cloud3, x_cloud3[i], y, y*5, y);
}
}
//fruits function
void icecream() {
//create icecream arraylist
for (int i = 0; i < y_icecream.length; i++) {
y_icecream[i] += 3;
float x = i * 6;
image(F_icecream, x, y_icecream[i]);
//if icecream be catched
if (x-30<mouseX&&mouseX<x+30 && y_icecream[i]-30<mouseY&&mouseY<y_icecream[i]+30) {
score= score+2;//score +2
fill(0);
text(scoreBox, 25, 40, 200, 200);
y_icecream[i]=-100;//that object dispeared
}
}
}
void apple() {
//create apple arraylist
for (int i = 0; i < y_apple.length; i++) {
y_apple[i] += 5;
float x = i * 6;
image(F_apple, x, y_apple[i]);
//if orange be catched
if (x-30<mouseX&&mouseX<x+30 && y_apple[i]-30<mouseY&&mouseY<y_apple[i]+30) {
score= score+1;//score+1
fill(0);
text(scoreBox, 25, 40, 200, 200);
y_apple[i]=-100;//that object dispeared
}
}
}
void cherry() {
//create cherries arraylist
for (int i = 0; i < y_cherry.length; i++) {
y_cherry[i] += 4;
float x = i * 6;
image(F_cherry, x, y_cherry[i]);
//if orange be catched
if (x-30<mouseX&&mouseX<x+30 && y_cherry[i]-30<mouseY&&mouseY<y_cherry[i]+30) {
score= score+5;
fill(0);
text(scoreBox, 25, 40, 200, 200);
y_cherry[i]=-100;//that object dispeared
}
}
}
void egg() {
//create bad egg arraylist
for (int i = 0; i < y_egg.length; i++) {
y_egg[i] += 8;//egg drop speed
float x = i * 6;
image(egg, x, y_egg[i]);
//if orange be catched
if (x-40<mouseX&&mouseX<x+40 && y_egg[i]-40<mouseY&&mouseY<y_egg[i]+40) {
time1=0;
y_egg[i]=-100;//that object dispeared
}
}
}
void display() {
fill(255, 0, 0, 70);
noStroke();
fill(255);
noStroke();
}
void reset() {
}
class ball {
float x;
float y;
float speed;
float r; //radius
ball(float tempX, float tempY, float tempR) {
x = tempX;
y = tempY;
r = tempR;
speed = 0;
}
void change() {
}
void display() {
noStroke();
image(ball, x, y, r, r);
}
//key commands
//ball flies off page in response to key command
void keyPressed() {
if (key == CODED) {
}
if (but.read ()== TK.HIGH) {
x = x+5;
if (x >= width - 25) {
x = width - 25;
}
println(but.read());
} else if (but1.read() == TK.HIGH) {
x = x-5;
if (x <= 25) {
x = 25;
}
println(but1.read());
}
}
void tick() {
seconds-=5;
}
}
Answers
where's your mouse collision detection? Which lines?
What have you tried? in which lines?
Since you have only one ball, I assume you want to catch with the ball either an egg or a cherry etc.; is that correct? You don't say.
Anyway, for collision detection you have to loop over all cherrys, eggs and so on and test against ball position:
Remark:
Later you want to have an item class for all egg and icecream etc... since at the moment, the x-pos is always the same for them...
The item class would look like the ball class just with an PImage in it.
You can have an ArrayList of that class then.
;-)
thank you for your help really appreciate it :) however i am no getting an error saying:
ball.x cannot be resolved or is not a field.
ball1.x
You have both a ball class and a ball variable (which is not an instance of the class!). You should stick to the convention of naming classes with an initial uppercase letter: class Ball, variable ball, etc.
so i have done as suggested and i am getting an error the function display() does not exist. sorry to be a pain i am still learning.
do you have ball1.display();
?
Pls show your code
remark 1
you got one class ball (which you should name Ball or BallClass)
you got one image ball (which you should name ballImage or so)
you got one object ball1 (which name is ok, you could say ballObject)
remark 2
pls note, you can't say ball.display, because ball is the name of the class (or the image); you must use ball1.display() since ball1 (with 1) is your object.
there is a tutorial on OOP (classes & objects) - pls read
we can't run your code since we don't have the images and the arduino with the buttons
this is your stripped down code - keys 4 and 5
;-)
i am still having no luck with it :| i have tried everything suggested above and get new errors is there any way of posting the actual file to show?
post your code here or in dropbox (public)
maybe ask in the arduino forum http://arduino.cc/en
https://www.dropbox.com/s/27m0gixd8f5wa43/sketch_150102990oop2.zip?dl=0 i have changed it so you don't need the arduino as suggested. :)
hmm...
the thing is, your images are much bigger than they seem to be...
they have a kind of transparent background around them
so their x,y position is in fact far left and above each image.
I countered that by saying ´imageMode(CENTER);´ which makes it so that x,y is in the middle / center of each image.
You can also cut the frame that is around each image in a separate graphics program like irfanview.
it works now for the eggs. I had to take out ´time = 0;´ though; that froze everything.
instead of the collision approach with > and && etc. you might want to try rectangle collision (but actually I think the former approach is good enough).
Best, Chrisir ;-)
wow thank you so much for all your time and effort that works perfect! really appreciate it all chrisir :)
great!