XNA:向向量添加力 [英] XNA: adding a force to a vector

查看:29
本文介绍了XNA:向向量添加力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实在二维平面上有 2 个点.一个已经有一个向量来确定它将向哪个方向移动.

i do have 2 points on a 2d plane. one has already an vector that does determine in which direction it will move.

现在我想向这个现有向量添加一个向量.于是他朝着另一点的方向加速.

now i want to add a vector to this existing vector. so he accelerates in the direction of the other point.

更清楚一点,大约有 2 颗小行星在太空中飞行(只有 2d),引力应该使它们彼此靠近一点.

to be a bit more clear, it is about 2 asteroids flying in space (only 2d) and gravitation should move them a bit closer to each other.

到目前为止我所做的是:

what i did build till now is this:

c = body.position - body2.position;
dist = c.Length();

acc = (body.masse * body2.masse) / (dist * dist);

xDist = body2.position.X - body.position.X;
yDist = body2.position.Y - body.position.Y;

direction = MathHelper.ToDegrees((float)(Math.Atan2((double)yDist, (double)xDist)));

body.velocity.Y = body.velocity.Y + (float)(Math.Sin(direction) * acc);
body.velocity.X = body.velocity.X + (float)(Math.Cos(direction) * acc);

在计算的方向完全关闭的那一刻.我肯定犯了一个愚蠢的错误,但我不知道.

in the moment the direction calculated is completly off. surely i am making just a stupid mistake, but i have no idea.

推荐答案

您需要将方向角以 弧度 为单位传递给 Math.sin 和 Math.Cos(而不是以度为单位)在您的简单代码中).

You need to pass your direction angle in in radians to Math.sin and Math.Cos (rather then in degree as you do in your smaple code).

另见:http://msdn.microsoft.com/en-us/图书馆/system.math.sin.aspx

角度 a 必须以弧度为单位.乘以 Math.PI/180 以将度数转换为弧度.

The angle, a, must be in radians. Multiply by Math.PI/180 to convert degrees to radians.

这篇关于XNA:向向量添加力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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