Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
working biut slow axis method
  • Loading branch information
rtv committed Feb 5, 2011
1 parent f607370 commit 7695d17
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 22 deletions.
95 changes: 76 additions & 19 deletions antix.cc
Expand Up @@ -11,6 +11,8 @@
#include "antix.h"
using namespace Antix;

unsigned int Robot::mbits( 4 );

// initialize static members
bool Robot::paused( false );
bool Robot::show_data( false );
Expand All @@ -30,7 +32,7 @@ unsigned int Robot::home_count(1);
unsigned int Robot::home_population( 20 );
unsigned int Robot::puck_count(100);
unsigned int Robot::sleep_msec( 10 );

std::vector<std::set<Robot*> > Robot::matrix( (1<<Robot::mbits) * (1<<Robot::mbits) );
std::vector<Robot*> RobotsByX;
std::vector<Robot*> RobotsByY;

Expand Down Expand Up @@ -63,7 +65,8 @@ Robot::Robot( Home* home,
speed(),
see_robots(),
see_pucks(),
puck_held(NULL)
puck_held(NULL),
index(0)
{
// add myself to the static vector of all robots
population.push_back( this );
Expand Down Expand Up @@ -171,7 +174,7 @@ void Robot::UpdateSensors()
// note: the following two large Fsensing operations could safely be
// done in parallel since they do not modify any common data

#if 0
#if 0
// first fill the robot sensor
// check every robot in the world to see if it is detected
FOR_EACH( it, population )
Expand Down Expand Up @@ -211,27 +214,57 @@ void Robot::UpdateSensors()
other->Holding() ) );
}
#else

std::set<Robot*> xneighbors, yneighbors;

for( Robot* l=left; l && pose.x - l->pose.x < Robot::range; l=l->left )
xneighbors.insert( l );
for( Robot* r=right; r && r->pose.x - pose.x < Robot::range; r=r->right )
xneighbors.insert( r );

for( Robot* d=down; d && pose.y - d->pose.y < Robot::range; d=d->down )
yneighbors.insert( d );
for( Robot* u=up; u && u->pose.y - pose.y < Robot::range; u=u->up )
yneighbors.insert( u );

neighbors.clear();
std::set_intersection( xneighbors.begin(), xneighbors.end(),
yneighbors.begin(), yneighbors.end(),
std::inserter( neighbors, neighbors.end() ) );

for( Robot* l=left; l && pose.x - l->pose.x < Robot::range; l=l->left )
{
bool found(false);

for( Robot* d=down; d && pose.y - d->pose.y < Robot::range; d=d->down )
if( l == d )
{
found=true;
break;
}

for( Robot* u=up; u && u->pose.y - pose.y < Robot::range; u=u->up )
if( l == u )
{
found=true;
break;
}

if( found )
neighbors.push_back( l );
}


for( Robot* r=right; r && r->pose.x - pose.x < Robot::range; r=r->right )
{
bool found(false);

for( Robot* d=down; d && pose.y - d->pose.y < Robot::range; d=d->down )
if( r == d )
{
found=true;
break;
}

for( Robot* u=up; u && u->pose.y - pose.y < Robot::range; u=u->up )
if( r == u )
{
found=true;
break;
}

if( found )
neighbors.push_back( r );
}


// check all the neighbors for FOV and hypot distance
FOR_EACH( it, neighbors )
FOR_EACH( it, neighbors )
{
Robot* other = *it;
double dx( WrapDistance( other->pose.x - pose.x ) );
Expand Down Expand Up @@ -326,7 +359,23 @@ bool Robot::Drop()
}
return false; // nothing to drop
}

unsigned int Cell( double x, double y )
{
double d = Robot::worldsize / (double)(1<<Robot::mbits);

unsigned int cx( floor( x / d ));
unsigned int cy( floor( y / d ));

const unsigned int i(cx + (cy << Robot::mbits) );

//printf( "(%.2f %.2f) [%u %u]=> %u\n", x, y, cx, cy, i );

assert( i < ((1<<Robot::mbits) * (1<<Robot::mbits)));

return i;
}

void Robot::UpdatePose()
{
// move according to the current speed
Expand All @@ -337,7 +386,15 @@ void Robot::UpdatePose()
pose.x = DistanceNormalize( pose.x + dx );
pose.y = DistanceNormalize( pose.y + dy );
pose.a = AngleNormalize( pose.a + da );

unsigned int newindex = Cell( pose.x, pose.y );

if( newindex != index )
{
matrix[index].erase( this );
matrix[newindex].insert( this );
index = newindex;
}

// if we're carrying a puck, update it's position
if( puck_held )
Expand Down
7 changes: 6 additions & 1 deletion antix.h
Expand Up @@ -98,6 +98,11 @@ namespace Antix

static Robot* first;

static std::vector<std::set<Robot*> > matrix;
static unsigned int mbits;

unsigned int index; // the matrix cell that currently holds this robot

#if GRAPHICS
static int winsize; // initial size of the window in pixels

Expand Down Expand Up @@ -209,7 +214,7 @@ namespace Antix
/* std::set<Robot*> up_set; */
/* std::set<Robot*> down_set; */

std::set<Robot*> neighbors;
std::vector<Robot*> neighbors;

// constructor
Robot( Home* home, const Pose& pose );
Expand Down
2 changes: 0 additions & 2 deletions controller.h
@@ -1,8 +1,6 @@

#include "antix.h"

static double xx(0.0);

// this is the robot controller code
class Forager : public Antix::Robot
{
Expand Down
16 changes: 16 additions & 0 deletions gui.cc
Expand Up @@ -131,6 +131,22 @@ void Robot::DrawAll()
}
glEnd();
#endif

// draw the matrix
double d = worldsize / (double)(1<<mbits);

glBegin( GL_LINES );
for( int x(0); x < (1<<mbits); x++ )
{
glVertex2f( x*d, 0 );
glVertex2f( x*d, worldsize );
}
for( int y(0); y < (1<<mbits); y++ )
{
glVertex2f( 0, y*d );
glVertex2f( worldsize, y*d );
}
glEnd();
}

// draw a robot
Expand Down

0 comments on commit 7695d17

Please sign in to comment.