Skip to content

Instantly share code, notes, and snippets.

@ivanseidel
Created April 17, 2015 21:10
Show Gist options
  • Save ivanseidel/a9b3e0014da7ce18d2a7 to your computer and use it in GitHub Desktop.
Save ivanseidel/a9b3e0014da7ce18d2a7 to your computer and use it in GitHub Desktop.
Processing Robot Hit Simulator
interface QuandoPrecisaExecutar{
public void executar(Robo eeew);
}
// http://bit.ly/1FSZBkP
class Robo{
Vetor position;
Vetor direction;
Robo(double x, double y){
position = new Vetor(x, y);
direction = fromTheta(0);
}
public void mover(double passos){
position = position.soma(direction.multiplicar(passos));
}
public void girar(double angulo){
direction = direction.rotate(Math.toRadians(angulo));
}
public void draw(){
fill(0, 255, 0);
ellipse((float)position.x, (float)position.y, 20, 20);
double pX = position.x;
double pY = position.y;
double dX = direction.x;
double dY = direction.y;
line( (float)pX, (float)pY,
(float)(pX + dX*20), (float)(pY + dY*20));
}
public boolean seVaiSair(double distancia){
Vetor pFinal = position.soma(direction.multiplicar(distancia));
if( pFinal.x < 0 || pFinal.y < 0 ||
pFinal.x > width || pFinal.y > height)
return true;
return false;
}
public QuandoPrecisaExecutar quandoExecutar;
public void executar(){
if(quandoExecutar != null)
quandoExecutar.executar(this);
}
}
ArrayList<Robo> robos = new ArrayList<Robo>();
boolean seVaiChocar(double d, Robo r){
Vetor pFinal = r.position.soma(r.direction.multiplicar(d));
for(Robo p:robos){
if(p == r)
continue;
if(pFinal.subtrair(p.position).size() <= 20)
return true;
}
return false;
}
void setup(){
size(700, 300);
QuandoPrecisaExecutar andadorFrente = new QuandoPrecisaExecutar(){
public void executar(Robo r){
if(seVaiChocar(4, r)){
r.girar(180);
}if(r.seVaiSair(1 + 10)){
r.girar(5);
}else{
r.mover(4);
r.girar(Math.random()*10 - 5);
}
}
};
QuandoPrecisaExecutar giradorAntihorario = new QuandoPrecisaExecutar(){
public void executar(Robo r){
r.mover(1);
r.girar(-10);
}
};
// for(int i = 0; i < 10; i++){
// Robo c = new Robo(i*30, 150);
// c.quandoExecutar = giradorAntihorario;
//
// robos.add(c);
// }
for(int i = 0; i < 50; i++){
Robo c = new Robo(i*30, 150);
c.quandoExecutar = andadorFrente;
robos.add(c);
}
}
void draw(){
background(255);
for(int i = 0; i < robos.size(); i++){
robos.get(i).executar();
}
for(int i = 0; i < robos.size(); i++){
robos.get(i).draw();
}
}
public class Vetor {
public double x = 0;
public double y = 0;
Vetor(double x, double y){
this.x = x;
this.y = y;
}
Vetor(){};
public double size(){
return Math.sqrt(x*x + y*y);
}
public Vetor soma(double x2, double y2){
return new Vetor(x + x2, y + y2);
}
public Vetor soma(Vetor v){
return new Vetor(x + v.x, y + v.y);
}
public Vetor subtrair(Vetor v){
return new Vetor(x - v.x, y - v.y);
}
public Vetor multiplicar(double e){
return new Vetor(x * e, y * e);
}
public Vetor dividir(double e){
if(e == 0)
return new Vetor();
return new Vetor(x / e, y / e);
}
// Creates a new vector from a Theta value (Radians is default)
public Vetor rotate(double theta){
double currentTheta = Math.atan2(this.y, this.x);
double currentNorm = Math.sqrt(x*x + y*y);
// Creates vector with new Angle
Vetor finalVector = fromTheta(currentTheta + theta);
// Un-normalize vector
return finalVector.multiplicar(currentNorm);
}
public void imprimir(){
System.out.printf("[%.2f,%.2f]",x,y);
}
}
Vetor fromTheta(double theta){
double x = Math.cos(theta);
double y = Math.sin(theta);
return new Vetor(x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment