1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifndef __BODY_H__
#define __BODY_H__
#include <stdbool.h>
#include "color.h"
#include "list.h"
#include "vector.h"
/**
* A rigid body constrained to the plane.
* Implemented as a polygon with uniform density.
*/
typedef struct body body_t;
/**
* Allocates memory for a body with the given parameters.
* Gains ownership of the shape list.
* The body is initially at rest.
* Asserts that the required memory is allocated.
*
* @param shape a list of vectors describing the initial shape of the body
* @param mass the mass of the body (if INFINITY, stops the body from moving).
* For now, the mass is unused. It will be needed next week for accelerations.
* @param color the color of the body, used to draw it on the screen
* @return a pointer to the newly allocated body
*/
body_t *body_init(list_t *shape, double mass, color_t color);
/**
* Gets the current shape of a body.
* Returns a newly allocated vector list, which must be list_free()d.
*
* @param body the pointer to the body
* @return a list of vectors
*/
list_t *body_get_shape(body_t *body);
/**
* Gets the current center of mass of a body.
*
* @param body the pointer to the body
* @return the body's center of mass
*/
vector_t body_get_centroid(body_t *body);
/**
* Translates a body to a new position.
* The position is specified by the position of the body's center of mass.
*
* @param body the pointer to the body
* @param x the body's new centroid
*/
void body_set_centroid(body_t *body, vector_t x);
/**
* Gets the current velocity of a body.
*
* @param body the pointer to the body
* @return the body's velocity vector
*/
vector_t body_get_velocity(body_t *body);
/**
* Changes a body's velocity (the time-derivative of its position).
*
* @param body the pointer to the body
* @param v the body's new velocity
*/
void body_set_velocity(body_t *body, vector_t v);
/**
* Returns a body's area.
* See https://en.wikipedia.org/wiki/Shoelace_formula#Statement.
*
* @param body the pointer to the body
* @return the area of the body
*/
double body_area(body_t *body);
/**
* Returns a body's color.
*
* @param body the pointer to the body
* @return the color_t struct representing the color
*/
color_t body_get_color(body_t *body);
/**
* Sets the display color of a body.
*
* @param body the pointer to the body
* @param color the body's color
*/
void body_set_color(body_t *body, color_t color);
/**
* Gets the rotation angle of a body.
*
* @param body the pointer to the body
* @return the body's rotation angle in radians
*/
double body_get_rotation(body_t *body);
/**
* Changes a body's orientation in the plane.
* The body is rotated about its center of mass.
* Note that the angle is *absolute*, not relative to the current orientation.
*
* @param body the pointer to the body
* @param angle the body's new angle in radians. Positive is counterclockwise.
*/
void body_set_rotation(body_t *body, double angle);
/**
* Moves a body at its current velocity over a given time interval.
*
* @param body the body to tick
* @param dt the number of seconds elapsed since the last tick
*/
void body_tick(body_t *body, double dt);
/**
* Gets the mass of a body.
*
* @param body a pointer to a body returned from body_init()
* @return the body's mass
*/
double body_get_mass(body_t *body);
/**
* Frees memory allocated for a body.
*
* @param body the pointer to the body
*/
void body_free(body_t *body);
#endif // #ifndef __BODY_H__