Panda3D
discEmitter.cxx
1 // Filename: discEmitter.cxx
2 // Created by: charles (22Jun00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "discEmitter.h"
16 
17 ////////////////////////////////////////////////////////////////////
18 // Function : DiscEmitter::DiscEmitter
19 // Access : Public
20 // Description : constructor
21 ////////////////////////////////////////////////////////////////////
24  _radius = 1.0f;
25  _inner_aoe = _outer_aoe = 0.0f;
26  _inner_magnitude = _outer_magnitude = 1.0f;
27  _cubic_lerping = false;
28 }
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function : DiscEmitter::DiscEmitter
32 // Access : Public
33 // Description : copy constructor
34 ////////////////////////////////////////////////////////////////////
36 DiscEmitter(const DiscEmitter &copy) :
37  BaseParticleEmitter(copy) {
38  _radius = copy._radius;
39  _inner_aoe = copy._inner_aoe;
40  _outer_aoe = copy._outer_aoe;
41  _inner_magnitude = copy._inner_magnitude;
42  _outer_magnitude = copy._outer_magnitude;
43  _cubic_lerping = copy._cubic_lerping;
44 
45  _distance_from_center = copy._distance_from_center;
46  _sinf_theta = copy._sinf_theta;
47  _cosf_theta = copy._cosf_theta;
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function : DiscEmitter::~DiscEmitter
52 // Access : Public
53 // Description : destructor
54 ////////////////////////////////////////////////////////////////////
57 }
58 
59 ////////////////////////////////////////////////////////////////////
60 // Function : make_copy
61 // Access : Public
62 // Description : copier
63 ////////////////////////////////////////////////////////////////////
66  return new DiscEmitter(*this);
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function : DiscEmitter::assign_initial_position
71 // Access : Public
72 // Description : Generates a location for a new particle
73 ////////////////////////////////////////////////////////////////////
74 void DiscEmitter::
75 assign_initial_position(LPoint3& pos) {
76  // position
77  PN_stdfloat theta = NORMALIZED_RAND() * 2.0f * MathNumbers::pi_f;
78 
79  _distance_from_center = NORMALIZED_RAND();
80  PN_stdfloat r_scalar = _distance_from_center * _radius;
81 
82  _sinf_theta = sinf(theta);
83  _cosf_theta = cosf(theta);
84 
85  PN_stdfloat new_x = _cosf_theta * r_scalar;
86  PN_stdfloat new_y = _sinf_theta * r_scalar;
87 
88  pos.set(new_x, new_y, 0.0f);
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function : DiscEmitter::assign_initial_velocity
93 // Access : Public
94 // Description : Generates a velocity for a new particle
95 ////////////////////////////////////////////////////////////////////
96 void DiscEmitter::
97 assign_initial_velocity(LVector3& vel) {
98  PN_stdfloat aoe, mag;
99 
100  // lerp type
101  if (_cubic_lerping == true) {
102  aoe = CLERP(_distance_from_center, _inner_aoe, _outer_aoe);
103  mag = CLERP(_distance_from_center, _inner_magnitude, _outer_magnitude);
104  }
105  else {
106  aoe = LERP(_distance_from_center, _inner_aoe, _outer_aoe);
107  mag = LERP(_distance_from_center, _inner_magnitude, _outer_magnitude);
108  }
109 
110  // velocity
111  PN_stdfloat vel_z = mag * sinf(deg_2_rad(aoe));
112  PN_stdfloat abs_diff = fabs((mag * mag) - (vel_z * vel_z));
113  PN_stdfloat root_mag_minus_z_squared = sqrtf(abs_diff);
114  PN_stdfloat vel_x = _cosf_theta * root_mag_minus_z_squared;
115  PN_stdfloat vel_y = _sinf_theta * root_mag_minus_z_squared;
116 
117  // quick and dirty
118  if((aoe > 90.0f) && (aoe < 270.0f))
119  {
120  vel_x = -vel_x;
121  vel_y = -vel_y;
122  }
123 
124  vel.set(vel_x, vel_y, vel_z);
125 }
126 
127 ////////////////////////////////////////////////////////////////////
128 // Function : output
129 // Access : Public
130 // Description : Write a string representation of this instance to
131 // <out>.
132 ////////////////////////////////////////////////////////////////////
133 void DiscEmitter::
134 output(ostream &out) const {
135  #ifndef NDEBUG //[
136  out<<"DiscEmitter";
137  #endif //] NDEBUG
138 }
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function : write
142 // Access : Public
143 // Description : Write a string representation of this instance to
144 // <out>.
145 ////////////////////////////////////////////////////////////////////
146 void DiscEmitter::
147 write(ostream &out, int indent) const {
148  #ifndef NDEBUG //[
149  out.width(indent); out<<""; out<<"DiscEmitter:\n";
150  out.width(indent+2); out<<""; out<<"_radius "<<_radius<<"\n";
151  out.width(indent+2); out<<""; out<<"_outer_aoe "<<_outer_aoe<<"\n";
152  out.width(indent+2); out<<""; out<<"_inner_aoe "<<_inner_aoe<<"\n";
153  out.width(indent+2); out<<""; out<<"_outer_magnitude "<<_outer_magnitude<<"\n";
154  out.width(indent+2); out<<""; out<<"_inner_magnitude "<<_inner_magnitude<<"\n";
155  out.width(indent+2); out<<""; out<<"_cubic_lerping "<<_cubic_lerping<<"\n";
156  out.width(indent+2); out<<""; out<<"_distance_from_center "<<_distance_from_center<<"\n";
157  out.width(indent+2); out<<""; out<<"_sinf_theta "<<_sinf_theta<<"\n";
158  out.width(indent+2); out<<""; out<<"_cosf_theta "<<_cosf_theta<<"\n";
159  BaseParticleEmitter::write(out, indent+2);
160  #endif //] NDEBUG
161 }
virtual ~DiscEmitter()
destructor
Definition: discEmitter.cxx:56
virtual BaseParticleEmitter * make_copy()
copier
Definition: discEmitter.cxx:65
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
Describes a planar disc region from which particles are generated.
Definition: discEmitter.h:25
virtual void write(ostream &out, int indent=0) const
Write a string representation of this instance to <out>.
DiscEmitter()
constructor
Definition: discEmitter.cxx:23
virtual void output(ostream &out) const
Write a string representation of this instance to <out>.
virtual void write(ostream &out, int indent=0) const
Write a string representation of this instance to <out>.
Describes a physical region in space in which particles are randomly generated.