MonoGame-在一个点周围有一个对象圈 [英] MonoGame - Have an object circle around a point

查看:110
本文介绍了MonoGame-在一个点周围有一个对象圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带Vector2 Position的对象和一个带Vector2 Position的光标.

I have an object with a Vector2 Position, and a cursor with Vector2 Position.

当我按住某个键时,我希望对象在对象周围盘旋,但是在计算正确的坐标时遇到了麻烦.

When I hold a certain key, I want the object to circle around the object, but I'm having trouble calculating the correct coordinates.

我已设法使用以下代码使对象围绕光标绕圈(但它并不是沿着理想的圆,而是更多的螺旋):

I've managed to make the object circle around the cursor (but it's not going in a perfect circle, but more of a spiral) with this code:

Vector2 diff= Vector2.Normalize(cursor.Location - this.Location);

float angle = (float)Math.Atan2(diff.Y, diff.X) + (float)(90 * (Math.PI / 180));
this.Position += new Vector2((float)(speed * Math.Cos(angle)), (float)(speed* Math.Sin(angle)));

我计算光标和对象位置之间的角度,并向该值加上90°(以弧度为单位),根据我的逻辑,这应该使对象绕一个完美的圆周运动.但是,光标和对象之间的距离会迅速扩展.

I calculate the angle between cursor's and object's locations, and add 90° (in radians) to that value, which, by my logic, should make the object travel in a perfect circle. However, the distance between the cursor and the object quickly spreads.

我在这里算错了什么?

推荐答案

通常,当您希望某物绕点旋转时,您可以将距离定义为一个量,然后在Update方法中逐渐更改角度.然后在绘制方法中可以通过计算光标位置来绘制它,位置,到光标的距离以及所需的距离.

Usually when you want something to circle around a point, you define the distance to an amount and you incrementally change the angle in your Update method. THEN in your draw method you can draw it where you should by calculating the position from the cursor.Location, the distance from the cursor and the desired distance.

在大多数情况下,您希望您的轨道器具有与光标相同的位置,因此在Draw方法中计算新位置的效果最佳,因为这些计算既便宜又超级快(通常您不希望绘制方法).

In most situations like these you want your orbiter to have the same loation like your cursor, so calculating the new position in the Draw method works best, given that these calculations are cheap and super fast (you generally do not want to hog down your Draw method).

我现在无法检查它,但是您应该在以下几行中进行操作:

I am not able to check it right now, but what you should be doing is something in these lines:

鉴于您的对象应以AngularVelocity(每秒)的角速度旋转远离光标D距离,然后在最初发生这种情况时,将可变角度设置为零.然后在您的更新中执行:

Given that your object should rotate D distance away from your cursor with an angular velocity of AngularVelocity (per second), then when this initially happens, set a variable angle to zero. Then in your update do:

angle += (gameTime.ElapsedGameTime.TotalSeconds * AngularVelocity)

并在Draw方法中执行:

and in your Draw method do:

var displacedPosition = new Vector2(D * Math.Sin(angle), D * Math.Cos(angle));

并使用"displacedPosition"(而不是正常位置)渲染您的轨道器(如果当前正在运行).

and render your orbiter using the displacedPosition instead of the normal position if it is currently orbiting.

这篇关于MonoGame-在一个点周围有一个对象圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆