gpt4 book ai didi

android - 如何确定圆碰撞中的点?

转载 作者:太空狗 更新时间:2023-10-29 13:50:13 25 4
gpt4 key购买 nike

我有 2 个圆圈,一个我移动,另一个在屏幕中央。我想要做的是,当我将一个圆圈移到另一个圆圈上时,圆圈不会重叠。我设法做了类似的事情,但这太可怕了,我想知道是否有更有效的方法。

public class Juego extends SurfaceView implements View.OnTouchListener{

private Paint paint;
int x = 100, y = 100, radio = 100, otroX, otroY;

public Juego(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnTouchListener(this);
setFocusable(true);
paint = new Paint();
}

public void onDraw(Canvas canvas) {

paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, getWidth(), getHeight(), paint);

paint.setColor(Color.BLACK);
canvas.drawCircle(x, y, radio, paint);

otroX = canvas.getWidth() / 2;
otroY = canvas.getHeight() / 2;

canvas.drawCircle(otroX, otroY, radio, paint);

invalidate();
}

@Override
public boolean onTouch(View view,MotionEvent motionEvent){

x = (int)motionEvent.getX();
y = (int)motionEvent.getY();

double dist = Math.sqrt(Math.pow((x - otroX), 2) + Math.pow((y - otroY), 2));

if (dist <= radio + radio) {

if (x < otroX) {
x = otroX - radio - (radio / 2);
}
if (x > otroX) {
x = otroX + radio + (radio / 2);
}
if (y < otroY) {
y = otroY - radio - (radio / 2);
}
if (y > otroY) {
y = otroY + radio + (radio / 2);
}
}

invalidate();
return true;
}

这是我拥有的:https://mega.nz/#!HZsVhR4L!v6AhTWgJ27U8vV1rYJ_BuO8O2TxgKJV113m58P6ANek

这就是我想要的:https://mega.nz/#!PJFHmDYR!auzX-L-TBTNCZuD8vX8ugUeZmi-HhtWLqs6mUilfW_M

最佳答案

为了解决这个问题,当移动的圆圈太近时,我们需要将它移动到预期的最小距离,在两个圆心定义的线上:

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

x = (int) motionEvent.getX();
y = (int) motionEvent.getY();

double dist = Math.sqrt(Math.pow((x - otroX), 2) + Math.pow((y - otroY), 2));

if (dist <= radius * 2) {
// This calculate the displacement of a the distance 'radius*2' on the line between the two circles centers.
double angle = Math.atan2(y - otroY, x - otroX);
int moveX = (int) ((radius * 2) * Math.cos(angle));
int moveY = (int) ((radius * 2) * Math.sin(angle));
// Then we need to add the displacement to the coordinates of the origin to have the new position.
x = otroX + moveX;
y = otroY + moveY;
}

invalidate();
return true;
}

此代码基于KazenoZ answer .

关于android - 如何确定圆碰撞中的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48997980/

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