Panda3D
linearNoiseForce.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file linearNoiseForce.cxx
10  * @author charles
11  * @date 2000-06-13
12  */
13 
14 #include <stdlib.h>
15 #include <math.h>
16 
17 #include "linearNoiseForce.h"
18 
19 // declare the statics
20 
21 ConfigVariableInt LinearNoiseForce::_random_seed
22 ("default_noise_force_seed", 665);
23 
24 bool LinearNoiseForce::_initialized = false;
25 unsigned char LinearNoiseForce::_prn_table[256];
26 LVector3 LinearNoiseForce::_gradient_table[256];
27 
28 TypeHandle LinearNoiseForce::_type_handle;
29 
30 /**
31  * One-time config function, sets up the PRN lattice.
32  */
35  // since this is a repeatable noise function, we always want to init with
36  // the same seed.
37  srand(_random_seed);
38 
39  LVector3 *gtable = _gradient_table;
40  for (int i = 0; i < 256; ++i) {
41  // fill the 1d array
42  _prn_table[i] = rand() & 255;
43 
44  // now fill the gradient array
45  *gtable++ = random_unit_vector();
46  }
47 }
48 
49 /**
50  * constructor
51  */
53 LinearNoiseForce(PN_stdfloat a, bool mass) :
54  LinearRandomForce(a, mass) {
55  if (_initialized == false) {
57  _initialized = true;
58  }
59 }
60 
61 /**
62  * copy constructor
63  */
66  LinearRandomForce(copy) {
67 }
68 
69 /**
70  * destructor
71  */
74 }
75 
76 /**
77  * copier
78  */
79 LinearForce *LinearNoiseForce::
80 make_copy() {
81  return new LinearNoiseForce(*this);
82 }
83 
84 /**
85  * Returns the noise value based on the object's position.
86  */
87 LVector3 LinearNoiseForce::
88 get_child_vector(const PhysicsObject *po) {
89  LPoint3 p = po->get_position();
90 
91  // get all of the components
92  PN_stdfloat int_x, int_y, int_z;
93  PN_stdfloat frac_x, frac_y, frac_z;
94 
95  frac_x = std::modf(p[0], &int_x);
96  frac_y = std::modf(p[1], &int_y);
97  frac_z = std::modf(p[2], &int_z);
98 
99  // apply the cubic smoother to the fractional values
100  PN_stdfloat cubic_x, cubic_y, cubic_z;
101 
102  cubic_x = cubic_step(frac_x);
103  cubic_y = cubic_step(frac_y);
104  cubic_z = cubic_step(frac_z);
105 
106  // trilinear interpolation into the cube (over, in, down)
107  LVector3 temp0, temp1, temp2, temp3;
108 
109  // x direction
110  temp0 = vlerp(cubic_x, get_lattice_entry(p),
111  get_lattice_entry(p[0] + 1.0f, p[1], p[2]));
112 
113  temp1 = vlerp(cubic_x, get_lattice_entry(p[0], p[1], p[2] + 1.0f),
114  get_lattice_entry(p[0] + 1.0f, p[1], p[2] + 1.0f));
115 
116  temp2 = vlerp(cubic_x, get_lattice_entry(p[0], p[1] + 1.0f, p[2]),
117  get_lattice_entry(p[0] + 1.0f, p[1] + 1.0f, p[2]));
118 
119  temp3 = vlerp(cubic_x, get_lattice_entry(p[0], p[1] + 1.0f, p[2] + 1.0f),
120  get_lattice_entry(p[0] + 1.0f, p[1] + 1.0f, p[2] + 1.0f));
121 
122  // y direction
123  temp0 = vlerp(cubic_z, temp0, temp1);
124  temp1 = vlerp(cubic_z, temp2, temp3);
125 
126  // z direction
127  return vlerp(cubic_y, temp0, temp1);
128 }
129 
130 /**
131  * Write a string representation of this instance to <out>.
132  */
134 output(std::ostream &out) const {
135  #ifndef NDEBUG //[
136  out<<""<<"LinearNoiseForce";
137  #endif //] NDEBUG
138 }
139 
140 /**
141  * Write a string representation of this instance to <out>.
142  */
144 write(std::ostream &out, int indent) const {
145  #ifndef NDEBUG //[
146  out.width(indent);
147  out<<""<<"LinearNoiseForce:";
148  out.width(indent+2); out<<""; out<<"_random_seed"<<_random_seed<<" (class)\n";
149  out.width(indent+2); out<<""; out<<"_initialized"<<_initialized<<" (class)\n";
150  out.width(indent+2); out<<""; out<<"_gradient_table"<<_gradient_table<<" (class)\n";
151  out.width(indent+2); out<<""; out<<"_prn_table"<<_prn_table<<" (class)\n";
153  #endif //] NDEBUG
154 }
This is a convenience class to specialize ConfigVariable as an integer type.
A force that acts on a PhysicsObject by way of an Integrator.
Definition: linearForce.h:23
Repeating noise force vector.
virtual void output(std::ostream &out) const
Write a string representation of this instance to <out>.
static void init_noise_tables()
One-time config function, sets up the PRN lattice.
virtual ~LinearNoiseForce()
destructor
LinearNoiseForce(PN_stdfloat a=1.0f, bool m=false)
constructor
virtual void write(std::ostream &out, int indent=0) const
Write a string representation of this instance to <out>.
Pure virtual, parent to noiseForce and jitterForce.
virtual void write(std::ostream &out, int indent=0) const
Write a string representation of this instance to <out>.
A body on which physics will be applied.
Definition: physicsObject.h:27
get_position
Position Query.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.