26 const static double rand_max = 0x7fff;
27 return ((rand()) / (rand_max + 1.0));
38 double random_clamped() {
39 return (rand_float() - rand_float());
42 Wander::Wander(
AICharacter *ai_ch,
double wander_radius,
int flag,
double aoe,
float wander_weight) {
44 _wander_radius = wander_radius ;
45 _wander_weight = wander_weight;
46 double theta = rand_float() * 2 * 3.14159;
47 double si = rand_float() * 3.14159;
50 _area_of_effect = aoe;
51 _init_pos = _ai_char->get_node_path().
get_pos(_ai_char->get_char_render());
60 _wander_target =
LVecBase3(_wander_radius * cos(theta), _wander_radius * sin(theta),0);
64 _wander_target =
LVecBase3(0, _wander_radius * cos(theta), _wander_radius * sin(theta));
68 _wander_target =
LVecBase3(_wander_radius * cos(theta), 0, _wander_radius * sin(theta));
72 _wander_target =
LVecBase3(_wander_radius * sin(theta) * cos(si), _wander_radius * sin(theta) * sin(si), _wander_radius * cos(theta));
76 _wander_target =
LVecBase3(_wander_radius * cos(theta), _wander_radius * sin(theta),0);
95 LVecBase3 present_pos = _ai_char->get_node_path().
get_pos(_ai_char->get_char_render());
97 double time_slice_1 = random_clamped() * 1.5;
98 double time_slice_2 = random_clamped() * 1.5;
99 double time_slice_3 = random_clamped() * 1.5;
102 _wander_target +=
LVecBase3(time_slice_1, time_slice_2, 0);
106 _wander_target +=
LVecBase3(0, time_slice_1, time_slice_2);
110 _wander_target +=
LVecBase3(time_slice_1, 0, time_slice_2);
114 _wander_target +=
LVecBase3(time_slice_1, time_slice_2, time_slice_3);
119 _wander_target =
LVecBase3(time_slice_1, time_slice_2, 0);
123 _wander_target *= _wander_radius;
127 target = _wander_target + target;
128 LVecBase3 desired_target = present_pos + target;
129 LVecBase3 desired_force = desired_target - _ai_char->get_node_path().
get_pos() ;
131 desired_force *= _ai_char->_movt_force;
132 double distance = (present_pos - _init_pos).length();
133 if(_area_of_effect > 0 && distance > _area_of_effect) {
134 LVecBase3 direction = present_pos - _init_pos;
136 desired_force = - direction * _ai_char->_movt_force;
137 LVecBase3 dirn = _ai_char->_steering->_steering_force;
139 _ai_char->_steering->_steering_force =
LVecBase3(0.0, 0.0, 0.0);
141 return desired_force;
This is the base class for all three-component vectors and points.
static LVector3f forward(CoordinateSystem cs=CS_default)
Returns the forward vector for the given coordinate system.
LPoint3 get_pos() const
Retrieves the translation component of the transform.
LVector3 get_relative_vector(const NodePath &other, const LVecBase3 &vec) const
Given that the indicated vector is in the coordinate system of the other node, returns the same vecto...
LVecBase3 do_wander()
This function performs the wander and returns the wander force which is used in the calculate_priorit...
bool normalize()
Normalizes the vector in place.