summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2010-08-19 13:24:37 +0000
committerIan C <ianc@noddybox.co.uk>2010-08-19 13:24:37 +0000
commitaf34bad8b39e95276b0d4e08ccdc26f89df2d2ec (patch)
tree427e06fe813cd847f96e12ff612d245b2da970c5
parent19bbd5822593c7198dd54eb0f5d38fa1ed8d8fb7 (diff)
Updates to maths and added the use of a 3D vector.
-rw-r--r--dat2
-rw-r--r--makefile8
-rw-r--r--mass.cpp79
-rw-r--r--mass.h3
-rw-r--r--test1.dat10
-rw-r--r--test2.dat17
-rw-r--r--vec3d.cpp166
-rw-r--r--vec3d.h71
8 files changed, 281 insertions, 75 deletions
diff --git a/dat b/dat
index 5c63d53..47b7d65 100644
--- a/dat
+++ b/dat
@@ -79,7 +79,7 @@ max_mass 1000;
# Define gravitational constant
#
-const 0.00001;
+const 1;
# By default the radius of the calcualted for each mass as if the sphere has a
diff --git a/makefile b/makefile
index 729eabe..8aa51f0 100644
--- a/makefile
+++ b/makefile
@@ -27,13 +27,15 @@ SOURCES = glgrav.cpp \
mass.cpp \
config.cpp \
particles.cpp \
- sparks.cpp
+ sparks.cpp \
+ vec3d.cpp
HEADERS = global.h \
config.h \
mass.h \
particles.h \
- sparks.h
+ sparks.h \
+ vec3d.h
OBJECTS = $(SOURCES:.cpp=.o)
@@ -73,7 +75,7 @@ $(TARGET): $(OBJECTS)
-include depend.mak
clean:
- -rm $(TARGET) $(TARGET).exe core *.stackdump *.o depend.mak
+ rm -f $(TARGET) $(TARGET).exe core *.stackdump *.o depend.mak
depend: $(DEPEND)
@echo Dependencies updated....
diff --git a/mass.cpp b/mass.cpp
index 20f932c..95a76f9 100644
--- a/mass.cpp
+++ b/mass.cpp
@@ -41,10 +41,6 @@ Mass::Mass()
m_y=0.0;
m_z=0.0;
- m_dx=0.0;
- m_dy=0.0;
- m_dz=0.0;
-
m_r=1.0;
m_g=1.0;
m_b=1.0;
@@ -69,9 +65,10 @@ Mass::Mass()
Mass::Mass(const string& name, double mass, double scale,
- double posx, double posy, double posz,
- double dx, double dy, double dz,
- GLfloat r, GLfloat g, GLfloat b, double rot, int shield)
+ double posx, double posy, double posz,
+ double dx, double dy, double dz,
+ GLfloat r, GLfloat g, GLfloat b, double rot, int shield)
+ : m_delta(dx, dy, dz)
{
m_name=name;
@@ -83,10 +80,6 @@ Mass::Mass(const string& name, double mass, double scale,
m_y=posy;
m_z=posz;
- m_dx=dx;
- m_dy=dy;
- m_dz=dz;
-
m_r=r;
m_g=g;
m_b=b;
@@ -128,8 +121,8 @@ Mass::~Mass()
// ---------------------------------------- PUBLIC MEMBERS
//
void Mass::setRing(double from, double to,
- GLfloat r, GLfloat g, GLfloat b, GLfloat alpha,
- double rot, double ang)
+ GLfloat r, GLfloat g, GLfloat b, GLfloat alpha,
+ double rot, double ang)
{
m_ring=true;
m_ring_from=from;
@@ -217,9 +210,9 @@ void Mass::getPreviousPosition(double& x, double& y, double& z) const
void Mass::getDelta(double& dx, double& dy, double& dz) const
{
- dx=m_dx;
- dy=m_dy;
- dz=m_dz;
+ dx = m_delta.dx();
+ dy = m_delta.dy();
+ dz = m_delta.dz();
}
@@ -235,9 +228,8 @@ void Mass::reset(double mass, double dx, double dy, double dz)
{
m_mass=mass;
calcSize();
- m_dx=dx;
- m_dy=dy;
- m_dz=dz;
+
+ m_delta.Set(dx, dy, dz);
if (m_mass<=0.0)
{
@@ -296,39 +288,13 @@ bool Mass::calcAttraction(Mass& m, double gr_const)
collide=false;
}
- // ga=((gr_const*m.m_mass)*(gr_const*m_mass))/(dist*gr_const);
+ ga = gr_const * (m.m_mass / (dist * dist));
- ga=gr_const * (m_mass / dist);
-
- if (m_x<m.m_x)
- {
- m_dx+=ga/(m_mass/dx);
- }
+ Vec3D force(dx, dy, dz);
- if (m_x>m.m_x)
- {
- m_dx-=ga/(m_mass/dx);
- }
+ force.SetLength(ga);
- if (m_y<m.m_y)
- {
- m_dy+=ga/(m_mass/dy);
- }
-
- if (m_y>m.m_y)
- {
- m_dy-=ga/(m_mass/dy);
- }
-
- if (m_z<m.m_z)
- {
- m_dz+=ga/(m_mass/dz);
- }
-
- if (m_z>m.m_z)
- {
- m_dz-=ga/(m_mass/dz);
- }
+ m_delta.Add(force);
return collide && !m_massless;
}
@@ -340,9 +306,9 @@ void Mass::move()
m_prevy=m_y;
m_prevz=m_z;
- m_x+=m_dx;
- m_y+=m_dy;
- m_z+=m_dz;
+ m_x+=m_delta.dx();
+ m_y+=m_delta.dy();
+ m_z+=m_delta.dz();
m_ang+=m_rot;
m_ring_ang+=m_ring_rot;
@@ -462,9 +428,9 @@ void Mass::draw(bool solid, bool texture, Mass::DrawItem item) const
//
double Mass::calcDistance2(Mass& m, double& dx, double& dy, double& dz)
{
- dx=abs(m_x-m.m_x);
- dy=abs(m_y-m.m_y);
- dz=abs(m_z-m.m_z);
+ dx = m.m_x-m_x;
+ dy = m.m_y-m_y;
+ dz = m.m_z-m_z;
return sqrt((dx*dx)+(dy*dy)+(dz*dz));
}
@@ -489,7 +455,8 @@ ostream& operator<<(ostream& os, const Mass& m)
os << "Name " << m.m_name << " (mass " << m.m_mass
<< ", size " << m.m_size << ", shield " << m.m_shield << ")" << endl
<< " P (" << m.m_x << ", " << m.m_y << ", " << m.m_z << ")" << endl
- << " D (" << m.m_dx << ", " << m.m_dy << ", " << m.m_dz << ")" << endl
+ << " D (" << m.m_delta.dx() << ", " << m.m_delta.dy()
+ << ", " << m.m_delta.dz() << ")" << endl
<< " R (" << m.m_rot << ", " << m.m_ang << ")" << endl
<< " T (" << m.m_textured << ", " << m.m_texture_id << ")" << endl
<< " Ring (" << m.m_ring << ", " << m.m_ring_from
diff --git a/mass.h b/mass.h
index 9c58660..778a0b4 100644
--- a/mass.h
+++ b/mass.h
@@ -28,6 +28,7 @@
#define MASS_H
#include "global.h"
+#include "vec3d.h"
class Mass
{
@@ -100,7 +101,7 @@ private:
double m_scale;
double m_x,m_y,m_z;
double m_prevx,m_prevy,m_prevz;
- double m_dx,m_dy,m_dz;
+ Vec3D m_delta;
GLfloat m_r,m_g,m_b;
bool m_massless;
diff --git a/test1.dat b/test1.dat
index d98a851..0797326 100644
--- a/test1.dat
+++ b/test1.dat
@@ -9,13 +9,13 @@ disable texture;
collide none;
-const 0.001;
+const 1;
scale 10;
body centre,
- 100,
+ 1000,
0,0,0,
0,0,0,
1,1,1,
@@ -27,7 +27,7 @@ light centre,1,1,1;
plane xy;
radial mass1,
- 1,
+ 10,
centre,
500,
90,
@@ -36,7 +36,7 @@ radial mass1,
0;
radial mass2,
- 2,
+ 20,
centre,
500,
270,
@@ -49,3 +49,5 @@ look mass1;
look mass2;
look_at centre;
+
+particles 0.0001,0.0,1.0;
diff --git a/test2.dat b/test2.dat
index 48b7c27..9771de3 100644
--- a/test2.dat
+++ b/test2.dat
@@ -1,6 +1,3 @@
-# For this test the two objects should orbit around the centre with the same
-# acceleration, independent of their mass.
-#
camera 0.0, 0.0, -1000.0, 0.0;
enable solid;
@@ -9,13 +6,13 @@ disable texture;
collide none;
-const 0.001;
+const 0.1;
-scale 10;
+scale 5;
body centre,
- 100,
+ 1000,
0,0,0,
0,0,0,
1,1,1,
@@ -31,16 +28,16 @@ radial mass1,
centre,
300,
90,
- 1,
+ 0.50,
1,0,0,
0;
radial mass2,
- 2,
+ 20,
centre,
300,
270,
- 1,
+ 0.50,
0,1,0,
0;
@@ -50,4 +47,4 @@ look mass2;
look_at centre;
-particles 0.0001,0.0,1.0;
+particles 0.001,0.0,1.0;
diff --git a/vec3d.cpp b/vec3d.cpp
new file mode 100644
index 0000000..5e073d8
--- /dev/null
+++ b/vec3d.cpp
@@ -0,0 +1,166 @@
+//
+// glgrav - OpenGL N-Body gravity simulator
+//
+// Copyright (C) 2003 Ian Cowburn (ianc@noddybox.demon.co.uk)
+//
+// This program 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.
+//
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// -------------------------------------------------------------------------
+//
+// 3D Vector
+//
+//
+static const char id[]="$Id: sparks.cpp 8 2010-08-18 14:38:34Z ianc $";
+
+#include "vec3d.h"
+
+#include <math.h>
+
+Vec3D::Vec3D()
+{
+ m_dx = 0.0;
+ m_dy = 0.0;
+ m_dz = 0.0;
+}
+
+Vec3D::Vec3D(double dx, double dy, double dz)
+{
+ m_dx = dx;
+ m_dy = dy;
+ m_dz = dz;
+}
+
+Vec3D::~Vec3D()
+{
+}
+
+void Vec3D::Set(double dx, double dy ,double dz)
+{
+ m_dx = dx;
+ m_dy = dy;
+ m_dz = dz;
+}
+
+double Vec3D::dx() const
+{
+ return m_dx;
+}
+
+double Vec3D::dy() const
+{
+ return m_dy;
+}
+
+double Vec3D::dz() const
+{
+ return m_dz;
+}
+
+
+bool Vec3D::IsNull() const
+{
+ return m_dx == 0.0 && m_dy == 0.0 && m_dz == 0.0;
+}
+
+
+double Vec3D::Length() const
+{
+ if (IsNull())
+ {
+ return 0.0;
+ }
+ else
+ {
+ return sqrt(m_dx * m_dx + m_dy * m_dy + m_dz * m_dz);
+ }
+}
+
+
+void Vec3D::Normalise()
+{
+ double l;
+
+ l = Length();
+
+ m_dx /= l;
+ m_dy /= l;
+ m_dz /= l;
+}
+
+void Vec3D::SetLength(double d)
+{
+ Normalise();
+ Scale(d);
+}
+
+
+void Vec3D::AddScalar(double d)
+{
+ m_dx += d;
+ m_dy += d;
+ m_dz += d;
+}
+
+void Vec3D::Minus()
+{
+ m_dx = -m_dx;
+ m_dy = -m_dy;
+ m_dz = -m_dz;
+}
+
+void Vec3D::Scale(double d)
+{
+ m_dx *= d;
+ m_dy *= d;
+ m_dz *= d;
+}
+
+
+void Vec3D::Add(const Vec3D& v)
+{
+ m_dx += v.m_dx;
+ m_dy += v.m_dy;
+ m_dz += v.m_dz;
+}
+
+void Vec3D::Subtract(const Vec3D& v)
+{
+ m_dx -= v.m_dx;
+ m_dy -= v.m_dy;
+ m_dz -= v.m_dz;
+}
+
+void Vec3D::Scale(const Vec3D& v)
+{
+ m_dx *= v.m_dx;
+ m_dy *= v.m_dy;
+ m_dz *= v.m_dz;
+}
+
+double Vec3D::DotProduct(const Vec3D& v)
+{
+ return m_dx*v.m_dx + m_dy*v.m_dy + m_dz*v.m_dz;
+}
+
+Vec3D Vec3D::CrossProduct(const Vec3D& v)
+{
+ return Vec3D(m_dy * v.m_dz - v.m_dy * m_dz,
+ m_dz * v.m_dx - v.m_dz * m_dx,
+ m_dx * v.m_dy - v.m_dx * m_dy);
+}
+
+
+
+// END OF FILE //
diff --git a/vec3d.h b/vec3d.h
new file mode 100644
index 0000000..5d424e8
--- /dev/null
+++ b/vec3d.h
@@ -0,0 +1,71 @@
+//
+// glgrav - OpenGL N-Body gravity simulator
+//
+// Copyright (C) 2003 Ian Cowburn (ianc@noddybox.demon.co.uk)
+//
+// This program 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.
+//
+// This program 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 this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// -------------------------------------------------------------------------
+//
+// 3D Vector
+//
+// $Id$
+//
+#ifndef VEC3D_H
+
+#define VEC3D_H
+
+#include "global.h"
+
+class Vec3D
+{
+public:
+ Vec3D();
+ Vec3D(double dx, double dy, double dz);
+ virtual ~Vec3D();
+
+ void Set(double dx, double dy ,double dz);
+
+ double dx() const;
+ double dy() const;
+ double dz() const;
+
+ bool IsNull() const;
+
+ double Length() const;
+
+ void Normalise();
+ void SetLength(double d);
+
+ void AddScalar(double d);
+ void Minus();
+ void Scale(double d);
+
+ void Add(const Vec3D& v);
+ void Subtract(const Vec3D& v);
+ void Scale(const Vec3D& v);
+
+ double DotProduct(const Vec3D& v);
+ Vec3D CrossProduct(const Vec3D& v);
+
+private:
+ double m_dx;
+ double m_dy;
+ double m_dz;
+};
+
+#endif
+
+// END OF FILE //