summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README2
-rw-r--r--xd.c159
2 files changed, 159 insertions, 2 deletions
diff --git a/README b/README
index 3ab53a7..7a4dfb8 100644
--- a/README
+++ b/README
@@ -1,6 +1,6 @@
BUILDING
--------
-To build just run 'make'. To run, just type 'xd'.
+To build just run 'make'. To run, just type './xd'.
DESCRIPTION
diff --git a/xd.c b/xd.c
index cb02ab5..8d043a7 100644
--- a/xd.c
+++ b/xd.c
@@ -169,10 +169,11 @@ static void TriangleDemo(void);
static void TriangleDemo2(void);
static void FollowDemo(void);
static void CatwalkDemo(void);
+static void GravityDemo(void);
/* ---------------------------------------- GLOBAL VARS
*/
-#define NO_DEMOS 17
+#define NO_DEMOS 18
static int demo=NO_DEMOS-1;
@@ -265,6 +266,11 @@ static DemoInfo demotbl[NO_DEMOS]=
CatwalkDemo,
UsesPixmap
},
+ {
+ "Gravity Demo (LMB to reset, RMB to move central mass)",
+ GravityDemo,
+ UsesPixmap
+ },
};
/* ---------------------------------------- X11 VARS
@@ -3895,4 +3901,155 @@ static void CatwalkDemo(void)
#undef TRENCH_Z_MIN
+/* ---------------------------------------- GRAVITY DEMO
+ */
+
+#define GRAV_OBJ 20
+#define GRAV_TRAIL 200
+#define GRAV_SIZE 4
+
+static void GravityDemo(void)
+{
+ static int init = FALSE;
+
+ typedef struct
+ {
+ double x,y,dx,dy;
+ int col;
+ int mass;
+ double tx[GRAV_TRAIL];
+ double ty[GRAV_TRAIL];
+ } GravObj;
+
+ static GravObj gravobj[GRAV_OBJ];
+
+ int f,r;
+
+ if (!init || mouse_b&Button1Mask)
+ {
+ for(f = 0; f < GRAV_OBJ; f++)
+ {
+ if (f == 0)
+ {
+ gravobj[f].x = centre_x;
+ gravobj[f].y = centre_y;
+ gravobj[f].dx = 0;
+ gravobj[f].dy = 0;
+ gravobj[f].mass = 80;
+ }
+ else
+ {
+ int a;
+ int d;
+
+ a = RND(3600);
+ d = RND2(400,200);
+
+ gravobj[f].x = centre_x + sintab[a] * d;
+ gravobj[f].y = centre_y + costab[a] * d;
+
+ a = (a + 900) % 3600;
+
+ gravobj[f].dx = sintab[a] * 10;
+ gravobj[f].dy = costab[a] * 10;
+
+ /*
+ gravobj[f].x = RND(width);
+ gravobj[f].y = RND(height);
+ gravobj[f].dx = (double)RND(10)/10.0;
+ gravobj[f].dy = (double)RND(10)/10.0;
+ */
+
+ gravobj[f].mass = RND2(10, 2);
+ }
+
+ gravobj[f].col = GetCol(RND2(255,128),
+ RND2(255,100),
+ RND2(255,100));
+
+ for(r = 0; r < GRAV_TRAIL; r++)
+ {
+ gravobj[f].tx[r] = gravobj[f].x;
+ gravobj[f].ty[r] = gravobj[f].y;
+ }
+ }
+
+ init=TRUE;
+ }
+
+ Cls();
+
+ if (mouse_b & Button3Mask)
+ {
+ gravobj[0].x = mouse_x;
+ gravobj[0].y = mouse_y;
+ }
+
+ for(f = 1; f < GRAV_OBJ; f++)
+ {
+ for(r = 0; r < GRAV_OBJ; r++)
+ {
+ if (f != r)
+ {
+ double d;
+ double g;
+ double dx;
+ double dy;
+
+ dx = gravobj[f].x - gravobj[r].x;
+ dy = gravobj[f].y - gravobj[r].y;
+
+ d = sqrt(dx*dx + dy*dy);
+
+ g = 1.0 / d*d;
+
+ g /= 100.0;
+ g /= gravobj[f].mass;
+
+ dx /= d;
+ dy /= d;
+
+ dx *= g;
+ dy *= g;
+
+ gravobj[f].dx -= dx;
+ gravobj[f].dy -= dy;
+ }
+ }
+ }
+
+ for(f = 0; f < GRAV_OBJ; f++)
+ {
+ gravobj[f].x += gravobj[f].dx / 50.0;
+ gravobj[f].y += gravobj[f].dy / 50.0;
+
+ for(r = GRAV_TRAIL - 1; r > 0; r--)
+ {
+ gravobj[f].tx[r] = gravobj[f].tx[r-1];
+ gravobj[f].ty[r] = gravobj[f].ty[r-1];
+ }
+
+ gravobj[f].tx[0] = gravobj[f].x;
+ gravobj[f].ty[0] = gravobj[f].y;
+
+ for(r = 0; r < GRAV_TRAIL - 1; r++)
+ {
+ Line(gravobj[f].tx[r], gravobj[f].ty[r],
+ gravobj[f].tx[r+1], gravobj[f].ty[r+1],
+ gravobj[f].col);
+ }
+ }
+
+ for(f = 0; f < GRAV_OBJ; f++)
+ {
+ FCircle(gravobj[f].x, gravobj[f].y,
+ gravobj[f].mass/GRAV_SIZE, gravobj[f].col);
+ }
+}
+
+#undef GRAV_OBJ
+#undef GRAV_TRAIL
+#undef GRAV_SIZE
+
+
/* END OF FILE */