c# - 如何制作类似驱散的效果?

标签 c# physics point

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

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?
       }
   }

所以行星正在绕恒星运行,我想模拟当恒星消失时行星会飘入太空。

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

最佳答案

一旦恒星消失,牛顿第一定律就会生效,该定律表明速度是恒定的。那么,什么是速度呢?您可以使用一个很好的“技巧”,即速度是位置相对于时间的导数,所以

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 是“刻度”数,即太阳消失时调用 timer1_Tick 的次数。它从 0 开始,每次触发 else 子句时都会增加 1


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

关于c# - 如何制作类似驱散的效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37126752/

相关文章:

c# - 如何在 WPF (xaml) 中读取 .csv 文件中的特定行

c# - 界限在哪里 - 是否有可能过于热爱 LINQ?

javascript - 在带有纹理的圆形路径上 Canvas 水/ Blob 物理?

javascript - 在坐标系中找到最多未填充的点

c# - "Add to My Drive"按钮背后的代码

c# - 如何使静态构造函数成为非私有(private)的?

javascript - 根据质量和弹跳系数计算球与球碰撞的速度和方向

cocos2d-iphone - 如何创建基于旋转的脉冲矢量(Cocos2d、Chipmunk、Spacemanager)

c# - 如何使用 LINQ 和 C# 找到最接近 0,0 点的点

r - 散点图点子集颜色形状 ifelse