[英]How to calculate a point out of an angle and a distance?
I searched and implemented things from this forum, it doesn't come out right.我从这个论坛搜索并实施了一些东西,结果不正确。 What I'm trying to achieve is to calculate a
spawnPoint
for player bullets relative to his position
and rotation
.我想要实现的是计算玩家子弹相对于他的
position
和rotation
的spawnPoint
。
The spawnPoint
should be and his X + his width
(the player is set to point to the right by default) and y + height/2
(to spawn from his center on the Y
axis). spawnPoint
应该是他的X + his width
(玩家默认设置为指向右侧)和y + height/2
(从他的Y
轴中心生成)。
This is what I got from this forum:这是我从这个论坛得到的:
this.bulletSpawn.x = (float)(this.position.x + this.width/2 + this.width * Math.cos(rotation));
this.bulletSpawn.y = (float)(this.position.y + this.height/2 + this.height/2 * Math.sin(rotation));
The rotation
is in Radians. rotation
以弧度为单位。 The this
is the Player
class. this
是Player
类。
Images showing what I expect to happen:显示我期望发生的事情的图像:
The red dot is the spawnPoint
I'm trying to calculate knowing the player position
and rotation
.红点是
spawnPoint
我试图计算知道玩家position
和rotation
。 The player Sprite is what rotates, and it rotates related to his center x and y, which is done with a lib, i do not have these variables.玩家精灵是旋转的,它旋转与他的中心 x 和 y 相关,这是用 lib 完成的,我没有这些变量。 The entire arrow would be the player , the arrow direction is where the player is pointing at, and the red dot would be the bulletSpawn point (or the expected one)
整个箭头将是 player ,箭头方向是玩家指向的地方,红点将是 bulletSpawn 点(或预期的点)
Using the code I posted, the bullets seem to be spawning from somewhere else.使用我发布的代码,子弹似乎是从其他地方产生的。 Even at the beggining they have an offset and when I rotate the player the
spawnPoint
seems to be relative to a different origin than what I'm expecting.即使在开始时,它们也有一个偏移量,当我旋转玩家时,
spawnPoint
似乎相对于与我期望的不同的原点。
This is the bullet position code:这是子弹位置代码:
position.x = holder.bulletSpawn.x - (float)(this.width/2 * holder.rotation);
position.y = holder.bulletSpawn.y - (float)(this.height/2 * holder.rotation);
This is inside the Bullet
class.这是在
Bullet
类中。 The position
variable is a Vector2
of bullet
, and holder
is the player instance.该
position
变量是一个Vector2
的bullet
,而holder
是播放器实例。 This code is merely to give an offset for the bullet to spawn at the center of its own size这段代码只是为了让子弹在其自身大小的中心产生一个偏移量
I added some fixes related to the comments, but the bullets still have a tiny offset that looks wrong at certain angles.我添加了一些与评论相关的修复,但子弹仍然有一个微小的偏移,在某些角度看起来是错误的。 Basically the distance i want to get is the width of the player, and his center y which is height/2.
基本上我想得到的距离是玩家的宽度,他的中心 y 是高度/2。
Let's initial position is X0, Y0
, rotation is about center point CX, CY
, and rotation angle is Theta
.让我们的初始位置是
X0, Y0
,旋转是关于中心点CX, CY
,旋转角度是Theta
。 So new position after rotation is:所以旋转后的新位置是:
NX = CX + (X0-CX) * Cos(Theta) - (Y0-CY) * Sin(Theta)
NY = CY + (X0-CX) * Sin(Theta) + (Y0-CY) * Cos(Theta)
This equations describe affine transformation of rotation of arbitrary point about center point, and affine matrix is combination of translation, rotation, and back translation matrices.该方程描述了任意点绕中心点旋转的仿射变换,仿射矩阵是平移、旋转和反向平移矩阵的组合。
About center CX, CY - you wrote关于中心 CX、CY - 你写道
it rotates related to his x and y origin at his bottom left
它旋转与他左下角的 x 和 y 原点有关
About initial point coordinate - for bullet it seems to be关于初始点坐标 - 对于子弹似乎是
X + Width, Y + Height/2
Swift extension:快速扩展:
extension CGSize {
static func offsetFrom(angle:CGFloat, distance:CGFloat) -> CGSize {
let rad = angle * CGFloat.pi / 180
return CGSize(width: sin(rad) * distance, height: cos(rad) * distance)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.