CGELMassParticle.h
Go to the documentation of this file.
1 //===========================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, CHAI3D.
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Francois Conti
40  \version 3.2.0 $Rev: 1869 $
41 */
42 //===========================================================================
43 
44 //---------------------------------------------------------------------------
45 #ifndef CGELMassParticleH
46 #define CGELMassParticleH
47 //---------------------------------------------------------------------------
48 #include "chai3d.h"
49 //---------------------------------------------------------------------------
50 
51 //===========================================================================
58 //===========================================================================
59 
60 //===========================================================================
68 //===========================================================================
70 {
71  //-----------------------------------------------------------------------
72  // CONSTRUCTOR & DESTRUCTOR:
73  //-----------------------------------------------------------------------
74 
75 public:
76 
79 
81  virtual ~cGELMassParticle();
82 
83 
84  //-----------------------------------------------------------------------
85  // PUBLIC METHODS:
86  //-----------------------------------------------------------------------
87 
88 public:
89 
91  void setMass(double a_mass);
92 
94  inline void addForce(chai3d::cVector3d &a_force)
95  {
96  m_force.add(a_force);
97  }
98 
100  inline void setExternalForce(chai3d::cVector3d &a_force)
101  {
102  m_externalForce = a_force;
103  }
104 
106  inline void computeNextPose(double a_timeInterval)
107  {
108  if (!m_fixed)
109  {
110  // Euler double integration for position
111  chai3d::cVector3d damping;
112  m_vel.mulr(-m_kDampingPos * m_mass, damping);
113  m_force.add(damping);
114  m_acc = cDiv(m_mass, cAdd(m_force, m_externalForce));
115  m_vel = cAdd(m_vel, cMul(a_timeInterval, m_acc));
116  m_nextPos = cAdd(m_pos, cMul(a_timeInterval, m_vel));
117  }
118  else
119  {
120  m_nextPos = m_pos;
121  }
122  }
123 
125  inline void applyNextPose()
126  {
127  m_pos = m_nextPos;
128  }
129 
131  inline void clearForces()
132  {
133  if (m_useGravity)
134  {
136  }
137  else
138  {
139  m_force.zero();
140  }
141  }
142 
144  inline void clearExternalForces()
145  {
146  m_externalForce.zero();
147  }
148 
150  inline void render()
151  {
152  // draw point
153  m_color.render();
154  glBegin(GL_POINTS);
155  glVertex3dv( (const double *)&m_pos);
156  glEnd();
157 
158  // render external forces
159  glColor4fv( (const float *)&m_color);
161  glBegin(GL_LINES);
162  glVertex3dv( (const double *)&m_pos);
163  glVertex3dv( (const double *)&v);
164  glEnd();
165  }
166 
167 
168  //-----------------------------------------------------------------------
169  // PUBLIC MEMBERS - PHYSICAL PROPERTIES:
170  //-----------------------------------------------------------------------
171 
172 public:
173 
175  double m_mass;
176 
179 
182 
185 
188 
190  bool m_fixed;
191 
194 
197 
198 
199  //-----------------------------------------------------------------------
200  // PUBLIC MEMBERS - GRAPHICAL PROPERTIES:
201  //-----------------------------------------------------------------------
202 
203 public:
204 
207 
208 
209  //-----------------------------------------------------------------------
210  // PUBLIC MEMBERS - POSITION:
211  //-----------------------------------------------------------------------
212 
213 public:
214 
217 
220 
221 
222  //-----------------------------------------------------------------------
223  // PRIVATE MEMBERS - EXTERNAL FORCES:
224  //-----------------------------------------------------------------------
225 
226 private:
227 
229  chai3d::cVector3d m_externalForce;
230 
231 
232  //-----------------------------------------------------------------------
233  // PUBLIC MEMBERS:
234  //-----------------------------------------------------------------------
235 
236 public:
237 
239  static double s_default_kDampingPos;
240 
242  static double s_default_mass;
243 
246 
248  static bool s_default_useGravity;
249 
252 
254  static bool s_show_forces;
255 
258 };
259 
260 //---------------------------------------------------------------------------
261 #endif
262 //---------------------------------------------------------------------------
double m_kDampingPos
Linear damping.
Definition: CGELMassParticle.h:187
chai3d::cVector3d m_vel
Instant velocity.
Definition: CGELMassParticle.h:184
chai3d::cVector3d m_gravity
Gravity field.
Definition: CGELMassParticle.h:196
void addForce(chai3d::cVector3d &a_force)
This method adds a force to this mass particle.
Definition: CGELMassParticle.h:94
cGELMassParticle()
Constructor of cGELMassParticle.
Definition: CGELMassParticle.cpp:75
cVector3d cAdd(const cVector3d &a_vector1, const cVector3d &a_vector2)
static double s_default_kDampingPos
Default property - linear damping.
Definition: CGELMassParticle.h:239
chai3d::cColorf m_color
Color used to display nodes.
Definition: CGELMassParticle.h:206
static chai3d::cVector3d s_default_gravity
Default property - gravity field.
Definition: CGELMassParticle.h:251
static bool s_scale_force_vector_display
Default property - force vector display magnitude.
Definition: CGELMassParticle.h:257
chai3d::cVector3d m_pos
Position of particle.
Definition: CGELMassParticle.h:216
virtual ~cGELMassParticle()
Destructor of cGELMassParticle.
Definition: CGELMassParticle.cpp:96
void mulr(const double &a_scalar, cVector3d &a_result) const
void applyNextPose()
This method updates the position with its latest computed values.
Definition: CGELMassParticle.h:125
cVector3d cDiv(const double &a_value, const cVector3d &a_vector)
bool m_useGravity
If true, then gravity field is taken into account.
Definition: CGELMassParticle.h:193
void clearForces()
This method clears all forces.
Definition: CGELMassParticle.h:131
cVector3d cMul(const double &a_value, const cVector3d &a_vector)
bool m_fixed
If true, then mass is fixed in space and cannot move.
Definition: CGELMassParticle.h:190
void clearExternalForces()
This method clears all external forces.
Definition: CGELMassParticle.h:144
static double s_default_mass
Default property - mass.
Definition: CGELMassParticle.h:242
chai3d::cVector3d m_force
Current force being applied.
Definition: CGELMassParticle.h:178
static bool s_show_forces
Default property - force display status.
Definition: CGELMassParticle.h:254
double m_mass
Mass property.
Definition: CGELMassParticle.h:175
chai3d::cVector3d m_nextPos
Next computed position of particle.
Definition: CGELMassParticle.h:219
void render()
This method renders this node in OpenGL.
Definition: CGELMassParticle.h:150
static chai3d::cColorf s_default_color
Default property - color.
Definition: CGELMassParticle.h:245
This class implements a simple mass particle point.
Definition: CGELMassParticle.h:69
void setExternalForce(chai3d::cVector3d &a_force)
This method assigns an external force to this mass particle.
Definition: CGELMassParticle.h:100
void setMass(double a_mass)
This method assigns the mass of this particle.
Definition: CGELMassParticle.cpp:108
void add(const cVector3d &a_vector)
void render() const
static bool s_default_useGravity
Default property - use of gravity field.
Definition: CGELMassParticle.h:248
chai3d::cVector3d m_acc
Instant acceleration.
Definition: CGELMassParticle.h:181
void computeNextPose(double a_timeInterval)
This method updates the simulation over a specified time interval.
Definition: CGELMassParticle.h:106