Enlightenment CVS committal Author : pfritz Project : e17 Module : proto/e_phys
Dir : e17/proto/e_phys/src/lib Modified Files: E_Phys.h Makefile.am e_phys_particle.c e_phys_world.c Added Files: e_phys_constraint.h e_phys_force.h e_phys_particle.h e_phys_world.h Log Message: *API BREAK* Here comes the first step of the e_phys API overhaul. There will be following some other changes. If you are using or plan to use e_phys, please, let it me now, so maybe we can find the best matching API. (Note: the core is untouched, the math and the algorithms are still the same) =================================================================== RCS file: /cvs/e/e17/proto/e_phys/src/lib/E_Phys.h,v retrieving revision 1.6 retrieving revision 1.7 diff -u -3 -r1.6 -r1.7 --- E_Phys.h 27 Nov 2006 21:38:06 -0000 1.6 +++ E_Phys.h 21 Nov 2007 23:34:50 -0000 1.7 @@ -1,3 +1,9 @@ +#ifndef E_PHYS_H +#define E_PHYS_H + +#ifdef __cplusplus +extern "C" { +#endif #include <Evas.h> #include <Ecore.h> @@ -5,177 +11,30 @@ #include <stdio.h> #include <math.h> -struct _E_Phys_Point +/** + * Type definition of the point + */ +typedef struct E_Phys_Point E_Phys_Point; + +/** + * The structur for the point + */ +struct E_Phys_Point { float x, y; }; -typedef struct _E_Phys_Point E_Phys_Point; -typedef struct _E_Phys_Point E_Phys_Vector; -typedef struct _E_Phys_Particle E_Phys_Particle; - -typedef struct _E_Phys_Constraint E_Phys_Constraint; -typedef struct _E_Phys_Force E_Phys_Force; -typedef struct _E_Phys_Force_Gravity E_Phys_Force_Gravity; -typedef struct _E_Phys_Force_Spring E_Phys_Force_Spring; -typedef struct _E_Phys_Force_Collision E_Phys_Force_Collision; -typedef struct _E_Phys_Force_Uniform E_Phys_Force_Constant; -typedef struct _E_Phys_Force_Uniform E_Phys_Force_Uniform; -typedef struct _E_Phys_Constraint_Boundary E_Phys_Constraint_Boundary; -typedef struct _E_Phys_Constraint_Stick E_Phys_Constraint_Stick; -typedef struct _E_Phys_Constraint_Anchor E_Phys_Constraint_Anchor; -typedef struct _E_Phys_World E_Phys_World; - -E_Phys_World *e_phys_world_add(int w, int h); -void e_phys_world_free(E_Phys_World *world); -void e_phys_world_go(E_Phys_World *world); -void e_phys_world_stop(E_Phys_World *world); -void e_phys_world_update_func_set(E_Phys_World *world, - void (*func) (void *data, E_Phys_World *world), - void *data); -void e_phys_world_size_set(E_Phys_World *world, int w, int h); -E_Phys_Particle *e_phys_world_nearest_particle(E_Phys_World *world, - int x, int y); - -E_Phys_Particle *e_phys_particle_add(E_Phys_World *world, float mass, - float x, float y, float vx, float vy); -void e_phys_particle_free(E_Phys_Particle *p); -void e_phys_particle_size_set(E_Phys_Particle *p, - float w, float h); - -void e_phys_force_init(E_Phys_Force *force, E_Phys_World *world, - void (*apply_func) (E_Phys_Force *force), - void (*free_func) (E_Phys_Force *force)); -void e_phys_force_free(E_Phys_Force *force); - -E_Phys_Force_Collision *e_phys_force_collision_add(E_Phys_World *world); -E_Phys_Force_Spring *e_phys_force_spring_add(E_Phys_World *world, - E_Phys_Particle *p1, E_Phys_Particle *p2, int k, int len); -E_Phys_Force_Spring *e_phys_force_modified_spring_add(E_Phys_World *world, - E_Phys_Particle *p1, E_Phys_Particle *p2, int k, int len); -E_Phys_Force_Gravity *e_phys_force_gravity_add(E_Phys_World *world, float g); -E_Phys_Force_Constant *e_phys_force_constant_add(E_Phys_World *world, E_Phys_Vector force, int is_acceleration); -E_Phys_Force_Uniform *e_phys_force_uniform_add(E_Phys_World *world, E_Phys_Vector (*force_func) (float t), int is_acceleration); - - -void e_phys_constraint_init(E_Phys_Constraint *con, E_Phys_World *world, - void (*apply_func) (E_Phys_Constraint *con), - void (*free_func) (E_Phys_Constraint *con)); -void e_phys_constraint_del(E_Phys_Constraint *con); -void e_phys_constraint_free(E_Phys_Constraint *con); - -E_Phys_Constraint_Boundary *e_phys_constraint_boundary_add(E_Phys_World *world); -E_Phys_Constraint_Stick *e_phys_constraint_stick_add( - E_Phys_Particle *p1, - E_Phys_Particle *p2, - int len); -E_Phys_Constraint_Anchor *e_phys_constraint_anchor_add( - E_Phys_Particle *p, float x, float y); - -E_Phys_Particle *e_phys_particle_map_insert( - E_Phys_Particle *head, - E_Phys_Particle *p); - - - -#define E_PHYS_CONSTRAINT(x) ((E_Phys_Constraint *)x) -#define E_PHYS_FORCE(x) ((E_Phys_Force *)x) - -enum _E_Phys_Particle_Direction { - E_PHYS_PARTICLE_DIRECTION_LEFT, - E_PHYS_PARTICLE_DIRECTION_RIGHT, - E_PHYS_PARTICLE_DIRECTION_ABOVE, - E_PHYS_PARTICLE_DIRECTION_BELOW, - E_PHYS_PARTICLE_DIRECTION_MAX -}; - -typedef enum _E_Phys_Particle_Direction E_Phys_Particle_Direction; - -struct _E_Phys_Particle { - E_Phys_World *world; - float m; - float w, h; // extended size - E_Phys_Point cur; - E_Phys_Point prev; - E_Phys_Vector force; - unsigned int steps; - E_Phys_Particle *neighbor[E_PHYS_PARTICLE_DIRECTION_MAX]; -}; - -struct _E_Phys_Constraint { - E_Phys_World *world; - void (*apply) (E_Phys_Constraint *con); - void (*free) (E_Phys_Constraint *con); -}; - -struct _E_Phys_Force { - E_Phys_World *world; - void (*apply) (E_Phys_Force *force); - void (*free) (E_Phys_Force *force); -}; - -struct _E_Phys_Force_Collision { - E_Phys_Force force; - float e; // elasticity? -}; - -struct _E_Phys_Force_Spring { - E_Phys_Force force; - E_Phys_Particle *p1; - E_Phys_Particle *p2; - float len; - float k; - int modified; -}; - -struct _E_Phys_Force_Gravity { - E_Phys_Force force; - float g; -}; - -struct _E_Phys_Force_Uniform { - E_Phys_Force force; - E_Phys_Vector const_force; - E_Phys_Vector (*force_func) (float t); - int is_acceleration; -}; - -struct _E_Phys_Constraint_Boundary { - E_Phys_Constraint con; - float e; // elasticity (0 fully inelastic -> 1 fully elastic) - Evas_List *exclusions; -}; - -struct _E_Phys_Constraint_Stick { - E_Phys_Constraint con; - E_Phys_Particle *p1; - E_Phys_Particle *p2; - float len; -}; - -struct _E_Phys_Constraint_Anchor { - E_Phys_Constraint con; - E_Phys_Particle *p; - E_Phys_Point pos; -}; - -struct _E_Phys_World -{ - int w, h; - - Evas_List *particles; - Evas_List *forces; - Evas_List *constraints; - - Ecore_Timer *timer; - - double time; - - double dt; - double elapsed; - int constraint_iter; - float friction; - - void (*update_func) (void *data, E_Phys_World *world); - void *update_data; -}; +/** + * Type definition of the vector + */ +typedef E_Phys_Point E_Phys_Vector; + +#include <e_phys_world.h> +#include <e_phys_particle.h> +#include <e_phys_force.h> +#include <e_phys_constraint.h> + +#ifdef __cplusplus +} +#endif +#endif =================================================================== RCS file: /cvs/e/e17/proto/e_phys/src/lib/Makefile.am,v retrieving revision 1.8 retrieving revision 1.9 diff -u -3 -r1.8 -r1.9 --- Makefile.am 22 Mar 2007 16:48:43 -0000 1.8 +++ Makefile.am 21 Nov 2007 23:34:50 -0000 1.9 @@ -2,14 +2,22 @@ lib_LTLIBRARIES = libephys.la -include_HEADERS = E_Phys.h +EPHYSHEADERS = \ + E_Phys.h \ + e_phys_world.h \ + e_phys_particle.h \ + e_phys_force.h \ + e_phys_constraint.h libephys_la_SOURCES = \ - E_Phys.h \ e_phys_constraint.c \ e_phys_force.c \ e_phys_particle.c \ e_phys_world.c \ - e_phys_map.c + e_phys_map.c \ + $(EPHYSHEADERS) + +installed_headersdir = $(includedir)/e_phys +installed_headers_DATA = $(EPHYSHEADERS) libephys_la_LIBADD = @EPHYS_LIBS@ -lm =================================================================== RCS file: /cvs/e/e17/proto/e_phys/src/lib/e_phys_particle.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -3 -r1.2 -r1.3 --- e_phys_particle.c 7 Oct 2007 01:04:45 -0000 1.2 +++ e_phys_particle.c 21 Nov 2007 23:34:50 -0000 1.3 @@ -2,32 +2,153 @@ /** * @param world - the world to work with - * @param mass - The mass of the new particle - * @param x - The x coordinate of the location - * @param y - The y coordinate of the location - * @param v_x - The x component of the velocity (FIXME: what is the unit?) - * @param v_y - The y component of the velocity * @return Returns the new created particle * @brief Creates a new particle. */ E_Phys_Particle * -e_phys_particle_add(E_Phys_World *world, float mass, float x, float y, float v_x, float v_y) +e_phys_particle_add(E_Phys_World *world) { E_Phys_Particle *p; p = calloc(1, sizeof(E_Phys_Particle)); p->world = world; - - p->m = mass; - p->cur.x = x; - p->cur.y = y; - p->prev.x = x - v_x; - p->prev.y = y - v_y; + p->m = 1.0; world->particles = evas_list_append(world->particles, p); return p; } /** + * @param p - the particles to work with + * @param mass - The mass of the new particle + * @return Returns no value. + * @brief Set the mass of the particle. + */ +void +e_phys_particle_mass_set(E_Phys_Particle *p, float mass) +{ + p->m = mass; +} + +/** + * @param p - the particles to work with + * @return Returns the mass of the particle. + * @brief Retrieve the mass of the particle. + */ +float +e_phys_particle_mass_get(E_Phys_Particle *p) +{ + return p->m; +} + +/** + * @param p - the particle to move + * @param pos - The position to move the particle + * @return Returns no value. + * @brief Move the particle to the new postion + */ +void +e_phys_particle_move(E_Phys_Particle *p, const E_Phys_Point *pos) +{ + p->cur = *pos; +} + +/** + * @param p - the particle to place + * @param pos - The position to place the particle + * @return Returns no value. + * @brief Place the particle to the new postion + */ +void +e_phys_particle_place(E_Phys_Particle *p, const E_Phys_Point *pos) +{ + p->cur = *pos; + p->prev = *pos; +} + +/** + * @param p - the particle to work with + * @return Returns the current position of the particle + * @brief Retrieve the current position of the particle + */ +E_Phys_Point +e_phys_particle_current_position_get(E_Phys_Particle *p) +{ + return p->cur; +} + +/** + * @param p - the particle to work with + * @return Returns the previous position of the particle + * @brief Retrieve the previous position of the particle + */ +E_Phys_Point +e_phys_particle_previous_position_get(E_Phys_Particle *p) +{ + return p->prev; +} + +/** + * @param p - the particle to work with + * @param vel - The new velocity of the particle (FIXME: what is the unit?) + * @return Returns no value. + * @brief Set a new velocity of the particle + */ +void +e_phys_particle_velocity_set(E_Phys_Particle *p, const E_Phys_Vector *vel) +{ + p->prev.x = p->cur.x - vel->x; + p->prev.y = p->cur.y - vel->y; +} + +/** + * @param p - the particle to work with + * @return Returns the velocity of the particle + * @brief Retrieve the velocity of the particle + */ +E_Phys_Vector +e_phys_particle_velocity_get(E_Phys_Particle *p) +{ + E_Phys_Vector v = {p->cur.x - p->prev.x, p->cur.y - p->prev.y}; + return v; +} + +/** + * @param p - the particle to work with + * @param f - The force to add to the particle + * @return Returns no value. + * @brief Add a force to the particle + */ +void +e_phys_particle_force_add(E_Phys_Particle *p, const E_Phys_Vector *f) +{ + p->force.x += f->x; + p->force.y += f->y; +} + +/** + * @param p - the particle to work with + * @param f - The new force of the particle + * @return Returns no value. + * @brief Set the new force of the particle + */ +void +e_phys_particle_force_set(E_Phys_Particle *p, const E_Phys_Vector *f) +{ + p->force = *f; +} + +/** + * @param p - the particle to work with + * @return Returns the force that is applied on the particle. + * @brief Retrieve the force + */ +E_Phys_Vector +e_phys_particle_force_get(E_Phys_Particle *p) +{ + return p->force; +} + +/** * @param p - the particle to work with * @param w - The new width of the particle * @param h - The new height of the particle @@ -42,6 +163,20 @@ } /** + * @param p - the particle to work with + * @param w - The pointer to save the width of the particle + * @param h - The pointer to save the height of the particle + * @return Returns no value. + * @brief Get the size of the particle + */ +void +e_phys_particle_size_get(E_Phys_Particle *p, float *w, float *h) +{ + if (w) *w = p->w; + if (h) *h = p->h; +} + +/** * @param p - the particle to delete * @return Returns no value. * @brief Remove the given particle from its world and free it @@ -70,3 +205,4 @@ { if (p) free(p); } + =================================================================== RCS file: /cvs/e/e17/proto/e_phys/src/lib/e_phys_world.c,v retrieving revision 1.6 retrieving revision 1.7 diff -u -3 -r1.6 -r1.7 --- e_phys_world.c 7 Oct 2007 01:04:45 -0000 1.6 +++ e_phys_world.c 21 Nov 2007 23:34:50 -0000 1.7 @@ -1,4 +1,5 @@ #include "E_Phys.h" +#include <values.h> static void e_phys_world_accumulate_forces(E_Phys_World *world); static void e_phys_world_verlet_integrate(E_Phys_World *world); @@ -12,13 +13,13 @@ * @brief Create a new world */ E_Phys_World * -e_phys_world_add(int w, int h) +e_phys_world_add(void) { E_Phys_World *world; world = calloc(1, sizeof(E_Phys_World)); world->constraint_iter = 10; - world->w = w; - world->h = h; + world->w = MAXFLOAT; + world->h = MAXFLOAT; world->dt = 1.0 / 60.0; return world; } @@ -57,6 +58,7 @@ e_phys_world_timer(void *data) { E_Phys_World *world; + double t; int i; world = data; @@ -126,7 +128,6 @@ E_Phys_Particle *nearest = NULL; float distance = 0.0; - for (l = world->particles; l; l = l->next) { E_Phys_Particle *p; @@ -156,11 +157,12 @@ * @brief set the new size of the given world. */ void -e_phys_world_size_set(E_Phys_World *world, int w, int h) +e_phys_world_size_set(E_Phys_World *world, float w, float h) { world->w = w; world->h = h; } + static void e_phys_world_accumulate_forces(E_Phys_World *world) { @@ -198,7 +200,6 @@ tmp.x = p->cur.x; tmp.y = p->cur.y; - //printf("force: (%0.2f, %0.2f)\n", p->force.x, p->force.y); p->cur.x = (2 - world->friction) * p->cur.x - (1 - world->friction) * p->prev.x + p->force.x * world->dt * world->dt / p->m; p->cur.y = (2 - world->friction) * p->cur.y - (1 - world->friction) *p->prev.y + p->force.y * world->dt * world->dt / p->m; ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ enlightenment-cvs mailing list enlightenment-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs