summaryrefslogtreecommitdiff
path: root/mass.cpp
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2010-08-18 14:38:34 +0000
committerIan C <ianc@noddybox.co.uk>2010-08-18 14:38:34 +0000
commit19bbd5822593c7198dd54eb0f5d38fa1ed8d8fb7 (patch)
tree97f7f8849c3a8282ba571a64ff2a655cdf5d41aa /mass.cpp
parent0d2f672482fee91a9642a7cb6eb01fcdded79159 (diff)
Reformatted code and altered the attraction rule to something a bit more
correct. Also added two tests to prove it works.
Diffstat (limited to 'mass.cpp')
-rw-r--r--mass.cpp142
1 files changed, 97 insertions, 45 deletions
diff --git a/mass.cpp b/mass.cpp
index e83cf55..20f932c 100644
--- a/mass.cpp
+++ b/mass.cpp
@@ -69,9 +69,9 @@ 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_name=name;
@@ -97,12 +97,14 @@ Mass::Mass(const string& name, double mass, double scale,
m_shield=shield;
if (m_mass<=0.0)
- {
- m_mass=0.0001;
+ {
+ m_mass=0.0001;
m_massless=true;
- }
+ }
else
- m_massless=false;
+ {
+ m_massless=false;
+ }
m_ring=false;
m_ring_textured=false;
@@ -126,8 +128,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;
@@ -159,10 +161,10 @@ bool Mass::getTexture(GLuint& id) const
void Mass::setRingTexture(GLuint id)
{
if (m_ring)
- {
+ {
m_ring_textured=true;
m_ring_id=id;
- }
+ }
}
@@ -238,12 +240,14 @@ void Mass::reset(double mass, double dx, double dy, double dz)
m_dz=dz;
if (m_mass<=0.0)
- {
- m_mass=0.0001;
+ {
+ m_mass=0.0001;
m_massless=true;
- }
+ }
else
- m_massless=false;
+ {
+ m_massless=false;
+ }
}
@@ -253,12 +257,14 @@ void Mass::reset(double mass)
calcSize();
if (m_mass<=0.0)
- {
- m_mass=0.0001;
+ {
+ m_mass=0.0001;
m_massless=true;
- }
+ }
else
- m_massless=false;
+ {
+ m_massless=false;
+ }
}
@@ -270,37 +276,59 @@ bool Mass::calcAttraction(Mass& m, double gr_const)
double dx,dy,dz;
if (m.m_massless)
- return false;
+ {
+ return false;
+ }
dist=calcDistance2(m,dx,dy,dz);
if (dist==0.0)
- return !m_massless;
+ {
+ return !m_massless;
+ }
if (dist<(m_size + m.m_size) && (m_shield==0 || m.m_shield==0))
- collide=true;
+ {
+ collide=true;
+ }
else
- collide=false;
+ {
+ collide=false;
+ }
+
+ // ga=((gr_const*m.m_mass)*(gr_const*m_mass))/(dist*gr_const);
- ga=((gr_const*m.m_mass)*(gr_const*m_mass))/(dist*gr_const);
+ ga=gr_const * (m_mass / dist);
if (m_x<m.m_x)
+ {
m_dx+=ga/(m_mass/dx);
+ }
if (m_x>m.m_x)
+ {
m_dx-=ga/(m_mass/dx);
+ }
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);
+ }
return collide && !m_massless;
}
@@ -320,7 +348,9 @@ void Mass::move()
m_ring_ang+=m_ring_rot;
if (m_shield)
- m_shield--;
+ {
+ m_shield--;
+ }
}
@@ -330,7 +360,7 @@ void Mass::draw(bool solid, bool texture, Mass::DrawItem item) const
GLUquadric *q;
if (m_massless && item==eMass)
- {
+ {
GLboolean l=glIsEnabled(GL_LIGHTING);
glDisable(GL_LIGHTING);
@@ -340,65 +370,79 @@ void Mass::draw(bool solid, bool texture, Mass::DrawItem item) const
glEnd();
if (l)
+ {
glEnable(GL_LIGHTING);
}
+ }
else
- {
+ {
// Render the object
//
if (item==eMass && (q=gluNewQuadric()))
- {
+ {
glColor4f(m_r,m_g,m_b,1.0f);
glTranslated(m_x,m_y,m_z);
if (!solid)
+ {
gluQuadricDrawStyle(q,GLU_LINE);
+ }
gluQuadricNormals(q,GLU_SMOOTH);
if (m_textured && texture && solid)
- {
+ {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,m_texture_id);
gluQuadricTexture(q,1);
- }
+ }
else
+ {
glDisable(GL_TEXTURE_2D);
+ }
if (m_ang!=0.0)
+ {
glRotated(m_ang,0.0,1.0,0.0);
+ }
glRotated(90.0,1.0,0.0,0.0);
gluSphere(q,m_size,sp,sp);
gluDeleteQuadric(q);
- }
+ }
// Render the ring (if present)
//
if (m_ring && item==eRing && (q=gluNewQuadric()))
- {
+ {
glColor4f(m_rr,m_rg,m_rb,m_ring_alpha);
glTranslated(m_x,m_y,m_z);
if (!solid)
+ {
gluQuadricDrawStyle(q,GLU_LINE);
+ }
gluQuadricNormals(q,GLU_SMOOTH);
if (m_ring_textured && texture && solid)
- {
+ {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,m_ring_id);
gluQuadricTexture(q,1);
- }
+ }
else
+ {
glDisable(GL_TEXTURE_2D);
+ }
glRotated(m_ring_tilt,1.0,0.0,0.0);
if (m_ring_ang!=0.0)
+ {
glRotated(-m_ring_ang,0.0,0.0,1.0);
+ }
gluDisk(q,m_size*m_ring_from,m_size*m_ring_to,sp,1);
@@ -409,8 +453,8 @@ void Mass::draw(bool solid, bool texture, Mass::DrawItem item) const
gluDisk(q,m_size*m_ring_from,m_size*m_ring_to,sp,1);
gluDeleteQuadric(q);
- }
}
+ }
}
@@ -432,7 +476,9 @@ void Mass::calcSize()
m_size=pow(m_size,0.333333333333333333333)*m_scale;
if (m_size<1.0)
- m_size=1.0;
+ {
+ m_size=1.0;
+ }
}
@@ -441,13 +487,13 @@ void Mass::calcSize()
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
- << " 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
- << ", " << m.m_ring_to << ")" << endl;
+ << ", 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
+ << " 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
+ << ", " << m.m_ring_to << ")" << endl;
return os;
}
@@ -458,11 +504,13 @@ bool findMass(vector<Mass>& m, const string& name, Mass& ret)
vector<Mass>::const_iterator i;
for(i=m.begin();i!=m.end();i++)
+ {
if (i->getName()==name)
- {
+ {
ret=*i;
return true;
- }
+ }
+ }
return false;
}
@@ -470,8 +518,12 @@ bool findMass(vector<Mass>& m, const string& name, Mass& ret)
bool findMass(vector<Mass>& m, const string& name, vector<Mass>::size_type& i)
{
for(i=0;i<m.size();i++)
+ {
if (m[i].getName()==name)
+ {
return true;
+ }
+ }
return false;
}