Why Doesn't My Win32 Pendulum Simulation Oscillate?

In summary, this is a simple simulation of a pendulum using forces, done in Win32 API. The program does not oscillate and the ball's behavior is not as expected. The equations used are acceleration = force_tension - force_gravity / mass, and the ball's position is calculated using the dot product of the rope and the force of gravity. Further debugging is needed to determine the cause of the unexpected behavior.
  • #1
stringa
7
0

Homework Statement



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);

Homework Equations



accelleration = Force_Tension - Force_Gravity / mass


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>

#define PI 3.14159f

struct _2Dfloat {

float x;
float y;

};

struct ball {

_2Dfloat current_position;
_2Dfloat current_velocity;
_2Dfloat current_accelleration;

int length_of_rope;
POINT rope_origin;

float theata;

};

float sqrt_g_over_r;

ball balls;

_2Dfloat Force_Gravity = {0, 10};
_2Dfloat 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;

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


void draw(HWND win) {
static COLORREF color = RGB(255,128,128);
HDC dc;
HBRUSH brush;
RECT rect;

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 float time = 0;

const float 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);

// 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

}
 
Physics news on Phys.org
  • #2
printf (or cout) is your friend...see if the values are what you expect them to be.

u might want to clean up the code so its readable

also printout gravity

and it might help to explain what is teh ball doing that's erroneous (besides sayign its not oscillating)...is it going to infinity.

from first look it might be sin/cos values...where your using floats.
 

FAQ: Why Doesn't My Win32 Pendulum Simulation Oscillate?

1. What is C++?

C++ is a high-level programming language that is used to create efficient and flexible software. It is widely used in developing operating systems, computer games, and other applications that require complex algorithms and high performance.

2. What is Win32?

Win32 is a programming interface (API) that is used to create applications for the Windows operating system. It provides a set of functions, data types, and constants that developers can use to access system resources and create graphical user interfaces.

3. What is physics simulation?

Physics simulation is the process of using mathematical algorithms to simulate real-world physical phenomena, such as gravity, collisions, and motion. In the context of C++ and Win32, physics simulation is often used to create realistic animations and simulations in computer graphics and video games.

4. How is C++ used in physics simulation?

C++ is a popular language for creating physics simulations due to its speed, efficiency, and ability to directly access system resources. Developers can use C++ to write algorithms that accurately model and simulate physical interactions, and then use Win32 to create a user interface for the simulation.

5. Are there any libraries or frameworks for C++/Win32 physics simulation?

Yes, there are several libraries and frameworks available for C++/Win32 physics simulation, such as Bullet, Box2D, and PhysX. These libraries provide pre-written code for common physics functions, making it easier for developers to create simulations without having to write everything from scratch.

Similar threads

Back
Top