st33d wrote on Feb 9th, 2007, 6:21pm:I'm giving my Boid a heading to follow and I want it to fly at a constant speed in that direction. How can I turn the code in the block if(mousepressed){} into a, "engines at half impulse towards the mouse pointer"
Try thinking in vector terms instead of trig terms.
Your "heading", defined at mouseX-this.x,mouseY-this.y is a vector. Problem is, it isn't normalized (that is, it's not on the unit circle). To normalize, divide by its magnitude (that is, its length).
In rough off top of head psuedocode:
vx = mouseX - this.x;
vy = mouseY - this.y;
mag = sqrt(vx*vx+vy*vy);
vx /= mag;
vy /= mag;
At that point, you'll have a unit velocity vector in the desired direction. To move at a given speed, say 4 units per frame, simply multiply your vector by desired speed and add to position:
speed = 4;
x += vx * speed;
y += vy * speed;
Alternatively, if you NEED to work in trig terms, continue calculating theta with atan2() and just use sin/cos on theta to reconstruct vx/vy whenever needed:
x += cos(theta) * speed; // cos(theta) == vx normalized
y += sin(theta) * speed; // sin(theta) == vy normalized
The methods are equivalent, but the vector method has performance advantages (a lot less trig calls).