@selennida04

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ブロック崩しの当たり判定について

解決したいこと

ブロック崩しで箱に球の反射後にまた箱にあたり瞬間移動しているように見える。
また反射しないことがある。

ゲームつくり初心者で当たり判定の組み方がわからないのでわかりやすく修正お願いします。

Javascript

export class Boxes{
    constructor(x,y,width,height){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.isHit = false;
    }
    ball_judge(player_ball){
        if(this.isHit) return false;
        if(
            this.x - player_ball.radius <= player_ball.x && player_ball.x <= this.x + this.width + player_ball.radius &&
            this.y - player_ball.radius <= player_ball.y && player_ball.y <= this.y + this.height + player_ball.radius
        ){
            let reflected = false;
            
            if (player_ball.prevX < this.x - player_ball.radius){
                player_ball.vx = -Math.abs(player_ball.vx);
                player_ball.x = this.x - player_ball.radius;
                reflected = true;
            }else if(player_ball.prevX > this.x +this.width + player_ball.radius){
                player_ball.vx = -Math.abs(player_ball.vx);
                player_ball.x = this.x + this.width + player_ball.radius;
            }

            if(player_ball.prevY < this.y - player_ball.radius){
                player_ball.vy = -Math.abs(player_ball.vy);
                player_ball.y = this.y - player_ball.radius;
                reflected = true;
            }else if(player_ball.prevY > this.y + this.height + player_ball.radius){
                player_ball.vy = -Math.abs(player_ball.vy);
                player_ball.y = this.y + this.height + player_ball.radius;
                reflected = true;
            }
            this.isHit = true;

            player_ball.prevX = player_ball.x;
            player_ball.prevY = player_ball.y;
            return reflected;
        }
        player_ball.prevX = player_ball.x;
        player_ball.prevY = player_ball.y;
        return false;

    }
    show(){
        if (this.isHit) return;
        ctx.fillStyle = "blue";
        ctx.fillRect(this.x,this.y,this.width,this.height);
    }
}

引数

インスタンスplayer_ballの引数には、x,y,radius,prevX,prevY,があり、
xは球のx座標
yは球のy座標
radiusは球の半径
prevXは球の一つ前のx座標
prevYは球の一つ前のy座標

0 likes

1Answer

html等も含めてコードは抜粋せずに すべて載せた方がいいと思います。

0Like

Your answer might help someone💌