Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/physics/linearNoiseForce.cxx

Go to the documentation of this file.
00001 // Filename: linearNoiseForce.cxx
00002 // Created by:  charles (13Jun00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include <stdlib.h>
00020 #include <math.h>
00021 
00022 #include "linearNoiseForce.h"
00023 
00024 // declare the statics
00025 
00026 bool LinearNoiseForce::_initialized = false;
00027 unsigned char LinearNoiseForce::_prn_table[256];
00028 LVector3f LinearNoiseForce::_gradient_table[256];
00029 
00030 TypeHandle LinearNoiseForce::_type_handle;
00031 
00032 ////////////////////////////////////////////////////////////////////
00033 //     Function : InitNoiseTables
00034 //       Access : Public
00035 //  Description : One-time config function, sets up the PRN
00036 //                lattice.
00037 ////////////////////////////////////////////////////////////////////
00038 void LinearNoiseForce::
00039 init_noise_tables(void) {
00040   int i;
00041   LVector3f *gtable = _gradient_table;
00042 
00043   // since this is a repeatable noise function, we always want
00044   // to init with the same seed.
00045   srand(_random_seed);
00046 
00047   for (i = 0; i < 256; i++) {
00048     // fill the 1d array
00049     _prn_table[i] = rand() & 255;
00050 
00051     // now fill the gradient array
00052     *gtable++ = random_unit_vector();
00053   }
00054 }
00055 
00056 ////////////////////////////////////////////////////////////////////
00057 //     Function : LinearNoiseForce
00058 //       Access : Public
00059 //  Description : constructor
00060 ////////////////////////////////////////////////////////////////////
00061 LinearNoiseForce::
00062 LinearNoiseForce(float a, bool mass) :
00063   LinearRandomForce(a, mass) {
00064   if (_initialized == false) {
00065     init_noise_tables();
00066     _initialized = true;
00067   }
00068 }
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function : LinearNoiseForce
00072 //       Access : Public
00073 //  Description : copy constructor
00074 ////////////////////////////////////////////////////////////////////
00075 LinearNoiseForce::
00076 LinearNoiseForce(const LinearNoiseForce &copy) :
00077   LinearRandomForce(copy) {
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function : ~LinearNoiseForce
00082 //       Access : Public
00083 //  Description : destructor
00084 ////////////////////////////////////////////////////////////////////
00085 LinearNoiseForce::
00086 ~LinearNoiseForce(void) {
00087 }
00088 
00089 ////////////////////////////////////////////////////////////////////
00090 //     Function : make_copy
00091 //       Access : Public
00092 //  Description : copier
00093 ////////////////////////////////////////////////////////////////////
00094 LinearForce *LinearNoiseForce::
00095 make_copy(void) {
00096   return new LinearNoiseForce(*this);
00097 }
00098 
00099 ////////////////////////////////////////////////////////////////////
00100 //     Function : get_child_vector
00101 //       Access : Public
00102 //  Description : Returns the noise value based on the object's
00103 //                position.
00104 ////////////////////////////////////////////////////////////////////
00105 LVector3f LinearNoiseForce::
00106 get_child_vector(const PhysicsObject *po) {
00107   LPoint3f p = po->get_position();
00108 
00109   // get all of the components
00110   int int_x, int_y, int_z;
00111   float frac_x, frac_y, frac_z;
00112 
00113   int_x = (int) p[0];
00114   frac_x = p[0] - int_x;
00115 
00116   int_y = (int) p[1];
00117   frac_y = p[1] - int_y;
00118 
00119   int_z = (int) p[2];
00120   frac_z = p[2] - int_z;
00121 
00122   // apply the cubic smoother to the fractional values
00123   float cubic_x, cubic_y, cubic_z;
00124 
00125   cubic_x = cubic_step(frac_x);
00126   cubic_y = cubic_step(frac_y);
00127   cubic_z = cubic_step(frac_z);
00128 
00129   // trilinear interpolation into the cube (over, in, down)
00130   LVector3f temp0, temp1, temp2, temp3;
00131 
00132   // x direction
00133   temp0 = vlerp(cubic_x, get_lattice_entry(p),
00134                 get_lattice_entry(p[0] + 1.0f, p[1], p[2]));
00135 
00136   temp1 = vlerp(cubic_x, get_lattice_entry(p[0], p[1], p[2] + 1.0f),
00137                 get_lattice_entry(p[0] + 1.0f, p[1], p[2] + 1.0f));
00138 
00139   temp2 = vlerp(cubic_x, get_lattice_entry(p[0], p[1] + 1.0f, p[2]),
00140                 get_lattice_entry(p[0] + 1.0f, p[1] + 1.0f, p[2]));
00141 
00142   temp3 = vlerp(cubic_x, get_lattice_entry(p[0], p[1] + 1.0f, p[2] + 1.0f),
00143                 get_lattice_entry(p[0] + 1.0f, p[1] + 1.0f, p[2] + 1.0f));
00144 
00145   // y direction
00146   temp0 = vlerp(cubic_z, temp0, temp1);
00147   temp1 = vlerp(cubic_z, temp2, temp3);
00148 
00149   // z direction
00150   return vlerp(cubic_y, temp0, temp1);
00151 }

Generated on Fri May 2 00:42:57 2003 for Panda by doxygen1.3