gpt4 book ai didi

java - 2D 碰撞位图问题

转载 作者:行者123 更新时间:2023-12-01 05:02:06 25 4
gpt4 key购买 nike

位图 A 的 X/Y 位置是静态的,位图 B 是移动位图,从屏幕底部移动到用户触摸屏幕的任何位置。然而,当移动位图到达静态位图时,它不会被识别为冲突,移动位图使用四舍五入的 float,静态位图使用 int。下面是一段代码,我想弄清楚需要更改什么才能使我的位图交叉路径被视为碰撞

                  canvas.drawBitmap(rock2, 70, 32, null);
//this is where it checks to see if the moving bitmaps y coordinate is matched to the Y coordinate of the static bitmap
if(canvas.getHeight() - Math.round(animY*-1) == (canvas.getHeight() - (canvas.getHeight()-32))){

Log.d("ALERT", "COLLIDE");

}
//the Y value is *-1 because the moving bitmap is starting from the bottom of the screen so this converts the value to positive value

我想知道我的计算是否错误,或者我以错误的方式处理这次碰撞。下面是我的运动代码 fragment

               //UP Y ANIMATION
if(animY*-1 < canvas.getHeight() && !bumpY){

animY += speedY;

//IF ROCK IS NOT AT EXTREME LEFT OR EXTREME RIGHT KEEP ON TRACK
if(animX < (canvas.getWidth()/2) || animX*-1 < (canvas.getWidth()/2) && !bumpX){
animX += speedX;
//IF ROCK HITS EDGE LEFT/RIGHT RETURN TO SENDER (INDICATE BUMP)
if(animX*-1 > (canvas.getWidth()/2) - rock.getWidth()/2 || animX > (canvas.getWidth()/2) - rock.getWidth()/2){
bumpX = true;
bumpY = true;
}
}

//IF Y HITS TOP OF SCREEN
if(animY*-1 > canvas.getHeight()){
bumpY = true;
}
}

//DOWN Y ANIMATION
if(animY < 0 && bumpY){

//REVERSE DIRECTION OF Y
animY -= speedY;

//IF ROCK HITS TOP OR SIDE REVERSE X DIRECTION
if(bumpX || bumpY)
animX -= speedX;

//IF AT STARTING POINT
if(animY > 0){
bumpY = false;
bumpX = false;
}
}

//in an ontouch method where the X and Y values are calculated
case MotionEvent.ACTION_UP:
finalX = event.getX() - (cross.getWidth() / 2);
finalY = event.getY() - (cross.getHeight() / 2);
moveToX = finalX - startX;
moveToY = finalY - startY;
speedX = moveToX / 50;
speedY = moveToY / 50;
break;

如有任何提示,我们将不胜感激,谢谢。

最佳答案

抱歉,如果这是一个愚蠢的答案,但您的检查应该是:

if(canvas.getHeight() - Math.round(animY*-1) >= (canvas.getHeight() - (canvas.getHeight()-32))){

相反? (将“==”更改为“">=”)否则您只检查岩石何时准确落在(canvas.getheight -32)上,而不是检查它是否经过

关于java - 2D 碰撞位图问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13257351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com