Simulation of LUAbot
(EN google-translate)
(PL google-translate)
LUAbot003_COLLISION.zip -- Processing Project.
LUAbot[] luabot = new LUAbot[8];
public void setup()
{
size(640,640);
frameRate(20);
for(int i=0;i<luabot.length;i++)
{
luabot[i] = new LUAbot(i,80+i*60,80,30.0*i,(i%2)*127+128,((i/2)%2)*127+128,((i/4)%2)*127+128, luabot);
//luabot[i].setV(5);
}
}
public void draw()
{
scale(1,-1); //coordinate transformation
translate(0,-640);
background(255);
strokeWeight(height/200);
for(int i=0;i<luabot.length;i++)
{
luabot[i].draw();
for(int k=0;k<10;k++) //Animation ten times faster
{
luabot[i].behavior();
luabot[i].timestep();
}
}
}
Code 0-1: LUAbot003_COLLISION
public class LUAbot
{
LUAbot[] bot;
int index;
float R=20.0;
float x,y,phi; //position orientation
float v,w; //speed pixel per second, angular speed degree per second
int r,g,b; //color RED GREEN BLUE
public LUAbot(int index, float x, float y, float phi, int r, int g, int b, LUAbot[] bot)
{
this.index = index;
this.x=x;
this.y=y;
this.phi=phi;
this.r=r;
this.g=g;
this.b=b;
this.bot =bot;
v=0.0;
w=0.0;
}
int COUNTDOWN = 0;
public void behavior()
{
if( (checkCollision() || checkCollisionWall()) && COUNTDOWN==0)
{
COUNTDOWN = 20;
}
if(COUNTDOWN>0)
{
setW(5);
setV(0);
COUNTDOWN--;
}
else
{
setW(0);
setV(10);
}
}
public void setV(int v) //Pixel pro Sekunde
{
if(v>20) v=20;
if(v<-20) v=-20;
this.v = v*0.05; //Pixel per timestep
}
public void setW(int w) //Degree per second
{
if(w>20) w=20;
if(w<-20) w=-20;
this.w = w*0.05; //degree per timestep of 0,05s (1/20s)
}
public void timestep()
{
//Integration angular speed:
phi+=w;
// 0<=phi<360:
if(phi<0.0)
phi+=(1.0-floor(phi/360.0))*360.0;
phi=phi - floor(phi/360.0)*360.0;
float p = phi*PI/180.0;
//Integration translational speed:
if(!checkCollision() && !checkCollisionWall()) //if collision no translational movement
{
x+=v*cos(p);
y+=v*sin(p);
}
}
public boolean checkCollisionWall()
{
float p = phi*PI/180.0;
float vx = cos(p);
float vy = sin(p);
if(vx<0.0 && x<=R) return true;
if(vx>0.0 && x>=height-R) return true;
if(vy<0.0 && y<=R) return true;
if(vy>0.0 && y>=height-R) return true;
return false;
}
public boolean checkCollision()
{
float p = phi*PI/180.0;
float vx = cos(p);
float vy = sin(p);
for(int i=0;i<bot.length;i++)
{
if(i!=index)
{
float dist = sqrt((x-bot[i].x)*(x-bot[i].x)+(y-bot[i].y)*(y-bot[i].y));
if(dist<=2.0*R)
{
float ux = bot[i].x - x;
float uy = bot[i].y - y;
float uu = sqrt(ux*ux+uy*uy); //normation
ux/=uu;
uy/=uu;
if(vx*ux+vy*uy>=0.0)
return true;
}
}
}
return false;
}
public void draw()
{
stroke(0);
fill(r,g,b);
ellipse(x,y,R*2,R*2);
float p = phi*PI/180.0;
line(x,y,x+R*cos(p),y+R*sin(p));
}
}
Code 0-2: Tab LUAbot