/* Physics: Given two objects with masses M1 and M2, the gravitationl force between these objects is F = (G*M1*M2)/(d*d), where d is the distance between the center of gravity of the two objects. Newton also says that F=MA, so A1 = G*M2/(d*d) is the accelartion caused by the pull of M2 on M1, and A2 = G*M1/(d*d) is that caused by M1 on M2. The gravitational constant G is 6.673e-11 meters^3/(kg*sec^2) in our universe (yours may be different :) Acceleration is given in meters/sec^2 Distances are in meters and mass in kg. */ #include "boxworld.h" #include class darea; class dwindow; class body; // "box" universe dimensions: #define UW 800 #define UH 600 #define UD 400 // gravitational constant in units of meters^3/(kg*sec^2) #define G 6.673e-11 // mass of earth, sun in kg: #define MEARTH 5.9742e24 #define MSUN 1.99e30 // Simulation Scaling factors, UDIST = distance represented by one pixel, // UTIME = seconds represented by each animation frame: #define UDIST 1.0e8 //#define UTIME 1 //#define UTIME 60 //#define UTIME 360 #define UTIME 3600 //#define UTIME 86400 //#define UTIME 2629800 //#define UTIME 31557600 //#define UTIME 31557600.0e3 // time unit between frames - in seconds, minutes, hours, days, etc ... // Real-time delay between animation frames #define FDELAY 1.0 #define GGC Glib::RefPtr // Main class that contains the simulation: class darea : public Gtk::DrawingArea { public: darea(int x=0, int y=0); virtual bool on_expose_event(GdkEventExpose* event); // inherited method Glib::RefPtr makecolorgc(short r, short g, short b); void darea::displaytext(const char *c, Glib::RefPtr colorgc, int x, int y ); int width, height; boxworld box; // 3d boxworld context Glib::RefPtr dbuffer; // offscreen buffer Glib::RefPtr win; // handle on window Glib::RefPtr blackgc; // gcs for drawing different colors Glib::RefPtr whitegc; Glib::RefPtr rgc, bgc, ggc; int i; // some counter bool started; void animate(); void iroutine(); }; // don't touch this class: class dwindow : public Gtk::Window { public: dwindow(); darea canvas; }; class body { public: double mass; // in kg int radius; // radius in screen pixels double xcord, ycord, zcord; // spacial coordinates double xvel, yvel, zvel; // velocity vector // acceleration vector to be calculated dynamically body(double m, int r, double x, double y, double z); // constructor void calcforce(body *B); void updateposition(); };