cloth

diff src/particle.cc @ 0:92983e143a03

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 11 Feb 2013 19:40:36 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/particle.cc	Mon Feb 11 19:40:36 2013 +0200
     1.3 @@ -0,0 +1,167 @@
     1.4 +#include <vector>
     1.5 +#include "opengl.h"
     1.6 +#include "particle.h"
     1.7 +#include "simworld.h"
     1.8 +
     1.9 +Particle::Particle()
    1.10 +	: forces(0, 0, 0), pos(0, 0, 0), velocity(0, 0, 0)
    1.11 +{
    1.12 +	rad = 1.0;
    1.13 +	elast = 0.75;
    1.14 +	mass = 1.0;
    1.15 +	friction = 0.0;
    1.16 +}
    1.17 +
    1.18 +void Particle::add_ignore(const Particle *p)
    1.19 +{
    1.20 +	ignorelist.insert(p);
    1.21 +}
    1.22 +
    1.23 +void Particle::set_radius(float rad)
    1.24 +{
    1.25 +	this->rad = rad;
    1.26 +}
    1.27 +
    1.28 +float Particle::get_radius() const
    1.29 +{
    1.30 +	return rad;
    1.31 +}
    1.32 +
    1.33 +void Particle::set_mass(float m)
    1.34 +{
    1.35 +	mass = m;
    1.36 +}
    1.37 +
    1.38 +float Particle::get_mass() const
    1.39 +{
    1.40 +	return mass;
    1.41 +}
    1.42 +
    1.43 +void Particle::set_elasticity(float e)
    1.44 +{
    1.45 +	elast = e;
    1.46 +}
    1.47 +
    1.48 +float Particle::get_elasticity() const
    1.49 +{
    1.50 +	return elast;
    1.51 +}
    1.52 +
    1.53 +void Particle::set_position(const Vector3 &pos)
    1.54 +{
    1.55 +	this->pos = pos;
    1.56 +}
    1.57 +
    1.58 +void Particle::set_velocity(const Vector3 &vel)
    1.59 +{
    1.60 +	velocity = vel;
    1.61 +}
    1.62 +
    1.63 +Vector3 &Particle::get_position()
    1.64 +{
    1.65 +	return pos;
    1.66 +}
    1.67 +
    1.68 +const Vector3 &Particle::get_position() const
    1.69 +{
    1.70 +	return pos;
    1.71 +}
    1.72 +
    1.73 +Vector3 &Particle::get_velocity()
    1.74 +{
    1.75 +	return velocity;
    1.76 +}
    1.77 +
    1.78 +const Vector3 &Particle::get_velocity() const
    1.79 +{
    1.80 +	return velocity;
    1.81 +}
    1.82 +
    1.83 +void Particle::set_friction(float frict)
    1.84 +{
    1.85 +	friction = frict;
    1.86 +}
    1.87 +
    1.88 +float Particle::get_friction() const
    1.89 +{
    1.90 +	return friction;
    1.91 +}
    1.92 +
    1.93 +void Particle::add_force(const Vector3 &fvec)
    1.94 +{
    1.95 +	forces += fvec;
    1.96 +}
    1.97 +
    1.98 +void Particle::step(SimWorld *world, float dt)
    1.99 +{
   1.100 +	Vector3 accel = forces / mass;
   1.101 +	forces.x = forces.y = forces.z = 0.0f;
   1.102 +
   1.103 +	velocity = velocity * world->damping + accel * dt - velocity * friction * dt;
   1.104 +
   1.105 +	Vector3 newpos = pos + velocity * dt;
   1.106 +
   1.107 +	Ray ray(pos, newpos - pos);
   1.108 +	Collision col;
   1.109 +
   1.110 +	if(world->collision(ray, rad, &col)) {
   1.111 +		pos = col.pos;
   1.112 +		velocity = -velocity.reflection(col.normal) * elast;
   1.113 +	} else {
   1.114 +		pos = newpos;
   1.115 +	}
   1.116 +}
   1.117 +
   1.118 +bool Particle::collision(const Particle *p2, Collision *col) const
   1.119 +{
   1.120 +	if(ignorelist.find(p2) != ignorelist.end()) {
   1.121 +		return false;
   1.122 +	}
   1.123 +
   1.124 +	Vector3 v = p2->pos - pos;
   1.125 +	float dist_sq = dot_product(v, v);
   1.126 +	float radsum = rad + p2->rad;
   1.127 +
   1.128 +	if(dist_sq < radsum * radsum) {
   1.129 +		float rad_ratio = rad / p2->rad;
   1.130 +		Vector3 dir = p2->pos - pos;
   1.131 +		col->pos = pos + dir * rad_ratio;
   1.132 +		col->normal = -dir.normalized();
   1.133 +		col->elast = elast * p2->elast;
   1.134 +		return true;
   1.135 +	}
   1.136 +	return false;
   1.137 +}
   1.138 +
   1.139 +void Particle::draw() const
   1.140 +{
   1.141 +	float color[] = {0.3, 0.9, 0.2, 1};
   1.142 +
   1.143 +	glPushMatrix();
   1.144 +	glTranslatef(pos.x, pos.y, pos.z);
   1.145 +
   1.146 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
   1.147 +
   1.148 +	glutSolidSphere(rad, 16, 8);
   1.149 +
   1.150 +	glPushAttrib(GL_ENABLE_BIT);
   1.151 +	glDisable(GL_DEPTH_TEST);
   1.152 +	glDisable(GL_LIGHTING);
   1.153 +	glEnable(GL_BLEND);
   1.154 +	glBlendFunc(GL_ONE, GL_ONE);
   1.155 +	glLineWidth(2.0);
   1.156 +
   1.157 +	glBegin(GL_LINES);
   1.158 +	glColor3f(0.3, 0, 0);
   1.159 +	glVertex3f(0, 0, 0);
   1.160 +	glVertex3f(velocity.x, velocity.y, velocity.z);
   1.161 +
   1.162 +	glColor3f(0, 0, 0.8);
   1.163 +	glVertex3f(0, 0, 0);
   1.164 +	glVertex3f(forces.x, forces.y, forces.z);
   1.165 +	glEnd();
   1.166 +
   1.167 +	glPopAttrib();
   1.168 +
   1.169 +	glPopMatrix();
   1.170 +}