ok wow im really sorry guys, first with the exemple i give you and with yours solution its work.
but i dont give you the right exemple i forgot i have changed my wandering behavior.
this is my wandering now(i give you the all code) :
Vehicle wanderer;
boolean debug = true;
void setup() {
size(640,360);
wanderer = new Vehicle(width/2,height/2);
}
void draw() {
background(255);
wanderer.wander();
wanderer.run();
}
void mousePressed() {
debug = !debug;
}
class Vehicle {
PVector position;
PVector velocity;
PVector acceleration;
float r;
float wandertheta;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
Vehicle(float x, float y) {
acceleration = new PVector(0,0);
velocity = new PVector(0,0);
position = new PVector(x,y);
r = 6;
wandertheta = 0;
maxspeed = 2;
maxforce = 0.05;
}
void run() {
update();
borders();
display();
}
// Method to update position
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
position.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
}
void wander() {
float wanderR = 25; // Radius for our "wander circle"
float wanderD = 80; // Distance for our "wander circle"
float change = 0.3;
wandertheta += random(-change,change); // Randomly change wander theta
// Now we have to calculate the new position to steer towards on the wander circle
PVector circlepos = velocity.get(); // Start with velocity
circlepos.normalize(); // Normalize to get heading
circlepos.mult(wanderD); // Multiply by distance
circlepos.add(position); // Make it relative to boid's position
float h = velocity.heading2D(); // We need to know the heading to offset wandertheta
PVector circleOffSet = new PVector(wanderR*cos(wandertheta+h),wanderR*sin(wandertheta+h));
PVector target = PVector.add(circlepos,circleOffSet);
seek(target);
// Render wandering circle, etc.
if (debug) drawWanderStuff(position,circlepos,target,wanderR);
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
void runaway(){
PVector desired = null;
PVector mouse = new PVector(mouseX, mouseY);
int distance = 150;
float dist = PVector.dist(mouse, position);
if(dist < distance){
desired = new PVector(position.x - mouseX, position.y - mouseY);
}
if(desired != null){
desired.normalize();
desired.mult(maxspeed);
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce);
applyForce(steer);
}
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
void seek(PVector target) {
PVector desired = PVector.sub(target,position); // A vector pointing from the position to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Steering = Desired minus Velocity
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce); // Limit to maximum steering force
applyForce(steer);
}
void display() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
fill(127);
stroke(0);
pushMatrix();
translate(position.x,position.y);
rotate(theta);
beginShape(TRIANGLES);
vertex(0, -r*2);
vertex(-r, r*2);
vertex(r, r*2);
endShape();
popMatrix();
}
// Wraparound
void borders() {
if (position.x < -r) position.x = width+r;
if (position.y < -r) position.y = height+r;
if (position.x > width+r) position.x = -r;
if (position.y > height+r) position.y = -r;
}
}
// A method just to draw the circle associated with wandering
void drawWanderStuff(PVector position, PVector circle, PVector target, float rad) {
stroke(0);
noFill();
ellipseMode(CENTER);
ellipse(circle.x,circle.y,rad*2,rad*2);
ellipse(target.x,target.y,4,4);
line(position.x,position.y,circle.x,circle.y);
line(circle.x,circle.y,target.x,target.y);
}
of course my runaway behavior dont work with that.
im really sorry i feel like i have wasted your time.
i gonna search alone, if i cant find a solution i will come for help.
thanks for your help guys :).