Panda3D
linearNoiseForce.cxx
1 // Filename: linearNoiseForce.cxx
2 // Created by: charles (13Jun00)
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 <stdlib.h>
16 #include <math.h>
17 
18 #include "linearNoiseForce.h"
19 
20 // declare the statics
21 
22 ConfigVariableInt LinearNoiseForce::_random_seed
23 ("default_noise_force_seed", 665);
24 
25 bool LinearNoiseForce::_initialized = false;
26 unsigned char LinearNoiseForce::_prn_table[256];
27 LVector3 LinearNoiseForce::_gradient_table[256];
28 
29 TypeHandle LinearNoiseForce::_type_handle;
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function : InitNoiseTables
33 // Access : Public
34 // Description : One-time config function, sets up the PRN
35 // lattice.
36 ////////////////////////////////////////////////////////////////////
39  // since this is a repeatable noise function, we always want
40  // to init with the same seed.
41  srand(_random_seed);
42 
43  LVector3 *gtable = _gradient_table;
44  for (int i = 0; i < 256; ++i) {
45  // fill the 1d array
46  _prn_table[i] = rand() & 255;
47 
48  // now fill the gradient array
49  *gtable++ = random_unit_vector();
50  }
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function : LinearNoiseForce
55 // Access : Public
56 // Description : constructor
57 ////////////////////////////////////////////////////////////////////
59 LinearNoiseForce(PN_stdfloat a, bool mass) :
60  LinearRandomForce(a, mass) {
61  if (_initialized == false) {
63  _initialized = true;
64  }
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function : LinearNoiseForce
69 // Access : Public
70 // Description : copy constructor
71 ////////////////////////////////////////////////////////////////////
74  LinearRandomForce(copy) {
75 }
76 
77 ////////////////////////////////////////////////////////////////////
78 // Function : ~LinearNoiseForce
79 // Access : Public
80 // Description : destructor
81 ////////////////////////////////////////////////////////////////////
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function : make_copy
88 // Access : Public
89 // Description : copier
90 ////////////////////////////////////////////////////////////////////
91 LinearForce *LinearNoiseForce::
92 make_copy() {
93  return new LinearNoiseForce(*this);
94 }
95 
96 ////////////////////////////////////////////////////////////////////
97 // Function : get_child_vector
98 // Access : Public
99 // Description : Returns the noise value based on the object's
100 // position.
101 ////////////////////////////////////////////////////////////////////
102 LVector3 LinearNoiseForce::
103 get_child_vector(const PhysicsObject *po) {
104  LPoint3 p = po->get_position();
105 
106  // get all of the components
107  int int_x, int_y, int_z;
108  PN_stdfloat frac_x, frac_y, frac_z;
109 
110  int_x = (int) p[0];
111  frac_x = p[0] - int_x;
112 
113  int_y = (int) p[1];
114  frac_y = p[1] - int_y;
115 
116  int_z = (int) p[2];
117  frac_z = p[2] - int_z;
118 
119  // apply the cubic smoother to the fractional values
120  PN_stdfloat cubic_x, cubic_y, cubic_z;
121 
122  cubic_x = cubic_step(frac_x);
123  cubic_y = cubic_step(frac_y);
124  cubic_z = cubic_step(frac_z);
125 
126  // trilinear interpolation into the cube (over, in, down)
127  LVector3 temp0, temp1, temp2, temp3;
128 
129  // x direction
130  temp0 = vlerp(cubic_x, get_lattice_entry(p),
131  get_lattice_entry(p[0] + 1.0f, p[1], p[2]));
132 
133  temp1 = vlerp(cubic_x, get_lattice_entry(p[0], p[1], p[2] + 1.0f),
134  get_lattice_entry(p[0] + 1.0f, p[1], p[2] + 1.0f));
135 
136  temp2 = vlerp(cubic_x, get_lattice_entry(p[0], p[1] + 1.0f, p[2]),
137  get_lattice_entry(p[0] + 1.0f, p[1] + 1.0f, p[2]));
138 
139  temp3 = vlerp(cubic_x, get_lattice_entry(p[0], p[1] + 1.0f, p[2] + 1.0f),
140  get_lattice_entry(p[0] + 1.0f, p[1] + 1.0f, p[2] + 1.0f));
141 
142  // y direction
143  temp0 = vlerp(cubic_z, temp0, temp1);
144  temp1 = vlerp(cubic_z, temp2, temp3);
145 
146  // z direction
147  return vlerp(cubic_y, temp0, temp1);
148 }
149 
150 ////////////////////////////////////////////////////////////////////
151 // Function : output
152 // Access : Public
153 // Description : Write a string representation of this instance to
154 // <out>.
155 ////////////////////////////////////////////////////////////////////
157 output(ostream &out) const {
158  #ifndef NDEBUG //[
159  out<<""<<"LinearNoiseForce";
160  #endif //] NDEBUG
161 }
162 
163 ////////////////////////////////////////////////////////////////////
164 // Function : write
165 // Access : Public
166 // Description : Write a string representation of this instance to
167 // <out>.
168 ////////////////////////////////////////////////////////////////////
170 write(ostream &out, unsigned int indent) const {
171  #ifndef NDEBUG //[
172  out.width(indent);
173  out<<""<<"LinearNoiseForce:";
174  out.width(indent+2); out<<""; out<<"_random_seed"<<_random_seed<<" (class)\n";
175  out.width(indent+2); out<<""; out<<"_initialized"<<_initialized<<" (class)\n";
176  out.width(indent+2); out<<""; out<<"_gradient_table"<<_gradient_table<<" (class)\n";
177  out.width(indent+2); out<<""; out<<"_prn_table"<<_prn_table<<" (class)\n";
178  LinearRandomForce::write(out, indent+2);
179  #endif //] NDEBUG
180 }
virtual void output(ostream &out) const
Write a string representation of this instance to <out>.
virtual ~LinearNoiseForce()
destructor
virtual void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
Pure virtual, parent to noiseForce and jitterForce.
LinearNoiseForce(PN_stdfloat a=1.0f, bool m=false)
constructor
A body on which physics will be applied.
Definition: physicsObject.h:29
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
virtual void write(ostream &out, unsigned int indent=0) const
Write a string representation of this instance to <out>.
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
A force that acts on a PhysicsObject by way of an Integrator.
Definition: linearForce.h:25
Repeating noise force vector.
static void init_noise_tables()
One-time config function, sets up the PRN lattice.
LPoint3 get_position() const
Position Query.
This is a convenience class to specialize ConfigVariable as an integer type.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85