Wander steering behavior

Hi,

I’m playing around with steering behaviors and ofxVec2f. This code looks like it’s working, though I’m not quite sure. I’m trying to get wandering behavior working. Does someone wants to have a look at it and tell me if I’m doing something wrong?

  
  
#include "Boid.h"  
  
Boid::Boid() {  
	//ctor  
	m_wander_angle = 0;  
	m_wander_distance = 60;  
	m_wander_radius = 30;  
	m_wander_range = 15;  
	m_vel.set(2,2);  
}  
  
Boid::~Boid() {  
}  
  
void Boid::setPos(float fX, float fY) {  
	m_pos.set(fX, fY);  
}  
ofxVec2f Boid::getPos() {  
	return m_pos;  
}  
  
void Boid::update() {  
	m_vel.limit(3);  
	m_pos += m_vel;  
	borders();  
	//m_pos.rotate(45.0f);  
  
}  
  
void Boid::draw() {  
	// Draw ourself  
	// ---------------------------------------  
	ofSetColor(0xFF00FF);  
	ofFill();  
	ofCircle(m_pos.x, m_pos.y, 3);  
	ofNoFill();  
	ofCircle(m_pos.x, m_pos.y, 10);  
  
	// Get the center of the wander circle, and draw it.  
	// ---------------------------------------------------------  
	//ofxVec2f center = m_pos + m_pos.getScaled(m_wander_distance);  
	ofxVec2f center = m_vel;  
	center.normalize();  
	center *= m_wander_distance;  
	center += m_pos;  
  
	ofSetColor(0xCCCCCC);  
	ofFill();  
	ofCircle(center.x, center.y,3);  
	ofNoFill();  
	ofCircle(center.x, center.y, m_wander_radius);  
	ofSetColor(0xFF00FF);  
  
  
	// Get the position on the radius of the wander circle  
	ofxVec2f offset = center;  
	offset = center + offset.getScaled(m_wander_radius); // getScaled does not 'add up'. It returns a new vec2f for the given length and current angle.  
  
	// because vec2f rotates around 0,0 we need:  
	// 1. "move back" to 0,0  
	// 2. then rotate  
	// 3. reset the original position  
	offset -= center;  
	offset = offset.rotated(m_wander_angle);  
	m_vel += offset.normalized(); // we need to add this here  
	offset += center;  
  
  
  
	// increase the angle  
	m_wander_angle += ofRandomf() * m_wander_range;  
  
	// And draw some info.  
	ofFill();  
	ofSetColor(0x00FF00);  
	ofLine(center.x, center.y, offset.x, offset.y);  
	ofSetColor(0x00FFDD);  
	ofCircle(offset.x, offset.y, 4);  
	ofLine(m_pos.x, m_pos.y,center.x, center.y);  
}  
  
void Boid::setWanderDistance(float fDist) {  
	m_wander_distance = fDist;  
}  
  
void Boid::setWanderRadius(float fRad) {  
	m_wander_radius = fRad;  
}  
  
void Boid::setWanderRange(float fRange) {  
	m_wander_range = fRange;  
}  
  
void Boid::borders() {  
	float r = 2;  
	if (m_pos.x < -r) m_pos.x = ofGetWidth()+ r;  
	if (m_pos.y < -r) m_pos.y = ofGetHeight() + r;  
	if (m_pos.x > ofGetWidth()+r) m_pos.x = -r;  
	if (m_pos.y > ofGetHeight()+r) m_pos.y = -r;  
}  
  

And the header

  
  
#ifndef BOID_H  
#define BOID_H  
  
#include "ofMain.h"  
#include "ofxVectorMath.h"  
  
class Boid {  
	public:  
		Boid();  
		virtual ~Boid();  
		void setPos(float fX, float fY);  
		ofxVec2f getPos();  
  
		void update();  
		void borders();  
		void draw();  
  
		void setWanderDistance(float fDist);  
		void setWanderRadius(float fRad);  
		void setWanderRange(float fRange);  
	protected:  
	private:  
		ofxVec2f m_pos;  
		ofxVec2f m_vel;  
		float m_wander_distance;  
		float m_wander_angle;  
		float m_wander_radius;  
		float m_wander_range;  
  
};  
  
#endif // BOID_H  
  
  

Roxlu