将路径对象(箭头)朝向屏幕上的另一个对象(位图)

发布于 2024-12-02 20:07:59 字数 977 浏览 0 评论 0原文

几乎得到它,但有一些问题。

这是我当前绘制箭头的代码。包括路径创建。

{//gets done just once to create the arrow
oBlipPath.moveTo(0, -5);
oBlipPath.lineTo(5, 0);
oBlipPath.lineTo(0, 5);
}

{//run in a for loop for every enemy (i)    
canvas.save();
canvas.translate(iWidth / 2, iHeight / 2);
canvas.rotate((float)Math.toDegrees(Math.atan2((oEnemies[i].getEnemyCenterY()-worldY)-user.getShipCenterY(), (oEnemies[i].getEnemyCenterX()-worldX)-user.getShipCenterX())));
canvas.drawPath(oBlipPath, oBlipPaint);
canvas.restore();
}

为了提供一些上下文,用户是位于屏幕中央的玩家。 敌人是玩家可以击落的物体,无论是在屏幕上还是屏幕外。

我试图做的是在玩家周围创建一种雷达。小箭头指示摧毁所有物体并完成任务的方法。

正如我在之前的文章中提到的,我很擅长逻辑,但在数学方面却很糟糕。感谢上帝,你们都在这里,因为这些障碍是一个杀手,我可能迟早会放弃。感谢您迄今为止的帮助!

无论如何,回到问题上来。翻译 20,20 确实用于调试。我试图弄清楚它的作用。现在,我看到了箭头,但它们看起来像是围绕屏幕左上角的轴以 20 像素为半径旋转。它们对对象坐标做出反应,但似乎是以错误的方式。

我会继续摆弄,但我的问题是,如何使旋转轴位于屏幕中心,并使箭头指向周围飞行的物体?准确地说,箭头不仅需要指向对象,而且还需要围绕屏幕中心玩家头像的半径移动。

谢谢你!

I've almost got it, but there's a few problems.

Here's my current code for drawing the arrows. The path creation included.

{//gets done just once to create the arrow
oBlipPath.moveTo(0, -5);
oBlipPath.lineTo(5, 0);
oBlipPath.lineTo(0, 5);
}

{//run in a for loop for every enemy (i)    
canvas.save();
canvas.translate(iWidth / 2, iHeight / 2);
canvas.rotate((float)Math.toDegrees(Math.atan2((oEnemies[i].getEnemyCenterY()-worldY)-user.getShipCenterY(), (oEnemies[i].getEnemyCenterX()-worldX)-user.getShipCenterX())));
canvas.drawPath(oBlipPath, oBlipPaint);
canvas.restore();
}

To give a little context, user is the player, who is on the center of the screen. oEnemies are objects which are for the player to shoot down, both onscreen and offscreen.

What I'm attempting to do is create a sort of radar around the player. Tiny little arrows to show the way to go to destroy all the objects and complete the mission.

As I've mentioned in my previous posts, I'm great with logic, but absolutely terrible at math. Thank god you folks are available here, because these roadblocks are a killer and I'd probably give up sooner than later. Thank you for the help so far!

Anyway, back to the issue. The translate 20,20 is there really for debugging. I was trying to figure out what it does. Right now, I see the arrows, but they look like they're rotating around an axis at the top left of the screen at a radius of 20 pixels. They are reacting to the object coordinates, but in the wrong way it seems.

I will keep fiddling, but my question is, how do I get the rotation pivot be at the center of the screen, and get the arrows to point towards the objects flying around? To be precise, the arrows need to not only point towards the objects, but also move around a radius around the player's avatar in the center of the screen.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2024-12-09 20:07:59

(我的数学不太好,但我知道这个问题)。

作为角度计算的一部分,您需要将您的播放器和其他所有内容相对于 0,0 进行平移,然后旋转并平移回来。

这样想,你总是围绕 0,0 旋转,否则旋转不准确(因为你总是相对于 0,0 旋转,如果你不在 0,0 处,你只会围绕它旋转),所以如果您需要将对象旋转 10,20 度 20 度,您首先减去两个轴上的差异(在本例中为 10 和 20),然后将对象旋转 20 度(因此您实际上是相对于0,0),然后再加上减去的 10 和 20。这使得旋转正确。

在你的例子中,你需要抵消整个世界(这实际上是一个世界翻译,我认为它被称为),所以你想计算出你的玩家与 0,0 的差异,并从所有对象中减去它,有效地使 0,0 成为你的中心播放器并保留与此相关的所有其他对象。然后进行旋转计算,将箭头放置在 0,0 周围。然后通过重新添加偏移量将所有内容(包括新创建的箭头)平移回来。

(更有效的方法是计算移动、旋转和移动所需的矩阵[参见矩阵乘法],并将其应用于所有同时旋转所有对象的对象。)

我只在 OpenGL 中完成了此操作为此,您只需使用平移矩阵将其应用于整个世界。我不确定 andriod canvas 中是否有类似的东西,但我认为这个方法仍然是相同的。

链接有一个图形说明这个概念。我相信这会让你走上正轨。

(my math isn't great but i know the issue).

You need to translate your player and everything else with respects to 0,0 as part of the angle calculation then rotate and translate back.

Think of it like this, you always rotate around 0,0 otherwise the rotation isn't accurate (because you always rotate with respect to 0,0, if you aren't at 0,0 you will just rotate around it), so if you need to rotate an object at 10,20 by 20 degrees you first subtract the differences in both axis (in this case 10 and 20) then you rotate the object by 20 degress (so you are effectively rotating this object with respect to 0,0) then you add back on the 10 and 20 you subtracted. This causes the rotation to be correct.

In your example you need to offset the entire world (this is actually a world translation i think it's called) so you want to work out your players difference from 0,0 and subtract that from all objects effectively making 0,0 the centre of your player and keeping all other objects in relation to this. Then you make the rotation calculations to place the arrows around 0,0. Then translate everything (including the newly created arrows) back by adding the offset back on.

(The more effecient way to do this would be to calculate the matrix needed to move, rotate and move [see matrix multiplication] and apply this to all objects which would rotate everything at the same time.)

I've only done this in OpenGL and for that you just use a translation matrix to apply it to the entire world. I'm not sure that there is anything like that in andriod canvas but i assume this method is still the same.

This Link has a graphical illustration of this concept. I believe this will put you on the right track.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文