From 7aa15ed38fcf1aa2562152eee62fe8a1cb551473 Mon Sep 17 00:00:00 2001 From: "Stephen M. Cameron" Date: Wed, 5 Aug 2015 18:19:57 -0700 Subject: [PATCH] Add function to compute elastic collision response Signed-off-by: Stephen M. Cameron --- Makefile | 3 +++ elastic_collision.c | 61 +++++++++++++++++++++++++++++++++++++++++++++ elastic_collision.h | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 elastic_collision.c create mode 100644 elastic_collision.h diff --git a/Makefile b/Makefile index 5a87e695..c8138dd7 100644 --- a/Makefile +++ b/Makefile @@ -697,6 +697,9 @@ arbitrary_spin.o: arbitrary_spin.c arbitrary_spin.h Makefile mtwist.o: mtwist.c Makefile $(Q)$(COMPILE) +elastic_collision.o: elastic_collision.c elastic_collision.h Makefile + $(Q)$(COMPILE) + fleet.o: fleet.c Makefile $(Q)$(COMPILE) diff --git a/elastic_collision.c b/elastic_collision.c new file mode 100644 index 00000000..5d4097d8 --- /dev/null +++ b/elastic_collision.c @@ -0,0 +1,61 @@ +/* + Copyright (C) 2015 Stephen M. Cameron + Author: Stephen M. Cameron + + This file is part of Spacenerds In Space. + + Spacenerds in Space is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + Spacenerds in Space is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Spacenerds in Space; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "elastic_collision.h" + +void elastic_collision(float m1, union vec3 *p1, union vec3 *v1i, float r1, + float m2, union vec3 *p2, union vec3 *v2i, float r2, + float energy_transmission_factor, union vec3 *v1o, union vec3 *v2o) +{ + union vec3 to2, to2n, to1n, deltav1, deltav2; + float dist; + + vec3_sub(&to2, p2, p1); + dist = vec3_dist(p1, p2); + + vec3_normalize(&to2n, &to2); + to1n.v.x = -to2n.v.x; + to1n.v.y = -to2n.v.y; + to1n.v.z = -to2n.v.z; + + /* Separate the particles, if necessary */ + if (dist < r1 + r2) { + /* split the difference, plus a little */ + float amount = (1.01 * (r1 - r2) - dist) / 2.0; + union vec3 d1, d2; + vec3_mul(&d1, &to1n, amount); + vec3_mul(&d2, &to2n, amount); + vec3_add_self(p1, &d1); + vec3_add_self(p2, &d2); + } + + /* transfer momentum... */ + float p1momentum = vec3_dot(v1i, &to2n) * m1 / m2; + float p2momentum = vec3_dot(v2i, &to1n) * m2 / m1; + + p1momentum *= sqrt(energy_transmission_factor); + p2momentum *= sqrt(energy_transmission_factor); + + vec3_mul(&deltav1, &to1n, p2momentum); + vec3_mul(&deltav2, &to2n, p1momentum); + + vec3_add(v1o, v1i, &deltav1); + vec3_add(v2o, v2i, &deltav2); +} diff --git a/elastic_collision.h b/elastic_collision.h new file mode 100644 index 00000000..0d401d54 --- /dev/null +++ b/elastic_collision.h @@ -0,0 +1,44 @@ +#ifndef ELASTIC_COLLISION_H__ +#define ELASTIC_COLLISION_H__ + +/* + Copyright (C) 2015 Stephen M. Cameron + Author: Stephen M. Cameron + + This file is part of Spacenerds In Space. + + Spacenerds in Space is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + Spacenerds in Space is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Spacenerds in Space; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include + +#include "quat.h" + +/* + * elastic_collision(): + * + * Inputs: + * m1, p1, v1i, and r1 are mass, position, input velocity and radius of particle 1. + * m2, p2, v2i, and r2 are mass, position, input velocity and radius of particle 2. + * + * Outputs: + * p1, and p2 are output as adjusted positions of particles (separated by at least r1 + r2 after + * collisions) + * v1o and v2o are new velocities of particles 1 and 2, respectively. + */ +void elastic_collision(float m1, union vec3 *p1, union vec3 *v1i, float r1, + float m2, union vec3 *p2, union vec3 *v2i, float r2, + float energy_transmission_factor, union vec3 *v1o, union vec3 *v2o); + +#endif