C++ _ Win32 _ Physics Simulation _ Physics Forums - The Fusion of Science and Community.pdf
(
221 KB
)
Pobierz
5.10.2017
C++ / Win32 / Physics Simulation | Physics Forums - The Fusion of Science and Community
Forums
Science Education
Homework and Coursework Questions
Advanced Physics Homework
Not nding help here? Sign up for a free 30min tutor trial with
Chegg Tutors
C++ / Win32 / Physics Simulation
stringa
1. The problem statement, all variables and given/known data
Simple Pendulum using Forces (Computer Simulation)
Max theata = PI / 6
Force Of Gravity is { 0, 10 }
Force_Tension.x = - 10 * sin(balls.theata);
Force_Tension.y = - 10 * cos(balls.theata);
2. Relevant equations
accelleration = Force_Tension - Force_Gravity / mass
#1
Apr 15, 2007
3. The attempt at a solution
Written in Win32 APi
All Physics is done in WM_CREATE and updatePhysics.........the program does not oscillate !!!
/* animation_shell.c
** -- Bare bones windows animation program.
** cs230 2/07
*/
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <math.h>
#de ne PI 3.14159f
struct _2D oat {
oat x;
oat y;
};
struct ball {
_2D oat current_position;
_2D oat current_velocity;
https://www.physicsforums.com/threads/c-win32-physics-simulation.165914/
1/5
5.10.2017
C++ / Win32 / Physics Simulation | Physics Forums - The Fusion of Science and Community
_2D oat current_accelleration;
int length_of_rope;
POINT rope_origin;
oat theata;
};
oat sqrt_g_over_r;
ball balls;
_2D oat Force_Gravity = {0, 10};
_2D oat Force_Tension;
void draw(HWND win);
void updatePhysics(void);
LRESULT CALLBACK WinProc(HWND win, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg) {
case WM_CREATE:
balls.length_of_rope = 300;
sqrt_g_over_r = sqrt(10.0 / balls.length_of_rope);
balls.rope_origin.x = 300;
balls.rope_origin.y = 0;
balls.theata = - PI / 6;
Force_Tension.x = - 10 * sin(balls.theata);
Force_Tension.y = - 10 * cos(balls.theata);
balls.current_accelleration.x = 0;
balls.current_accelleration.y = 0;
balls.current_velocity.x = 0;
balls.current_velocity.y = 0;
balls.current_position.x = balls.rope_origin.x + balls.length_of_rope * sin(balls.theata);
balls.current_position.y = balls.length_of_rope * cos(balls.theata);
break;
case WM_CHAR:
if (wp == 0x1b)
DestroyWindow(win);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
https://www.physicsforums.com/threads/c-win32-physics-simulation.165914/
2/5
5.10.2017
C++ / Win32 / Physics Simulation | Physics Forums - The Fusion of Science and Community
default:
return DefWindowProc(win,msg,wp,lp);
}
return 0;
}
int WINAPI WinMain(HINSTANCE instance, HINSTANCE ignore,
LPSTR command_line, int show) {
char *window_title = "Put title here",
*class_name = "Give class name";
WNDCLASS wc;
HWND win;
MSG msg;
wc.style = 0;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = instance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = class_name;
RegisterClass(&wc);
win = CreateWindow(class_name,window_title,
WS_POPUP,CW_USEDEFAULT,CW_USEDEFAULT,
GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),NULL,NULL,instance,NULL);
ShowWindow(win,show);
// Animation Styple "greedy" event loop
while (IsWindow(win)) {
if (PeekMessage(&msg,win,0,0,PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg); }
else
updatePhysics();
draw(win);
// give some cycles back to the CPU
Sleep(50);
}
UnregisterClass(class_name,instance);
return msg.wParam;
}
MENU
LOG IN OR SIGN UP
void draw(HWND win) {
static COLORREF color = RGB(255,128,128);
HDC dc;
HBRUSH brush;
RECT rect;
https://www.physicsforums.com/threads/c-win32-physics-simulation.165914/
3/5
5.10.2017
C++ / Win32 / Physics Simulation | Physics Forums - The Fusion of Science and Community
dc = GetDC(win);
GetClientRect(win,&rect);
FillRect(dc,&rect,(HBRUSH) GetStockObject(WHITE_BRUSH));
const int scale = 50;
// Draw the Rope
MoveToEx(dc, balls.rope_origin.x, balls.rope_origin.y, NULL);
LineTo(dc, balls.current_position.x, balls.current_position.y);
// Draw the force of Tension
LineTo(dc, balls.current_position.x + scale * Force_Tension.x, balls.current_position.y + scale * Force_Tension.y);
MoveToEx(dc, balls.current_position.x, balls.current_position.y, NULL);
LineTo(dc, balls.current_position.x, balls.current_position.y + Force_Gravity.y);
ReleaseDC(win,dc);
}
void updatePhysics(void)
{
static oat time = 0;
const oat change_of_time = .9f;
// Update Theata
// Calculate Accelleration by old theata
Force_Tension.x = - 10 * sin(balls.theata);
Force_Tension.y = - 10 * cos(balls.theata);
// The mass has already been divided out of the system...thus subtracting the forces
// gives me the accelleration
balls.current_accelleration.x = Force_Gravity.x - Force_Tension.x;
balls.current_accelleration.y = Force_Gravity.y - Force_Tension.y;
if (balls.theata < 0)
{
balls.current_velocity.x = - balls.current_accelleration.x * (change_of_time);
}
else
{
balls.current_velocity.x = balls.current_accelleration.x * (change_of_time);
}
balls.current_velocity.y = balls.current_accelleration.y * (change_of_time);
// Update Ball Position
// I thin this is ****ing everything up
balls.current_position.x += balls.current_velocity.x * (change_of_time);
balls.current_position.y += balls.current_velocity.y * (change_of_time);
https://www.physicsforums.com/threads/c-win32-physics-simulation.165914/
4/5
5.10.2017
C++ / Win32 / Physics Simulation | Physics Forums - The Fusion of Science and Community
// Calculate my new theata with the dot product
// I will try and dot the current rope with the force of gravity
//balls.theata = I keep trying the dotProduct of gravity and current ball position
// and it never seems to work
// what am I doing wrong
}
Phys.org - latest science and technology news stories
on Phys.org
• Game
over? Computer beats human champ in ancient Chinese game
• Simplifying
solar cells with a new mix of materials
• Imaged
'jets' reveal cerium's post-shock inner strength
neurocomp2003
printf (or cout) is your friend....see if the values are what you expect them to be.
u might wanna clean up the code so its readable
also printout gravity
#2
Apr 15, 2007
and it might help to explain what is teh ball doing thats erroneous (besides sayign its not oscillating)...is it going
to in nity.
from rst look it might be sin/cos values...where your using oats.
Know someone interested in this topic? Share this thread via
Reddit,
Google+,
Twitter,
or
Facebook
Relativity Variables: Velocity,
Doppler-Bondi k, and Rapidity
Interview with Astronomer
Pamela Gay
Interview with a Theoretical
Physicist: Sabine Hossenfelder
Super p-Brane Theory Emerging Damped Motion in Classical and
from Super Homotopy Theory
Quantum Mechanics
Forums
Science Education
Homework and Coursework Questions
Advanced Physics Homework
CONTACT US
© 2001-2017 Physics Forums
HELP
Terms and Rules Privacy Policy
https://www.physicsforums.com/threads/c-win32-physics-simulation.165914/
5/5
Plik z chomika:
jacekplacekjacek
Inne pliki z tego folderu:
professional-c-4-0-and-net-4.pdf
(44260 KB)
addison-wesley-windows-system-programming-4ed-mtshart2010.pdf
(10460 KB)
Visual C++ and MFC Programming 2nd.pdf
(9754 KB)
chand_colorfigs.pdf
(5736 KB)
Introduction to C++ Programming and Graphics.pdf
(3705 KB)
Inne foldery tego chomika:
Arduino
Asembler
C++
DirectX
GDB
Zgłoś jeśli
naruszono regulamin