如何产生分散效果?

时间:2016-05-09 23:00:48

标签: c# physics point

我正在Visual Studio 2015中创建一个项目,其中我是一个行星图片框和一个星形图片框。行星在计时器周围旋转,每100ms更新一次。这是代码。

public partial class Form1 : Form
{
    float angle = 0;
    float rotSpeed = 1;
    Point point = new Point(253, 151);
    int distance = 200;

 private void timer1_Tick(object sender, EventArgs e) {
       if (star.Visible) { // star is a picturebox
        angle += rotSpeed;
        int x = (int)(point.X + distance * Math.Sin(angle * Math.PI / 180));
        int y = (int)(point.Y + distance * Math.Cos(angle * Math.PI / 180));
        planet.Location = new Point(x, y);
       }
         else {
        // What do i put here so that when the star disappears, the planet
        // infinitely drifts off in the direction it was going?
       }
   }

所以这颗行星围着这颗恒星运行,我想模拟这颗行星在恒星消失时漂移到太空中。

差不多,如果一个明星在现实生活中消失会发生什么。

1 个答案:

答案 0 :(得分:2)

一旦这颗恒星消失,牛顿的第一部法则就开始了,它说速度是恒定的。那么,速度是多少?这是一个不错的诀窍'你可以使用,即速度是位置相对于时间的导数,所以

vx = distance * Math.Cos(angle * Math.PI / 180) * rotSpeed * Math.PI / 180
vy = -distance * Math.Sin(angle * Math.PI / 180) * rotSpeed * Math.PI / 180

我们使用d/dt angle = rotSpeed

因此,我们只需要根据其恒定速度更新其位置:

x0 = point.X + distance * Math.Sin(angle * Math.PI / 180)
y0 = point.Y + distance * Math.Cos(angle * Math.PI / 180)
x = x0 + driftTicks * vx
y = y0 + driftTicks * vy

其中driftTicks是' ticks'的数量,即太阳消失时调用timer1_Tick的次数。它从0开始,每次1子句触发时都会增加else

注意:如果您想以更灵活和可扩展的方式执行此操作,通常的办法是在太阳消失时保存xy位置,而不是每次都重新计算时间,并将这些保存的值用于x0y0