|
Author |
Topic: ball object collision (Read 1219 times) |
|
hallo
|
ball object collision
« on: Jan 26th, 2005, 2:08pm » |
|
Hi again, I've made this code, but when the ball hit the object, it repeats. But when it misses the object(retangle) it stops, I would like to know if someone knows how to repeat it? Maybe with a if statement? Could anyone help me? //code float x, y = 20; float a, w = 20; float w1=40, h=20, h1=40; void setup() { ellipse(x,y,w,h); size(300,500); rect(a,350,w1,h1); }//end setup void loop() { background(220); ellipse(x,y,w,h); y+=5;//speed of the ball rect(a,400-h1,w1,h1); if((y==400-w-w1 && (a-(w1/2))-(x+(w/2))<0)||(y==400-w-w1 && ((a+(w1/2))- x <-w/2))) { y=0; } }//end method void keyPressed() { if(key=='a'||key==LEFT) { a = a - 5; }else if(key=='d'||key==RIGHT) { a = a + 5; } }//end method
|
|
|
|
moving_ninja
|
Re: ball object collision
« Reply #1 on: Jan 27th, 2005, 6:28am » |
|
When the ball hits something, instead of setting y to equal zero, you have to change the direction in which the value of y will change. It can be easiest to create an integer which can be equal to either 1 or -1. Then when you simulate motion, you add the amount(in your case 5) multiplied by the direction. When a collision occurs, just change the direction to the opposite of what it was before(eg if 1, change to -1). This method can easily be extended by having an x,y and z direction for 3d collisions. Below is your code modified to use this method: //code float x, y = 20; float a, w = 20; float w1=40, h=20, h1=40; //This determines which direction the ball will move in int direction = 1; void setup() { ellipse(x,y,w,h); size(300,500); rect(a,350,w1,h1); }//end setup void loop() { background(220); ellipse(x,y,w,h); y+= 5 * direction;//speed of the ball rect(a,400-h1,w1,h1); if((y==400-w-w1 && (a-(w1/2))-(x+(w/2))<0)||(y==400-w-w1 && ((a+(w1/2))- x <-w/2))) { //When collision with box, change direction direction*=-1; } if(y==0) { //When collision with border, change direction direction*=-1; } }//end method void keyPressed() { if(key=='a'||key==LEFT) { a = a - 5; }else if(key=='d'||key==RIGHT) { a = a + 5; } }//end method hope that helps.
|
http://www.movingninja.com
|
|
|
bsr
|
Re: ball object collision
« Reply #2 on: Jan 27th, 2005, 12:35pm » |
|
i get an out of memory error when running that ninja!
|
http://hippocamp.net
|
|
|
eskimoblood
|
Re: ball object collision
« Reply #3 on: Jan 27th, 2005, 1:32pm » |
|
Try this: Code: //code float x, y = 20; float a, w = 20; float w1=40, h=20, h1=40; //This determines which direction the ball will move in int direction = 1; void setup() { ellipse(x,y,w,h); size(300,500); rect(a,350,w1,h1); }//end setup void loop() { background(220); ellipse(x,y,w,h); y+= 5 * direction;//speed of the ball rect(a,400-h1,w1,h1); if((y==400-w-w1 && (a-(w1/2))-(x+(w/2))<0)||(y==400-w-w1 && ((a+(w1/2))- x <-w/2))) { //When collision with box, change direction direction*=-1; } if(y==0){ //When collision with border, change direction direction*=-1; } }//end method void keyPressed(){ if(key=='a'||key==LEFT){ a = a - 5; }else if(key=='d'||key==RIGHT){ a = a + 5; } } |
|
|
|
|
|
hallo
|
Re: ball object collision
« Reply #4 on: Jan 27th, 2005, 4:31pm » |
|
Thank all of you for the trouble, but thats not i really wanted. Sorry, if i wasn't clear. When the ball hit the square, the ball should dissapear and it should start all over again. When the ball doesn't hit the square, the ball don't dissapear. The ball just follow through the end. And if somenone knows how to do this, it would be great! Its a study project. Ideally would be, when the ball hit the square somehow there would be a point for hitting the object. The score would somewhere on the sketch. Thank you for the trouble.
|
|
|
|
eskimoblood
|
Re: ball object collision
« Reply #5 on: Jan 27th, 2005, 4:42pm » |
|
Did you mean this: edit: I've shortening the code a little bit Code: int ellX=50, ellY = 20, ellW=20, ellH = 20; int rectX=40, rectY=380, rectW=40, rectH=40; int direction = 1; void setup() { size(300,500); rectMode(CENTER_DIAMETER); ellipseMode(CENTER_DIAMETER); } void loop() { rectX=mouseX; rectY=mouseY; ellY+= 5 * direction; background(220); ellipse(ellX,ellY,ellW,ellH); rect(rectX,rectY,rectW,rectH); if(ellY+ellH/2>rectY-rectH/2 && ellY-ellH/2<rectY+rectH/2 && ellX+ellW/2>rectX-rectW/2 && ellX-ellW/2<rectX+rectW/2 || ellY+ellH/2>height || ellY<ellH/2) { direction*=-1; } } |
|
|
« Last Edit: Jan 27th, 2005, 5:06pm by eskimoblood » |
|
|
|
|
hallo
|
Re: ball object collision
« Reply #6 on: Jan 27th, 2005, 5:53pm » |
|
Thank you for the reply eskimoblood!, but it's not what i was thinking. It should be like this: - a ball falls randomly from the top - there is a square at the bottom (it can only move Left to Right, with the keyboard <-- and --> ) - when the square touch/hit the ball, the ball dissapear (the ball DOES NOT not bounce back) - and when the ball dissapeared, the whole process starts all over again - when the ball does not touch/hit the object, nothing happens I hope it's clear this time. Thank you for the trouble...
|
|
|
|
st33d
|
Re: ball object collision
« Reply #7 on: Jan 27th, 2005, 9:51pm » |
|
Just swap my use of java's Rectangle class for your own float version of the same if you're not keen on it. Code: thing one; thing two; float y1; void setup(){ size(200,200); one = new thing(mouseX, 200, 10); two = new thing(0, 95, 10); smooth(); framerate(24); y1 = 0; } void loop(){ background(200); one.update(mouseX, 100); two.update(95,y1); y1 = (y1 + 1)%height; if (one.c.intersects(two.c)){ y1 = 0; } } class thing{ float x,y,s; Rectangle c; thing (float x, float y, float s){ this.x = x; this.y = y; this.s = s; this.c = new Rectangle (int(x),int(y),int(s),int(s)); } void update(float nx, float ny){ x = nx; y = ny; c.x = int(nx); c.y = int(ny); rect (x,y,s,s); } } |
| Is this what you meant? Update: whoops, no it isn't. For the whole non repeat thing why not just set a boolean (boolean dostuff = true) and stick a big fat if around your idea that you only want to work if dostuff is true: if (dostuff){
|
« Last Edit: Jan 27th, 2005, 10:02pm by st33d » |
|
I could murder a pint.
|
|
|
hallo
|
Re: ball object collision
« Reply #8 on: Jan 27th, 2005, 10:30pm » |
|
Thank you st33d, It's good and it's close what i wanted. I've been using processing for almost 2 weeks now, it's a project i have to complete for my study. The only thing it should modify is that the square should move only with the keyboard <-- and -->. And if you would help me, everytime the objects touch/hit somewhere topright of the sketch a point will added. Thank you!
|
|
|
|
|