绕一点移动一个圆

发布于 2024-11-30 17:19:49 字数 743 浏览 1 评论 0原文

我有一个偏离实体中心点的圆(边界圆),我正在研究如何在实体旋转时围绕该圆移动该圆,以便它始终位于角色的同一位置。例如,假设我有一个汽车前部的边界圆,当汽车转弯时,该边界圆也需要转动。

所以我有两点:position,它是实体中心点的位置;offset,它是圆相对于该位置的偏移量。假设角度为 0。

因此,如果我的车面向 0 度: position (150, 150) 和 offset (50, 0) 那么边界圆将位于 200, 150。

现在,如果我将汽车旋转 90 度,边界圆 将位于 200, 150 处。应该在位置 150、200。

这就是我现在所拥有的:

var differenceX : Number = _centre.x - _offset.x;
var differenceY : Number = _centre.y - _offset.y;

var rotatedX : Number = differenceX * Math.cos(_angle);
var rotatedY : Number = differenceY * Math.sin(_angle);

var currentOffset : Point = new Point(_centre.x + rotatedX, _centre.y + rotatedY);

但它给了我这些长椭圆形,现在是一个完美的圆形。

有想法吗?

I have a circle (bounding circle) offset from the centre point of my entity and I'm looking on how to move that circle around the entity as it rotates so that it's always in the same spot of the character. For example, say I have a bounding circle for the front of a car, when the car turns, that bounding circle needs to turn too.

So I have two points: position which is the position of the entity's centre point and offset which is the offset of the circle from that position. This assumes an angle of 0.

So if my car is facing 0 degrees:
position (150, 150) and offset (50, 0) then the bounding circle would be at 200, 150.

Now if I rotated the car 90 degrees, the bounding circle should be at position 150, 200.

This is what I have now:

var differenceX : Number = _centre.x - _offset.x;
var differenceY : Number = _centre.y - _offset.y;

var rotatedX : Number = differenceX * Math.cos(_angle);
var rotatedY : Number = differenceY * Math.sin(_angle);

var currentOffset : Point = new Point(_centre.x + rotatedX, _centre.y + rotatedY);

But it's giving me these long ovals and now a perfect circle.

Ideas?

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

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

发布评论

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

评论(3

避讳 2024-12-07 17:19:49

您正在旋转对象的偏移和位置之间的差异。我认为您直观上有正确的想法,想要旋转圆与其对象之间的距离,但可能会错误地将偏移量视为圆的位置?无论哪种方式,由于您已经有了偏移量,因此我们不需要计算两者之间的距离。

此外,当您旋转 2D 矢量时,您还需要使用 2D 旋转。 2D 旋转使用 x 和 y 分量来组成每个新的 x' 和 y' 分量。快速谷歌会返回类似这个的内容。

因为代码总是更容易理解:

var cosAngle : Number = Math.cos(_angle);
var sinAngle : Number = Math.sin(_angle);

var rotatedX : Number = _offset.x * cosAngle - _offset.y * sinAngle;
var rotatedY : Number = _offset.x * sinAngle + _offset.y * cosAngle;

var currentOffset : Point = new Point(_centre.x + rotatedX, _centre.y + rotatedY);

之前旋转的椭圆形是由于未使用 2D 旋转而导致的,并且圆的距离可能是关闭的,因为 DifferenceX 和 DifferenceY 不是正确的值。

You are rotating the difference between the offset and the position of your object. I think you intuitively had the right idea of wanting to rotate the distance between the circle and it's object but might have mistaken the offset for your circle's position?. Either way, since you already have the offset, we don't need to calculate the distance between the two.

Also, when you rotate a 2D vector, you need to use 2D rotation as well. 2D rotations use the x and y component to compose each new x' and y' component. A quick google returns something like this.

Since code is always easier to understand :

var cosAngle : Number = Math.cos(_angle);
var sinAngle : Number = Math.sin(_angle);

var rotatedX : Number = _offset.x * cosAngle - _offset.y * sinAngle;
var rotatedY : Number = _offset.x * sinAngle + _offset.y * cosAngle;

var currentOffset : Point = new Point(_centre.x + rotatedX, _centre.y + rotatedY);

The oval shape of your previous rotation was due to not using a 2D rotation and the distance of your circle was probably off because the differenceX and differenceY weren't the right values.

情魔剑神 2024-12-07 17:19:49

也许尝试不使用实体来绘制圆圈:

var circlePositionX : Number = _centre.x - _offset.x;
var circlePositionY : Number = _centre.y - _offset.y;

var radius : Number = 50;

var rotatedX : Number = circlePositionX + (radius * Math.cos(_angle));
var rotatedY : Number = circlePositionY + (radius * Math.sin(_angle));

希望有帮助......

Maybe try not using the entity to draw the circle:

var circlePositionX : Number = _centre.x - _offset.x;
var circlePositionY : Number = _centre.y - _offset.y;

var radius : Number = 50;

var rotatedX : Number = circlePositionX + (radius * Math.cos(_angle));
var rotatedY : Number = circlePositionY + (radius * Math.sin(_angle));

Hope that helps...

奢欲 2024-12-07 17:19:49

要旋转汽车,您可以有效地将其带回原点并在那里旋转,然后将其平移回来。圆心是相同的,即它只是汽车上的另一个点。只需单独对_offset进行旋转,然后通过_centre平移即可

To rotate the car you effectively take it back to the origin and rotate it there and then translate it back. The circle centre is the same i.e. it is just another point on the car. Just perform the rotation on _offset alone and then translate it by _centre
.

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