body.h 2.95 KB
#ifndef __BODY_H__
#define __BODY_H__

#include "color.h"
#include "list.h"
#include "vector.h"

typedef struct body body_t;

/**
 * Initialize a body object given a list of vertices.
 *
 * @param points the list of vertices that make up the body. The body takes
 * ownership of the list.
 * @param initial_velocity a vector representing the initial velocity of the
 * body
 * @param rotation_speed the rotation angle of the body per unit time
 * @param color the color of the body
 * @return a body object pointer
 */
body_t *body_init(list_t *points, vector_t initial_velocity,
                  double rotation_speed, color_t color);

/**
 * Return the list of vectors representing the vertices of the body.
 *
 * @param body the pointer to the body
 * @return a list of vectors
 */
list_t *body_get_points(body_t *body);

/**
 * Translate and rotate the body.
 *
 * @param body the pointer to the body
 * @param time_elapsed time/# of frames elapsed since the last tick
 */
void body_move(body_t *body, double time_elapsed);

/**
 * Set the x and y components of a body's velocity vector.
 *
 * @param body the pointer to the body
 * @param v_x x component of velocity to set
 * @param v_y y component of velocity to set
 */
void body_set_velocity(body_t *body, double v_x, double v_y);

/**
 * Free memory allocated for object associated with a body.
 *
 * @param body the pointer to the body
 */
void body_free(body_t *body);

/**
 * Return x-component of body's velocity vector.
 *
 * @param body the pointer to the body
 * @return the x-velocity of a body
 */
double body_get_velocity_x(body_t *body);

/**
 * Return y-component of body's velocity vector.
 *
 * @param body the pointer to the body
 * @return the y-velocity of a body
 */
double body_get_velocity_y(body_t *body);

/**
 * Computes the area of a body.
 * 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);

/**
 * Computes the center of mass of a body.
 * See https://en.wikipedia.org/wiki/Centroid#Of_a_polygon.
 *
 * @param body the pointer to the body
 * @return the centroid of the body
 */
vector_t body_centroid(body_t *body);

/**
 * Translates all vertices in a body by a given vector.
 * Note: mutates the original body.
 *
 * @param body the pointer to the body
 * @param translation the vector to add to each vertex's position
 */
void body_translate(body_t *body, vector_t translation);

/**
 * Rotates vertices in a body by a given angle about a given point.
 *
 * @param body the pointer to the body
 * @param angle the angle to rotate the body, in radians.
 * A positive angle means counterclockwise.
 * @param point the point to rotate around
 */
void body_rotate(body_t *body, double angle, vector_t point);

/**
 * Return the 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);

#endif // #ifndef __BODY_H__