如何实现多个重力源,特别是所需的触发。 (C#) VS 2010

发布于 2024-09-09 08:38:29 字数 2244 浏览 1 评论 0原文

我正在设计一个小游戏,其中物体同时被多个物体吸引。我正在做的是绘制这些物体的拍摄路线。

为了计算行星的引力,我使用这个公式:gravityStr/distToTarg^2(伪代码)。我的问题在于计算我需要移动镜头的方向(以及随后的余弦和正弦)。下面是我的“Shot”类的代码。

这里未包括的其他类是“Planet”类,它具有 XY 值(其中表单的左上角为 0,0)。以及重力(拉力)。

似乎发生的事情是行星拒绝了我的射击。我尝试过颠倒planetModY 和planetModX 的符号,但我得到了更奇怪的效果。

值得注意的是,当镜头从地球的右上方出发,以及向下和向右移动时,镜头似乎起作用了。

我真的很感谢一些答案以及代码更正。

C#:

public class Shot
{
    static public Shot[] Shots = new Shot[0];
    static public int shotSteps = 3000;

    public const double rad = Math.PI / 180;

    PointF[] PointFs = new PointF[0];

    public Shot(int x, int y, int dir, int pow)
    {
        addShot(this);
        double cos = Math.Cos((dir * rad));
        double sin = Math.Sin((dir * rad));

        addPoint(new PointF(x, y));
        addPoint(new PointF((float)(cos * pow + x), (float)(sin * pow + y)));

        for (int step = 2; step < shotSteps; step++)
        {
            PointF prevPrevPoint = PointFs[step - 2];
            PointF prevPoint = PointFs[step - 1];

            Double radians = Math.Atan2(prevPoint.Y - prevPrevPoint.Y, prevPoint.X - prevPrevPoint.X);
            Double curCos = Math.Cos(radians);
            Double curSin = Math.Sin(radians);

            PointF curPoint = new PointF(prevPoint.X + (float)curCos * pow, prevPoint.Y + (float)curSin * pow);


            int planetModX = 0;
            int planetModY = 0;

            for (short index = 0; index < Planet.Planets.Length; index++)
            {
                Planet curPlanet = Planet.Planets[index];
                double planetRadians = Math.Atan2(curPoint.Y - curPlanet.Y, curPoint.X - curPlanet.X);
                double planetCos = Math.Cos(planetRadians);
                double planetSin = Math.Sin(planetRadians);

                double planetShotDist = distTo(curPlanet.X, curPlanet.Y, curPoint.X, curPoint.Y);

                double pullPower = curPlanet.Gravity / (planetShotDist * planetShotDist);
                planetModY += (int)(planetSin * pullPower);
                planetModY += (int)(planetCos * pullPower);
            }

            curPoint.X += planetModX;
            curPoint.Y += planetModY;

            addPoint(curPoint);
        }
    }

I am designing a small game wherein objects are attracted by multiple objects at once. What I am doing is plotting the course that the shots of these objects will take.

To calculate the pull of the planets, I am using this formula: gravityStr/distToTarg^2 (pseudo code). Where my problem lies is in calculating the direction (and subsequently the cos, and sine) that I need the shot to be moved by. Below is the code for my "Shot" class.

Other classes not included here are the "Planet" class, which has an X Y value (where the upper left of the form is 0,0). As well as a gravity (the strength of the pull).

What seems to occur, is the planets REPULSE my shots. I've tried reversing the sign of planetModY and planetModX, but I get even STRANGER effects.

It is worth noting that the shot appears to work when it originates to the upper right of the planet, and when it is traveling down and to the right.

I would really appreaciate some answers, as well as code corrections.

C#:

public class Shot
{
    static public Shot[] Shots = new Shot[0];
    static public int shotSteps = 3000;

    public const double rad = Math.PI / 180;

    PointF[] PointFs = new PointF[0];

    public Shot(int x, int y, int dir, int pow)
    {
        addShot(this);
        double cos = Math.Cos((dir * rad));
        double sin = Math.Sin((dir * rad));

        addPoint(new PointF(x, y));
        addPoint(new PointF((float)(cos * pow + x), (float)(sin * pow + y)));

        for (int step = 2; step < shotSteps; step++)
        {
            PointF prevPrevPoint = PointFs[step - 2];
            PointF prevPoint = PointFs[step - 1];

            Double radians = Math.Atan2(prevPoint.Y - prevPrevPoint.Y, prevPoint.X - prevPrevPoint.X);
            Double curCos = Math.Cos(radians);
            Double curSin = Math.Sin(radians);

            PointF curPoint = new PointF(prevPoint.X + (float)curCos * pow, prevPoint.Y + (float)curSin * pow);


            int planetModX = 0;
            int planetModY = 0;

            for (short index = 0; index < Planet.Planets.Length; index++)
            {
                Planet curPlanet = Planet.Planets[index];
                double planetRadians = Math.Atan2(curPoint.Y - curPlanet.Y, curPoint.X - curPlanet.X);
                double planetCos = Math.Cos(planetRadians);
                double planetSin = Math.Sin(planetRadians);

                double planetShotDist = distTo(curPlanet.X, curPlanet.Y, curPoint.X, curPoint.Y);

                double pullPower = curPlanet.Gravity / (planetShotDist * planetShotDist);
                planetModY += (int)(planetSin * pullPower);
                planetModY += (int)(planetCos * pullPower);
            }

            curPoint.X += planetModX;
            curPoint.Y += planetModY;

            addPoint(curPoint);
        }
    }

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

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

发布评论

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

评论(1

月亮坠入山谷 2024-09-16 08:38:29

暗示!每个物体不仅有位置,还有速度。在每一步,速度根据对象加速度而变化,位置根据速度而变化(二阶系统)。如果找出每个重力对对物体的总贡献并将它们矢量相加,您需要做什么。

如果我对你说希腊语(除了因为我是希腊人),那么你需要一本介绍矢量力学的物理书。

还要考虑两个物体彼此距离太近导致大重力和大距离步长的情况。要使其足够稳定以令人愉快,这不是一个小问题,但这是可能的。如果成功了,那将是非常令人欣慰的。

这是其他人以类似方式所做的事情(重力屏幕保护程序)

Hint! Each object not only has a position, but also velocity. At each step the velocity changes per the object acceleration, and the position per the velocity (2nd order system). What you need to do if figure out what the total contribution of each gravity pair is the object and add them vectorially.

If I am speaking Greek to you (besides because I am Greek), then you need an introduction to Physics book where they describe vector mechanics.

Also consider the case where two objects are too close to each other resulting in large gravity forces and large distance steps. This is not a trivial problem to make it stable enough to be enjoyable, but it is possible. If you succeed, it will be very gratifying.

Here is what somebody else has done in a similar fashion (Gravity Screensaver)

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