grav

view src/sim.cc @ 0:68db0e456733

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 23 May 2014 03:13:47 +0300
parents
children 3d541da6e48c
line source
1 #include <GL/gl.h>
2 #include "sim.h"
4 Particle::Particle()
5 {
6 mass = 1.0;
7 }
9 void Particle::impulse(const Vector3 &imp)
10 {
11 accel += imp;
12 }
14 void Particle::step(float dt)
15 {
16 Vector3 npos = pos + vel * dt;
18 vel += accel * dt;
19 accel.x = accel.y = accel.z = 0.0;
21 pos = npos;
22 }
25 // ---- SimWorld ----
26 SimWorld::SimWorld()
27 {
28 set_world_bounds(1000.0f);
29 }
31 void SimWorld::set_world_bounds(float rad)
32 {
33 radius_sq = rad * rad;
34 }
36 void SimWorld::add_particle(const Particle &p)
37 {
38 particles.push_back(p);
39 }
41 void SimWorld::step(float dt)
42 {
43 std::list<Particle>::iterator it = particles.begin();
44 while(it != particles.end()) {
45 Particle *p = &*it;
46 p->step(dt);
48 if(p->pos.length_sq() > radius_sq) {
49 it = particles.erase(it);
50 } else {
51 ++it;
52 }
53 }
54 }
56 void SimWorld::draw() const
57 {
58 glBlendFunc(GL_ONE, GL_ONE);
59 glEnable(GL_BLEND);
61 glBegin(GL_POINTS);
62 glColor3f(0.5, 0.6, 0.7);
64 std::list<Particle>::const_iterator it = particles.begin();
65 while(it != particles.end()) {
66 const Particle *p = &*it++;
67 glVertex3f(p->pos.x, p->pos.y, p->pos.z);
68 }
70 glEnd();
72 glDisable(GL_BLEND);
73 }