gpt4 book ai didi

android - 如何使用 AndEngine 确定在屏幕上画线的位置

转载 作者:行者123 更新时间:2023-11-29 01:39:29 25 4
gpt4 key购买 nike

我有一个屏幕 (480x800),M(mx, my) 是一个静态点,N(nx,ny) 是屏幕上的一个动态点。 N(nx, ny) 的位置取决于触摸的位置。我想确定 P(?,?) 和 Q(?,?) 的位置以绘制第 1 行第 2 行第 2 行是反射(reflect)第 1 行

enter image description here

这是我的代码:

    private Line l2;

@Override
public boolean onSceneTouchEvent(final Scene pScene,
final TouchEvent pSceneTouchEvent) {
if (this.mPhysicsWorld != null) {
switch (pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_DOWN:
// Get position
p1x = pSceneTouchEvent.getX();
p1y = pSceneTouchEvent.getY();
return true;

case TouchEvent.ACTION_MOVE:
// Remove instance of the old line
mScene.detachChild(l2);

p3x = pSceneTouchEvent.getX();
p3y = pSceneTouchEvent.getY();

Rectangle testR = new Rectangle(CAMERA_WIDTH / 2,
CAMERA_HEIGHT / 2, 20, 20,
getVertexBufferObjectManager());

l2 = new Line(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2, p3x, p3y,
getVertexBufferObjectManager());
l2.setColor(new Color(223f / 255f, 118f / 255f, 43f / 255f));
l2.setLineWidth(5);
mScene.attachChild(l2);
return true;
}
return false;
}
return false;
}

如果您有其他方法可以解决我的问题。请与我分享。谢谢。

最佳答案

我有一段时间没有用 AndEngine 编程了,但这与 AndEngine 无关,所以我可以给你一个伪代码来解决你的问题。

1) 检查 nx < mx

2) 计算线 1 的斜率:line1slope = (my-ny)/(mx-nx)

3) 使用等式找到 P 坐标:y-y1 = m(x-x1)

其中 m = line1slope

y1 = 我的

x1 = mx

y = 480(P 的 y 坐标)(如果 line1slope > 0 则 y = 0)

然后你可以找到你的x(这是P的x坐标)

line2slope = -1 * line1slope 因为它们是反光的

现在你又需要找到 Q(你知道 X = 0 所以你只需要找到 Y 坐标)使用等式:y-y1 = m(x-x1)

其中 m = line2slope

y1 = py

x1 = px

x = 0

然后你可以找到你的y坐标(也就是Q的y坐标)

希望对您有所帮助。

关于android - 如何使用 AndEngine 确定在屏幕上画线的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25635715/

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