找出物体的具体位置以进行碰撞检测

发布于 2024-12-02 19:10:16 字数 256 浏览 2 评论 0原文

我有一个正方形,它旋转到随机角度,然后沿其指向的方向沿直线行进。它通过使用变量作为其 x 轴然后调用

 Variable++

Eachframe.

不幸的是,我无法弄清楚如何返回正方形的确切位置,因为正方形可以以任何角度移动,因此并不严格遵循世界坐标网格。这意味着 x 变量不是形状的 x 坐标。

我如何返回形状的精确坐标以及如何以这样的方式进行操作,以便我可以从同一类中绘制两个表现不同的正方形。

I have a square that rotates to a random angle and then travels in a straight line in the direction it is pointing. It does this by using a variable as its x axis and then calling

 Variable++

Each frame.

unfortunatley i cannot work out how to return the exact position of the square because the square can be travelling at any angle and therefore doesn't rigidly follow the world coordinte grid. This means that the x variable is not the shapes x coordinate.

How do i return the shapes exact coordinates and how do i do it in such a way that i can have two squares drawn from the same class behaving differently.

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

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

发布评论

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

评论(2

鹿港巷口少年归 2024-12-09 19:10:16

那么,您已经测量了物体沿着其内部侧向轴开始的距离,并测量了该轴与水平线之间的角度?

如果是这样,那么您想要的公式就是简单的三角函数。假设对象从 (x, y) 开始,并沿着与水平面成“角度”角度的轴移动了“距离”单位,则当前位置 (x', y') 为:

x' = x + distance * cos(angle)
y' = y + distance * sin(angle)

如果原点位于屏幕左下角和坐标轴排列方格纸样式,x 向右增加,y 向上增加,假设角度是逆时针测量的,并且当角度为零时,对象沿正 x 方向前进。

如果您允许挥手解释,该公式是有效的,因为正弦和余弦的一个定义是它们是单位圆外侧指定角度处的点的 (x, y) 坐标。它也符合大多数人了解三角学的第一件事,即正弦是“与斜边相反”,而余弦是“与斜边相邻”。在这种情况下,您的斜边的长度为“距离”,并且您想要获得与轴重合的直角三角形的“相反”和“相邻”长度。

假设 Android 在这方面遵循 J2SE,需要注意的一件事是 Math.sinMath.cos 采用弧度角度,而 OpenGL 的 rotatef 接受一个以度为单位的参数。 Math.toDegreesMath.toRadians 可以为您进行转换。

So you've got a measure of distance from where the object started along its internal sideways axis and a measure of the angle between that axis and the horizontal?

If so then the formula you want is simple trigonometry. Assuming the object started at (x, y) and has travelled 'distance' units along an axis at an angle of 'angle' with the horizontal then the current position (x', y') is:

x' = x + distance * cos(angle)
y' = y + distance * sin(angle)

If you have the origin in the lower left of the screen and axes arranged graph paper style with x increasing to the right and y increasing as you go upward, that assumes that the angle is measured anticlockwise and that the object is heading along positive x when angle is zero.

If you'll permit a hand waving explanation, the formula works because one definition of sine and cosine is that they're the (x, y) coordinates of the point on the outside of a unit circle at the angle specified. It also matches with the very first thing most people learn about trigonometry, that sine is 'opposite over hypotenuse', and cosine is 'adjacent over hypotenuse'. In this case your hypotenuse has length 'distance' and and you want to get the 'opposite' and 'adjacent' lengths of a right angled triangle that coincides with the axes.

Assuming Android follows J2SE in this area, the one thing to watch out for is that Math.sin and Math.cos take an angle in radians, whereas OpenGL's rotatef takes an argument in degrees. Math.toDegrees and Math.toRadians can do the conversion for you.

浅暮の光 2024-12-09 19:10:16

当你制作形状时,你应该已经指定了它的 X 和 X 。 Y 坐标。我不太清楚你说找不到坐标是什么意思?

还要确保你做成名的独立运动;目前,您正在程序的每个循环中向变量添加一个。这意味着如果它以 60 帧每秒 (FPS) 运行,它将移动 60 个单位,但如果它以 30FPS 运行,它将以一半的速度移动

When you made the shape you should have already specified its X & Y coordinates. Im not too sure what you mean when you say you cant find the coordinates?

Also make sure you do fame independent movement; currently you are adding one to your variable on every loop of your program. This means if it runs a 60 Frame Per Second(FPS) it will move 60 units, but if it runs at 30FPS it will move at half the speed

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