Lephisto/src/physics.h

62 lines
2.2 KiB
C

#pragma once
#include <math.h>
#define VX(v) ((v).x) /**< Get the X component of a vector. */
#define VY(v) ((v).y) /**< Get the Y component of a vector. */
#define VMOD(v) ((v).mod) /**< Get the modulus of a vector. */
#define VANGLE(v) ((v).angle) /**< Get the angle of a vector. */
#define MOD(x,y) (sqrt((x)*(x) + (y)*(y)))/**<Get the modulus of a vector by cartesion coords.*/
#define ANGLE(x,y) (atan2(y,x)) /**< Get the angle of two cartesian coords. */
#define vect_dist(v,u) MOD((v)->x-(u)->x, (v)->y-(u)->y)/**<Get the distance between two vectors.*/
#define vect_odist(v) MOD((v)->x, (v)->y) /**< Get the distance of a vector from the origin. */
/**
* @brief Represents a 2d vector.
*/
typedef struct Vec2_ {
double x, y; /**< Cartesian values. */
double mod; /**< Modulus of the vector. */
double angle; /**< Angle of the vector. */
} Vec2;
/* Misc */
double angle_diff(const double ref, double a);
void limit_speed(Vec2* vel, const double speed, const double dt);
/* Vector manupulation. */
void vect_cset(Vec2* v, const double x, const double y);
/* Doesn't set mod nor angle. */
void vect_csetmin(Vec2* v, const double x, const double y);
void vect_pset(Vec2* v, const double mod, const double angle);
void vectcpy(Vec2* dest, const Vec2* src);
void vectnull(Vec2* v);
double vect_angle(const Vec2* ref, const Vec2* v);
void vect_cadd(Vec2* v, const double x, const double y);
void vect_reflect(Vec2* r, Vec2* v, Vec2* n);
double vect_dot(Vec2* a, Vec2* b);
/**
* @brief Represents a solid in the game.
*/
typedef struct Solid_ {
double mass; /**< Solids mass. */
double dir; /**< Direction solid is facing. */
double dir_vel; /**< Velocity at which solid is rotating. */
Vec2 vel; /**< Velocity of the solid. */
Vec2 pos; /**< Position of the solid. */
Vec2 force; /**< Forces acting on the solid. */
void(*update)(struct Solid_*, const double); /**< Update method. */
} Solid;
/* Solid manipulation. */
void solid_init(Solid* dest, const double mass, const double dir,
const Vec2* pos, const Vec2* vel);
Solid* solid_create(const double mass, const double dir,
const Vec2* pos, const Vec2* vel);
void solid_free(Solid* src);