在吸引人的身体上找到加速的力量
Finding the force of acceleration on an attractive body
我有一个旋转星星的行星模拟。地球上力的方程是:
g·m 1 走气 2 走定(q 1 -q 2 )/|| Q 1 -Q
其中g是重力常数,m
我已将此方程转换为
的组件形式 f x =gšm 1 走ch>22 )/|| Q 1 -q
f y =gÅm 1 走ch>22 )/|| Q 1 -q
使用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;
相关文章:
- 加速C++练习2.4
- 找不到以下加速库:boost_fiber
- 玩家加速穿越世界(C++)
- 为什么我的程序在使用预留后没有加速?
- 为什么 CUDA 不会导致C++代码加速?
- 读取文件时无法使用 OpenMP 获得加速
- FFmpeg——使用硬件加速进行视频解码
- Opencv 加速与 CUDA 在 C++.
- 为什么从具有较大阵列的 SIMD 内部函数中获得的相对加速比标量更大?
- 加速创建大标准::矢量
- Xcode 中的加速框架以获得 A 的 QR 分解,但找不到 zgeqrf
- 使用 Rcpp 加速替换迭代算法中的列表和向量元素是否合法?
- 使用加速融合获取结构名称
- 加速兽握手:sslv3 警报握手失败错误
- 获取加速::p rocess::子的退出代码 在提升::asio::io_service?
- 使用字符串流加速 std::cout 日志记录
- OpenCV如何处理Mat作为指针来加速代码
- 使用加速进程间创建消息队列 - 内存访问冲突
- 加速 CRC 优化在手臂上失败
- 在吸引人的身体上找到加速的力量