簡體   English   中英

有沒有辦法讓我的圓圈從可移動物體的頂部反彈?

[英]Is there a way for me to make the circle bounce off the top of the movable object?

基本上我只想要一些關於如何使圓圈從可移動物體上反彈的指導。 我遇到麻煩,已經嘗試了三個小時,因此轉向論壇尋求幫助。 我嘗試了多個'if'語句,但很明顯我沒有正確理解,因為沒有一個正在工作。 謝謝 :)

我已經嘗試了3個小時用不同的if語句來解決這個問題。

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;
    }
}

將任何變量插入if語句並僅接收回:球被我的鼠標光標(不是對象),毛刺的圓圈和stuttery圖像反彈。

必須檢查球的x坐標是否在對象的范圍內( objW是對象的寬度):

circle_x > x && circle_x < x + objW

如果球的y坐標已達到物體的高度( objH是物體的高度,而circleR是球的半徑):

circle_y > objH - circleR

此外,重要的是首先進行命中測試並在物體反彈后進行測試。 一個好的風格是在else if語句中執行此操作:

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;
}  

此外,我建議首先計算球的位置,然后在當前位置繪制球:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM