We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Doing angles with vector math
Page Index Toggle Pages: 1
Doing angles with vector math (Read 763 times)
Doing angles with vector math
Aug 6th, 2007, 9:45pm
 
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?
Re: Doing angles with vector math
Reply #1 - Aug 6th, 2007, 10:28pm
 
What about using translate/rotate?

Code:

background(0);
stroke(255);
translate(width/2,height/2);
line(0,0,mouseX-width/2,mouseY-height/2);
rotate(-PI/4);
line(0,0,mouseX-width/2,mouseY-height/2);


Is this what you mean? (sorry if i misunderstood)
Re: Doing angles with vector math
Reply #2 - Aug 7th, 2007, 10:48am
 
What I'm actually doing is a little submarine graphic that turns to face the mouse. When the submarine beaches itself it uses a spring box to simulate collision and to roll around.

Now I can align the graphic with the central axis of the spring box but it then it weirdly snaps to that angle leaving the water.

What I need is for the sub to leave the water - lock to that angle, and then when the spring box starts to turn from rolling on the ground the sub starts to turn.


Now I've got it working with trig, but I was just wondering if there was some way this trick could be done with vector math.
Re: Doing angles with vector math
Reply #3 - Aug 7th, 2007, 11:57am
 
If you have a vector, and an angle you want to rotate it by, then I think you have to use trig, though you can do it with only a single sin and cos if the rotatino is around one of the 3 primary axes by using matrix multiplication.

If you had vectors and you wanted to "copy" the rotation between them (e.g. you have vectors A and B, find the angle between them, and then rotate C by that angle), then you can avoid trig I believe.
Re: Doing angles with vector math
Reply #4 - Aug 8th, 2007, 11:12am
 
Nah, what I was looking for was a way of not having to find out the angle in the first place. I was hoping there would be a way to map a vector on top of another normal.

I've tried using a Matrix, but it's being weird about it what with the center point I need to spin round being off somewhere in space. I try translating to that point and rotating from there and apply the transformation and still no dice.

Bah. Sad
Re: Doing angles with vector math
Reply #5 - Aug 8th, 2007, 6:32pm
 
It's not entirely clear to me what you're trying to accomplish.  I run the sketch and get a black line from the origin to the mouse, and a red line from the origin to a point about 30 degrees behind the black line at a fixed radius.

Is that all it needs to accomplish?  If so, yes, you could do it solely with vectors.  Let's call your red vector R, and black vector B.  Normalize B, extend a new vector M perpendicular off the endpoint of B, of magnitude sin(desired_span_angle) (define as a constant) - the vector from the endpoint of M to the origin is the new R (interpreted geometrically, it's the hypotenuse of the triangle formed by origin-B-M), normalize and scale by desired radius and you're done.
Page Index Toggle Pages: 1