Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add function to compute elastic collision response
Signed-off-by: Stephen M. Cameron <stephenmcameron@gmail.com>
  • Loading branch information
smcameron committed Aug 7, 2015
1 parent 782c624 commit 7aa15ed
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -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)

Expand Down
61 changes: 61 additions & 0 deletions 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);
}
44 changes: 44 additions & 0 deletions 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 <math.h>

#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

0 comments on commit 7aa15ed

Please sign in to comment.