# HG changeset patch # User John Tsiombikas # Date 1400804027 -10800 # Node ID 68db0e456733ef8514bb73a0d73f0fb9a42bed1e initial commit diff -r 000000000000 -r 68db0e456733 Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri May 23 03:13:47 2014 +0300 @@ -0,0 +1,13 @@ +src = $(wildcard src/*.cc) +obj = $(src:.cc=.o) +bin = grav + +CXXFLAGS = -pedantic -Wall -g +LDFLAGS = -lGL -lGLU -lglut -lm -lvmath + +$(bin): $(obj) + $(CXX) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r 000000000000 -r 68db0e456733 src/main.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main.cc Fri May 23 03:13:47 2014 +0300 @@ -0,0 +1,39 @@ +#include +#include +#include +#include "sim.h" + +static bool init(); +static void cleanup(); +static void update(long tmsec); +static void display(); +static void idle(); +static void reshape(int x, int y); +static void keyb(unsigned char key, int x, int y); + +static SimWorld sim; + +int main(int argc, char **argv) +{ + glutInitWindowSize(1024, 768); + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); + glutCreateWindow("gravity"); + + glutDisplayFunc(display); + glutIdleFunc(idle); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); +} + + +static bool init() +{ +} + +static void cleanup(); +static void update(long tmsec); +static void display(); +static void idle(); +static void reshape(int x, int y); +static void keyb(unsigned char key, int x, int y); diff -r 000000000000 -r 68db0e456733 src/sim.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sim.cc Fri May 23 03:13:47 2014 +0300 @@ -0,0 +1,73 @@ +#include +#include "sim.h" + +Particle::Particle() +{ + mass = 1.0; +} + +void Particle::impulse(const Vector3 &imp) +{ + accel += imp; +} + +void Particle::step(float dt) +{ + Vector3 npos = pos + vel * dt; + + vel += accel * dt; + accel.x = accel.y = accel.z = 0.0; + + pos = npos; +} + + +// ---- SimWorld ---- +SimWorld::SimWorld() +{ + set_world_bounds(1000.0f); +} + +void SimWorld::set_world_bounds(float rad) +{ + radius_sq = rad * rad; +} + +void SimWorld::add_particle(const Particle &p) +{ + particles.push_back(p); +} + +void SimWorld::step(float dt) +{ + std::list::iterator it = particles.begin(); + while(it != particles.end()) { + Particle *p = &*it; + p->step(dt); + + if(p->pos.length_sq() > radius_sq) { + it = particles.erase(it); + } else { + ++it; + } + } +} + +void SimWorld::draw() const +{ + glBlendFunc(GL_ONE, GL_ONE); + glEnable(GL_BLEND); + + glBegin(GL_POINTS); + glColor3f(0.5, 0.6, 0.7); + + std::list::const_iterator it = particles.begin(); + while(it != particles.end()) { + const Particle *p = &*it++; + glVertex3f(p->pos.x, p->pos.y, p->pos.z); + } + + glEnd(); + + glDisable(GL_BLEND); +} diff -r 000000000000 -r 68db0e456733 src/sim.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sim.h Fri May 23 03:13:47 2014 +0300 @@ -0,0 +1,34 @@ +#ifndef SIM_H_ +#define SIM_H_ + +#include +#include + +class Particle { +public: + float mass; + Vector3 pos, vel; + Vector3 accel; + + Particle(); + + void impulse(const Vector3 &imp); + void step(float dt); +}; + +class SimWorld { +private: + std::list particles; + float radius_sq; + +public: + SimWorld(); + + void set_world_bounds(float rad); + + void add_particle(const Particle &p); + void step(float dt); + void draw() const; +}; + +#endif // SIM_H_