6064584f3a9e88342eac3fce2e6b42cb38faa1bd
[matches/honours.git] / course / semester2 / pprog / assignment0 / 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         // Used for detecting and handling collisions
30         float x_old[DIMENSIONS]; //Position on previous step
31         float v_old[DIMENSIONS]; //Velocity on previous step
32
33         bool exists; //Used by memory management
34         bool collided; //Used to enforce "one collision per step"
35
36 } Body;
37
38 /**
39  * Structure to represent a system of N Bodies
40  */
41 typedef struct
42 {
43
44         //--- Physical parameters ---
45
46         float G; //Universal Gravitational Constant; used to calculate forces between Bodies
47         float dt; //Time interval
48         float cor; //Coefficient of restitution for objects in the system
49
50         float x[DIMENSIONS]; //The origin of the system
51
52
53         //--- Memory management and simulation parameters ---
54
55         Body * bodies; //Array of Bodies
56         unsigned nMax; //Size of array; maximum number of Bodies before Array needs to be resized
57         unsigned n; //Number of bodies currently in the system
58         unsigned step; //Number of steps executed by the System
59
60         
61
62 } System;
63
64 /**
65  * Functions for operating with Body objects
66  */
67 extern void Body_Init(Body * body, float m, float x[DIMENSIONS], float v[DIMENSIONS]); //Initialise Body
68 extern void Body_Destroy(Body * body, System * system); //Destroy Body
69 extern void Body_Step(Body * body, System * system); //Step Body
70 extern void Body_Force(Body * A, Body * B, System * system); //Calculate Force between Bodies
71
72 extern bool Body_CheckCollision(Body * A, Body * B, System * system, float * t); //Check for collision between Bodies
73 extern void Body_HandleCollision(Body * A, Body * B, System * system, float t); //Handle collision between Bodies
74
75 /**
76  * Functions for operating with System object
77  */
78 extern void System_Init(System * system, float x[DIMENSIONS], float dt, float G, float cor, unsigned nMax); //Initialise System
79 extern void System_Destroy(System * system); //Destroy System
80 extern void System_Step(System * system); //Step all Bodies in System
81 extern Body * System_AddBody(System * system, float m, float x[DIMENSIONS], float v[DIMENSIONS]); //Add Body to System
82 extern void System_RemoveBody(System * system, Body * body); //
83 extern void System_MergeBodies(System * system, Body * A, Body * B);
84 extern void System_WriteData(System * system, FILE * file); //Write System info to a file
85
86
87 /**
88  * Functions to be used when "graphics.h" is included
89  */
90
91 #ifdef _GRAPHICS_H
92         extern void System_Draw(System * system); //Draw a System of Bodies
93         extern void Body_Draw(Body * body, System * system); //Draw an individual Body
94 #endif //_GRAPHICS_H
95
96
97
98 #endif //_NBODY_H
99
100 //EOF

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