在吸引人的身体上找到加速的力量

Finding the force of acceleration on an attractive body

本文关键字:加速 力量 身体上 吸引人      更新时间:2023-10-16

我有一个旋转星星的行星模拟。地球上力的方程是:

g·m 1 走气 2 走定(q 1 -q 2 )/|| Q 1 -Q 2 || 3

其中g是重力常数,m 1 和m 2 是恒星和行星的质量,q 1 -q 2 是两个物体的位置向量之间的差异。

我已将此方程转换为

的组件形式

f x =gšm 1 走ch>22 )/|| Q 1 -q 2 || 3

f y =gÅm 1 走ch>22 )/|| Q 1 -q 2 || 3

使用x 1 -x 2 是向量的x分量之间的差异,y 1 - y 2是向量的y组分之间的差异。

|| Q 1 -Q 2 || 3 可以被重写为SQRT(((x 1 - x 2 2 (y 1 - y 2 2 3 我们可以将其重写为((x 1 -x 2 )) 2 (y 1 --y 2 2 3/2

这是我的代码

    xdif = (planets[0].x - stars[0].x);
    ydif = (planets[0].y - stars[0].y);
    planets[0].ax = -10*xdif / pow((pow(xdif, 2) + pow(ydif, 2)), 1.5);
    planets[0].ay = -10*ydif / pow((pow(xdif, 2) + pow(ydif, 2)), 1.5);

出于某种原因,地球没有像它那样遵循轨道。有人知道我错了吗?

编辑::忘了提到,所有这些变量都是双人的,语言为c

编辑2 ::这是完整的代码

    #define WIN32_LEAN_AND_MEAN
    #include <math.h>
    #include <windows.h>
    #include <stdlib.h>
    #include <string>
    #include <iostream>
    #include <fstream>
    #include <chrono>
    using namespace std;
    #define SCREEN_WIDTH  1920
    #define SCREEN_HEIGHT 1080
    #define LGREY RGB(200,200,200)
    #define BLACK RGB(0, 0, 0)
    #define WHITE RGB(255,255,255)
    WPARAM w;
    HINSTANCE hInst;
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    POINT mousePos;
    int t;
    struct star { double x, y; };
    struct Body { double x, y, vx, vy, ax, ay; };
    star stars[2];
    Body planets[1];
    long newtime= std::chrono::system_clock::now().time_since_epoch().count();
    long oldtime=newtime;
    int signx;
    int signy;
    double force = 0;
    double xdif;
    double ydif;
    void drawCircle(int x, int y, int w, int h, COLORREF insidecolor, COLORREF bordercolor, HDC hdc) {
        HGDIOBJ B1 = CreateSolidBrush(insidecolor);
        HPEN P1 = CreatePen(PS_SOLID, 1, bordercolor);
        SelectObject(hdc, B1);
        SelectObject(hdc, P1);
        Arc(hdc, x, y, x + w, y + h, x - 1, y - 1, x + 1, y + 1);
        DeleteObject(B1);
        DeleteObject(P1);
    }
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
        mousePos = { 1200, 600 };
        WNDCLASSEX wc;
        ZeroMemory(&wc, sizeof(WNDCLASSEX));
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WindowProc;
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.lpszClassName = L"WindowClass";
        wc.hbrBackground = CreateSolidBrush(WHITE);
        RegisterClassEx(&wc);
        HWND hWnd = CreateWindowEx(NULL, L"WindowClass", L"Template", WS_OVERLAPPEDWINDOW, 10, 10, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
        hInst = hInstance;
        //separated by 10 au
        stars[0].x = 0;
        stars[0].y = 0;
        stars[1].x = 10;
        stars[1].y = 0;
        planets[0].x = -5;
        planets[0].y = 0;
        planets[0].vx = 0;
        planets[0].vy = -1;
        planets[0].ax = 0;
        planets[0].ay = 0;
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);

        MSG msg;
        bool running = TRUE;
        while (running) {
            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
                if (msg.message == WM_QUIT) {
                    running = FALSE;
                }
            }
        }
        return msg.wParam;
    }
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
        PAINTSTRUCT ps;
        HDC hdc;
        switch (message)
        {
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            planets[0].x += planets[0].vx;
            planets[0].y += planets[0].vy;
            planets[0].vx += planets[0].ax;
            planets[0].vy += planets[0].ay;
            xdif = (planets[0].x - stars[0].x);
            ydif = (planets[0].y - stars[0].y);
            planets[0].ax = -10.0*xdif / pow((pow(xdif, 2) + pow(ydif, 2)), 1.5);
            planets[0].ay = -10.0*ydif / pow((pow(xdif, 2) + pow(ydif, 2)), 1.5);
            for (int i = 0; i < 1; i++) {
                drawCircle(planets[i].x * 50 + 1920 / 2 - 10, planets[i].y + 1080 / 2 - 10, 20, 20, BLACK, BLACK, hdc);
            }
            for (int i = 0; i < 2; i++) {//1920x1080
                drawCircle(stars[i].x*50+1920/2 - 25, stars[i].y+1080/2 - 25, 50, 50, BLACK, BLACK, hdc);
            }
            while (newtime < oldtime + 10000000/60) {//60 frames per second
                newtime = std::chrono::system_clock::now().time_since_epoch().count();
            }
            oldtime = newtime;
            InvalidateRect(hWnd, NULL, TRUE);
            EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }

使用Visual C 2015,可以按以下方式编译(使用控制台子系统,适合测试故障应用程序):

cl orbit.cpp -d unicode gdi32.lib user32.lib kernel32.lib/link/subsystem:console/entry:winmaincrtstartup

我对所使用的公式有问题,我相信分离向量的正确方法是使用等式C*(q1-q2)/|| Q1-Q2 ||^2

这是代码:

    xdif = (planets[0].x - stars[0].x);
    ydif = (planets[0].y - stars[0].y);
    planets[0].ax = -.01*xdif / pow(hypot(xdif, ydif), 2);
    planets[0].ay = -.01*ydif / pow(hypot(xdif, ydif), 2);

这似乎现在起作用了,我认为我可以处理一些不稳定。非常感谢!

不是答案,但这不适合评论。

,如果您进行以下简单调整,则可以以更安全,更准确,更高效和更可读性的方式使用<chrono>

auto newtime = std::chrono::system_clock::now();
auto oldtime = newtime;
// ...
using frames = std::chrono::duration<
                                std::chrono::system_clock::rep, std::ratio<1, 60>>;
while (newtime < oldtime + frames{1}) {//60 frames per second
    newtime = std::chrono::system_clock::now();
}
oldtime = newtime;