I've tried all day to figure out this problem by projecting vectors on to each other but I can't figure it out.
The problem is that I have two lines. One follows the mouse, the other follows the mouse relative to where it started - like a small fan.
I figured it out using trigonometry but I can't figure it out using just vector math (which I would like to - because I really need the speed).
Here's my cut down code (I've left some vector math stuff in):
Code:
Line mouse;
Line relative;
float angleDifference;
void setup(){
size(200, 200);
mouse = new Line(new Point(100,100), new Point(mouseX, mouseY));
relative = new Line(mouse.a, new Point(100,0));
// Here's how I figure out the angle my other line is away from the mouse
angleDifference = relative.getTheta() - mouse.getTheta();
}
void draw(){
background(255);
stroke(0);
line(mouse.a.x, mouse.a.y, mouse.b.x, mouse.b.y);
stroke(255,0,0);
line(relative.a.x, relative.a.y, relative.b.x, relative.b.y);
mouse.b = new Point(mouseX, mouseY);
mouse.updateLine();
// And I just use trig to force the other line to be at the right angle away from the mouse
float theta = angleDifference + mouse.getTheta();
relative.b.x = relative.a.x + cos(theta) * relative.len;
relative.b.y = relative.a.y + sin(theta) * relative.len;
relative.updateLine();
}
static class Line{
Point a, b;
float vx, vy, dx, dy, lx, ly, rx, ry, len, invLen, theta;
// Constructor
Line(Point a, Point b){
this.a = a;
this.b = b;
updateLine();
}
// Refresh values of normals
void updateLine(){
vx = b.x - a.x;
vy = b.y - a.y;
len = sqrt(vx * vx + vy * vy);
if(len > 0){
dx = vx/len;
dy = vy/len;
} else {
dx = 0.0f;
dy = 0.0f;
}
rx = -dy;
ry = dx;
lx = dy;
ly = -dx;
}
// Get the angle of the line
float getTheta(){
theta = atan2(vy, vx);
return theta;
}
// Return the dot product of two vectors (lines)
static float dot(Line a, Line b){
return a.vx * b.vx + a.vy * b.vy;
}
}
static class Point{
float x, y;
// Constructor
Point(float x, float y){
this.x = x;
this.y = y;
}
}
So there's how you do it with trig - but does anyone know how to do it with out trig? Please?