775699f0353cab22c81bb58696e4792011fd1a2b
[matches/honours.git] / nbody.h
1 /**
2  * @file nbody.h
3  * @purpose Declarations for N-Body simulator
4  * @author Sam Moore
5  * @date August 2012
6  */
7
8 #ifndef _NBODY_H
9 #define _NBODY_H
10
11 #include <stdlib.h>
12 #include <stdbool.h>
13
14 #include "graphics.h" //TODO: Comment out this line to build the program without any graphics at all
15
16 #define DIMENSIONS 2 //Number of dimensions to use in the simulation (2 or 3)
17
18 /** 
19  * Structure to represent a single Body
20  */
21
22 typedef struct
23 {
24         float m; //Mass
25         float x[DIMENSIONS]; //Position vector
26         float v[DIMENSIONS]; //Velocity vector
27         float F[DIMENSIONS]; //Net force due to all other bodies in the System
28
29         bool exists; //Used by memory management
30 } Body;
31
32 /**
33  * Structure to represent a system of N Bodies
34  */
35 typedef struct
36 {
37         Body * bodies; //Array of Bodies
38         unsigned nMax; //Size of array; maximum number of Bodies before Array needs to be resized
39         unsigned n; //Number of bodies currently in the system
40         float G; //Universal Gravitational Constant; used to calculate forces between Bodies
41
42         float dt; //Time interval
43         unsigned step; //Number of steps executed by the System
44
45         float x[DIMENSIONS]; //The origin of the system
46
47 } System;
48
49 /**
50  * Functions for operating with Body objects
51  */
52 extern void Body_Init(Body * body, float m, float x[DIMENSIONS], float v[DIMENSIONS]); //Initialise Body
53 extern void Body_Destroy(Body * body); //Destroy Body
54 extern void Body_Step(Body * body, System * system); //Step Body
55 extern void Body_Force(Body * A, Body * B, System * system); //Calculate Force between Bodies
56
57
58 /**
59  * Functions for operating with System object
60  */
61 extern void System_Init(System * system, float x[DIMENSIONS], float dt, float G, unsigned nMax); //Initialise System
62 extern void System_Destroy(System * system); //Destroy System
63 extern void System_Step(System * system); //Step all Bodies in System
64 extern Body * System_AddBody(System * system, float m, float x[DIMENSIONS], float v[DIMENSIONS]); //Add Body to System
65 extern void System_RemoveBody(System * system, Body * body); //
66 extern void System_MergeBodies(System * system, Body * A, Body * B);
67 extern void System_WriteData(System * system, FILE * file); //Write System info to a file
68
69
70 /**
71  * Functions to be used when "graphics.h" is included
72  */
73
74 #ifdef _GRAPHICS_H
75         extern void System_Draw(System * system); //Draw a System of Bodies
76         extern void Body_Draw(Body * body, System * system); //Draw an individual Body
77
78         extern void Process_Events(); //Handle any events from the SDL system
79 #endif //_GRAPHICS_H
80
81
82
83 #endif //_NBODY_H
84
85 //EOF

UCC git Repository :: git.ucc.asn.au