3

Essentially i just want some guidance on how to make the circle bounce off the movable object. Im having trouble and have been trying for three hours and thus have turned to forums for assistance. I have attempted multiple 'if' statements but clearly I am not understanding properly as none are working. Thank you :)

I have been trying for 3 hours to figure this out with different if statements.

float x;
float easing = 1;
float circle_x = 1;
float circle_y = 30;
float rad = 12.5;
float gravity = 0.98;
float move_x = 5;
float move_y = 5;

void setup() {
    size(640, 480);
    frameRate(60);
}

void draw() {
    background(#87CEEB);

    fill(#7cfc00);
    rect(0, 430, 640, 80);

    float targetX = mouseX;
    float dx = targetX - x;
    x += dx * easing;

    fill(#000000);
    rect(x, 400, 30, 30);
    rect(x-20, 390, 70, 10);
    rect(x, 430, 5, 20);
    rect(x+25, 430, 5, 20);


    ellipse(circle_x, circle_y, 25, 25);
    circle_x = circle_x + move_x;
    circle_y = circle_y + move_y;

    if (circle_x > width) {
        circle_x = width;
        move_x = -move_x;
    }

    if (circle_y > height) {
        circle_y = height;
        move_y = -move_y;
    }

    if (circle_x < 0) {
        circle_x = 0;
        move_x = -move_x;
    }

    if (circle_y < 0) {
        circle_y = 0;
        move_y= -move_y;
    }
}

Inserting any variables into if statements and only receiving back: the ball being bounced by my mouse cursor (not the object), glitchy circles and stuttery images.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

2

It has to be checked if the x coordinate of the ball is in the range of the object ( objW is the width of the object):

circle_x > x && circle_x < x + objW

and if the y coordinate of the ball has reached the level of the object (objH is the level of the object and circleR is the radius of the ball):

circle_y > objH - circleR

Further it is important to do the hit test first and to do the test if the object bounce the ground after. A good style is to do this in an else if statement:

int objX1 = -20;
int objX2 = 70;
int objH = 390;
int circleR = 25/2;
if (circle_x > x + objX1  && circle_x < x + objX2 && circle_y > objH - circleR ) {
    circle_y = objH-circleR;
    move_y = -move_y; 
}
else if (circle_y > height) {
    circle_y = height;
    move_y = -move_y;
}
else if (circle_y < 0) {
    circle_y = 0;
    move_y= -move_y;
}  

Further I recommend to calculate the position of the ball first and then draw the ball at ist current position:

float x;
float easing = 1;
float circle_x = 1;
float circle_y = 30;
float rad = 12.5;
float gravity = 0.98;
float move_x = 5;
float move_y = 5;

void setup() {
    size(640, 480);
    frameRate(60);
}

void draw() {
    background(#87CEEB);

    fill(#7cfc00);
    rect(0, 430, 640, 80);

    float targetX = mouseX;
    float dx = targetX - x;
    x += dx * easing;

    circle_x = circle_x + move_x;
    circle_y = circle_y + move_y;
    if (circle_x > width) {
        circle_x = width;
        move_x = -move_x;
    }
    else if (circle_x < 0) {
        circle_x = 0;
        move_x = -move_x;
    }

    int objW = 70;
    int objH = 390;
    int circleR = 25/2;
    if (circle_x > x && circle_x < x + objW && circle_y > objH-circleR ) {
        circle_y = objH-circleR;
        move_y = -move_y; 
    }
    else if (circle_y > height) {
        circle_y = height;
        move_y = -move_y;
    }
    else if (circle_y < 0) {
        circle_y = 0;
        move_y= -move_y;
    }

    fill(#000000);
    rect(x, 400, 30, 30);
    rect(x-20, 390, 70, 10);
    rect(x, 430, 5, 20);
    rect(x+25, 430, 5, 20);

    ellipse(circle_x, circle_y, 25, 25);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Really appreciate the extent of help you went to :) You have greatly assisted me and I thank you for it. I used your advice as guidance and fortunately it now works so thank you very much – Lachlan Muir Apr 07 '19 at 05:25