[osg-users] Flocking

2011-01-21 Thread john song
i am suppose to implement flocking but im not sure how can anyone help??

Puck. h: 


#ifndef PUCK_H
#define PUCK_H
#include 
#include 
#include 
#include 
#include 
#include 

#include 

#include "Obstacle.h"

using namespace osg;
using namespace osgViewer;
using namespace std;

class Puck
{
public:
Puck(Group *root, Obstacle** obstacles, int ob_count, double x, double 
y);
};


#endif 









obstacle.h:

#ifndef OBSTACLE_H
#define OBSTACLE_H
#include 
#include 
#include 
#include 
#include 

#include 

using namespace osg;
using namespace osgViewer;

class Obstacle
{

public:
double x,y;
double radius;
Obstacle(Group *root, double x, double y, double radius);
};
#endif 





obstacle.cpp:

#include "Obstacle.h"

Obstacle::Obstacle(Group *root, double x_pos, double y_pos, double r) 
{
x = x_pos;
y = y_pos;
radius = r;
Cylinder *shape=new Cylinder(Vec3(x,y,0),radius,0.1);
ShapeDrawable *draw=new ShapeDrawable(shape);
draw->setColor(Vec4(1,0,1,0));
Geode *geode=new Geode();
geode->addDrawable(draw);

MatrixTransform *T=new MatrixTransform();
T->addChild(geode);

root->addChild(T);
}



puck.cpp:

#include "Puck.h"

#ifndef M_PI
#define M_PI 3.1415
#endif

class TranslateCB : public osg::NodeCallback
{
private: 
double _dx,_dy;
double _dirx,_diry;
double _inc;
double _theta;
double _radius;
double _x,_y;
Obstacle** obstacles;
int ob_count;

public:
TranslateCB() : _dx( 0. ), _dy( 0. ), _dirx(1), _diry(0), _inc(0.2), 
_theta(0) {}
TranslateCB(Obstacle** ob, int count, double r, double x, double y) : 
_dx( 0. ), _dy( 0. ), 
_dirx(2.0*rand()/RAND_MAX-1), _diry(2.0*rand()/RAND_MAX-1), 
_inc(0.001), _theta(0)
{ obstacles = ob; ob_count = count; _radius = r; _x = x; _y = 
y; }

virtual void operator()( osg::Node* node, osg::NodeVisitor* nv )
{
osg::MatrixTransform* mt =
dynamic_cast( node );
osg::Matrix mR, mT;
mT.makeTranslate( _dx , _dy, 0. );
mt->setMatrix( mT );

double ob_dirx;
double ob_diry;
double ob_dist;

_theta = 0;
double min = 4;
for(int i = 0; i < ob_count; i++)
{
ob_dirx = (_x+_dx) - obstacles[i]->x;
ob_diry = (_y+_dy) - obstacles[i]->y;
ob_dist = sqrt(ob_dirx*ob_dirx+ob_diry*ob_diry);

//normalise directions
double ob_norm = sqrt(ob_dirx*ob_dirx+ob_diry*ob_diry);
ob_dirx = (ob_dirx)/ob_norm;
ob_diry = (ob_diry)/ob_norm;
double norm = sqrt(_dirx*_dirx+_diry*_diry);
_dirx = (_dirx)/norm;
_diry = (_diry)/norm;

//calculate angle between direction travelling, and 
direction to obstacle
double angle = acos(_dirx*ob_dirx + _diry*ob_diry);
//calculate closest distance between puck and obstacle 
if continues on same path
double min_dist = ob_dist*sin(angle);

if(min_dist < _radius + obstacles[i]->radius  && 
ob_dist < min+obstacles[i]->radius)
{
min = ob_dist;
if(ob_dist < _radius + obstacles[i]->radius){ 
_theta = 0; }
else if(angle < M_PI/2){ _theta = -0.3; }
else{ _theta = 0.3; }
}
}

//change direction accordingly
_dirx = _dirx*cos(_theta) + _diry*sin(_theta);
_diry = _diry*cos(_theta) - _dirx*sin(_theta);

_dx += _inc*_dirx;
if((_x+_dx > 10 && _dirx > 0) || (_x+_dx < -10 && _dirx < 0))
{
_dirx = -_dirx;
_diry += (0.2*rand()/RAND_MAX-0.1); //add 
randomness to bounce
}
_dy += _inc*_diry;
if((_y+_dy > 10 && _diry > 0) || (_y+_dy < -10 && _diry < 0))
{
_diry = -_diry;
_dirx += (0.2*rand()/RAND_MAX-0.1); //add 
randomness to bounce
}

traverse( node, nv );
}

};

Puck::Puck(Group *root, Obstacle** obstacles, int ob_count, double x, double y)
{
// geometry
double radius = 0.2;
Cylinder *shape=new Cylinder(Vec3(x,

Re: [osg-users] Flocking

2011-01-21 Thread Jason Daly

On 01/21/2011 11:31 AM, john song wrote:

i am suppose to implement flocking but im not sure how can anyone help??


I don't have time to debug your code for you, but this site is an 
excellent reference for steering behaviors like flocking:


http://www.red3d.com/cwr/steer/


They also have a library that you can use if you don't want to code it 
yourself.


--"J"

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org