Glitch in Collision Detection
in
Programming Questions
•
1 year ago
New Year Wishes to all.
Please i have this little Challenge, i got this Code with the help of this forum that drops Objects( actually images) at random. I also have a Stationary Object(an Image also). I want to detect when one of these droping objects Touches my Other Stationary Object.
This is what i have so far, it works(so poorly), and does not detect most of these falling objects when they touch my stationary object
Thanks for the help
- void setup(){
- ...
- for (int i=0; i < nbrParticles; ++i) {
- myFlakes[i] = new Particle();
- }
- }
- void draw(){
- background(109);
- drop();
- }
- class Particle
- {
- float vx, vy, vspin; // velocities - added
- float x, y, spin;
- boolean show ;
- Particle() {
- x = random(width);
- y = random(height);
- show = true;
- vspin = random(-maxSpin, maxSpin);
- spin = random(TWO_PI);
- vx = 0;
- vy = 0;
- }
- void render() {
- pushMatrix();
- translate(x,y);
- rotate(spin);
- if(millis()/2 ==2){
- image(deathBall, -deathBall.width, -deathBall.height);
- }
- else{
- image(deathBall, -deathBall.width, -deathBall.height);
- }
- popMatrix();
- }
- }
- Particle[] myFlakes = new Particle[nbrParticles];
- public void drop(){
- for (int i=0; i < nbrParticles; ++i) {
- Particle f = myFlakes[i];
- if(f.show){
- f.render();
- }
- // application of forces to velocities
- f.vy += random(-drift,+drift); // brownian motion
- f.vx += random(-drift,+drift);
- f.vy += gravity; // gravity
- // velocity affects position
- f.x += f.vx; // add vx to x
- f.y += f.vy; // add vy to y
- // constant spin rate
- f.spin += f.vspin;
- // apply damping (air friction) to movement velocities
- f.vx *= damping; // shortcut for vx = vx * damping
- f.vy *= damping;
- // wrap around
- if (f.y > height+50) {
- f.y = -50;
- }
- if (f.x > width+50) {
- f.x = -50;
- }
- if (f.x < -50) {
- f.x = width+50;
- }
- if(isColiding(floor(marioX), floor(marioY), floor(f.x), floor(f.y))){ //marioX and marioY is the X and Y Location of the Image(the Stationary Object)
- gameOver();
- }
- }
- }
- public boolean isColiding(float ix,float iy,float ix1,float iy2){
- if( floor(iy2) > floor(iy) && floor(ix1)== floor(ix)){
- return true;
- }
- return false;
- }
1