My Project
Loading...
Searching...
No Matches
CGELMassParticle.h
Go to the documentation of this file.
1//===========================================================================
2/*
3 Software License Agreement (BSD License)
4 Copyright (c) 2003-2024, 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 1.0.0
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
75public:
76
79
81 virtual ~cGELMassParticle();
82
83
84 //-----------------------------------------------------------------------
85 // PUBLIC METHODS:
86 //-----------------------------------------------------------------------
87
88public:
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
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 {
121 }
122 }
123
125 inline void applyNextPose()
126 {
128 }
129
131 inline void clearForces()
132 {
133 if (m_useGravity)
134 {
136 }
137 else
138 {
139 m_force.zero();
140 }
141 }
142
145 {
146 m_externalForce.zero();
147 }
148
150 inline void render()
151 {
152#ifdef C_USE_OPENGL
153
154 // draw point
155 m_color.render();
156 glBegin(GL_POINTS);
157 glVertex3dv( (const double *)&m_pos);
158 glEnd();
159
160 // render external forces
161 glColor4fv( (const float *)&m_color);
162 chai3d::cVector3d v = cAdd(m_pos, cMul(s_scale_force_vector_display, m_externalForce));
163 glBegin(GL_LINES);
164 glVertex3dv( (const double *)&m_pos);
165 glVertex3dv( (const double *)&v);
166 glEnd();
167
168#endif
169 }
170
171
172 //-----------------------------------------------------------------------
173 // PUBLIC MEMBERS - PHYSICAL PROPERTIES:
174 //-----------------------------------------------------------------------
175
176public:
177
179 double m_mass;
180
183
186
189
192
195
198
201
202
203 //-----------------------------------------------------------------------
204 // PUBLIC MEMBERS - GRAPHICAL PROPERTIES:
205 //-----------------------------------------------------------------------
206
207public:
208
211
212
213 //-----------------------------------------------------------------------
214 // PUBLIC MEMBERS - POSITION:
215 //-----------------------------------------------------------------------
216
217public:
218
221
224
225
226 //-----------------------------------------------------------------------
227 // PRIVATE MEMBERS - EXTERNAL FORCES:
228 //-----------------------------------------------------------------------
229
230private:
231
233 chai3d::cVector3d m_externalForce;
234
235
236 //-----------------------------------------------------------------------
237 // PUBLIC MEMBERS:
238 //-----------------------------------------------------------------------
239
240public:
241
244
246 static double s_default_mass;
247
250
253
256
258 static bool s_show_forces;
259
262};
263
264//---------------------------------------------------------------------------
265#endif
266//---------------------------------------------------------------------------
This class implements a simple mass particle point.
Definition CGELMassParticle.h:70
chai3d::cVector3d m_vel
Instant velocity.
Definition CGELMassParticle.h:188
void clearForces()
This method clears all forces.
Definition CGELMassParticle.h:131
bool m_fixed
If true, then mass is fixed in space and cannot move.
Definition CGELMassParticle.h:194
chai3d::cVector3d m_force
Current force being applied.
Definition CGELMassParticle.h:182
chai3d::cVector3d m_nextPos
Next computed position of particle.
Definition CGELMassParticle.h:223
virtual ~cGELMassParticle()
Destructor of cGELMassParticle.
Definition CGELMassParticle.cpp:96
void clearExternalForces()
This method clears all external forces.
Definition CGELMassParticle.h:144
static bool s_show_forces
Default property - force display status.
Definition CGELMassParticle.h:258
static double s_default_mass
Default property - mass.
Definition CGELMassParticle.h:246
chai3d::cColorf m_color
Color used to display nodes.
Definition CGELMassParticle.h:210
void render()
This method renders this node in OpenGL.
Definition CGELMassParticle.h:150
void addForce(chai3d::cVector3d &a_force)
This method adds a force to this mass particle.
Definition CGELMassParticle.h:94
void setExternalForce(chai3d::cVector3d &a_force)
This method assigns an external force to this mass particle.
Definition CGELMassParticle.h:100
chai3d::cVector3d m_acc
Instant acceleration.
Definition CGELMassParticle.h:185
void applyNextPose()
This method updates the position with its latest computed values.
Definition CGELMassParticle.h:125
static bool s_default_useGravity
Default property - use of gravity field.
Definition CGELMassParticle.h:252
void computeNextPose(double a_timeInterval)
This method updates the simulation over a specified time interval.
Definition CGELMassParticle.h:106
double m_kDampingPos
Linear damping.
Definition CGELMassParticle.h:191
void setMass(double a_mass)
This method assigns the mass of this particle.
Definition CGELMassParticle.cpp:108
double m_mass
Mass property.
Definition CGELMassParticle.h:179
static chai3d::cColorf s_default_color
Default property - color.
Definition CGELMassParticle.h:249
static bool s_scale_force_vector_display
Default property - force vector display magnitude.
Definition CGELMassParticle.h:261
chai3d::cVector3d m_pos
Position of particle.
Definition CGELMassParticle.h:220
chai3d::cVector3d m_gravity
Gravity field.
Definition CGELMassParticle.h:200
bool m_useGravity
If true, then gravity field is taken into account.
Definition CGELMassParticle.h:197
cGELMassParticle()
Constructor of cGELMassParticle.
Definition CGELMassParticle.cpp:75
static chai3d::cVector3d s_default_gravity
Default property - gravity field.
Definition CGELMassParticle.h:255
static double s_default_kDampingPos
Default property - linear damping.
Definition CGELMassParticle.h:243
void render() const
void mulr(const double &a_scalar, cVector3d &a_result) const
void add(const cVector3d &a_vector)