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 & HelpSyntax Questions › Vector Maths problem
Page Index Toggle Pages: 1
Vector Maths problem (Read 313 times)
Vector Maths problem
Oct 11th, 2008, 5:22pm
 
Hi,

I'm having difficulty thinking through the maths behind a simple vector-based animation I'm trying to get working.

Basically, I want an object I'm moving x+1, y+1 every frame to change direction so that it is moving towards mouseX, mouseY when the mouse button is pressed.  I've been fumbling around with pythagoras and SOHCAHTOA but am really just shooting in the dark - my maths is very rusty.  Ive posted my code below this message.

If anyone can suggest any reading material to clear the fog for me I'd really appreciate it.  Or if someones got an out-and-out solution then that would be swell to.

Many thanks

Code:


/* Vector Motion Main */

import processing.opengl.*;
import java.util.*;

Circle myCircle = new Circle();

void setup() {
size(500, 500, OPENGL);
myCircle.init();
smooth();
}

void draw() {
background(255, 255, 255);
myCircle.update();
myCircle.render();
}

void mousePressed() {
// Move in the direction of the mouse click
/* Changes the objects direction seemingly randomly - doesn't work.
float hypotenuse = sqrt(sq(myCircle.xPos - mouseX) + sq(myCircle.yPos - mouseY));
myCircle.circleVector[0] = (sin(hypotenuse));
myCircle.circleVector[1] = (cos(hypotenuse));
*/
}



Code:


/* Circle Class */

class Circle {

public float[] circleVector = { 1, 1 };
int circleRadius = 10;
float xPos = 0;
float yPos = 0;
float currentxPos = 0;
float currentyPos = 0;

public void Circle() {
}

public void init() {
}

public void update() {
xPos = xPos + circleVector[0];
yPos = yPos + circleVector[1];
}

public void render() {
fill(10, 10, 10, 150);
noStroke();
ellipse(xPos, yPos, circleRadius, circleRadius);
stroke(0);
line (xPos, yPos, xPos + (circleVector[0]*50), yPos + (circleVector[1]*50));
}

}

Re: Vector Maths problem
Reply #1 - Oct 11th, 2008, 7:28pm
 
Hi.
The hypotenuse is the longest side of a triangle. what you are calculating there is actually the distance between your circle and the mouse.
what you want is the angle between them. this can be calculated by using the atan2 function.

just change this in your code:
Code:

void mousePressed() {
 // Move in the direction of the mouse click
//  Changes the objects direction seemingly randomly - doesn't work.
 float angle = atan2(mouseY - myCircle.yPos, mouseX - myCircle.xPos);
 myCircle.circleVector[0] = (cos(angle));
 myCircle.circleVector[1] = (sin(angle));
 
}
Re: Vector Maths problem
Reply #2 - Oct 11th, 2008, 10:34pm
 
Absolute Legend, thanks very much.
Page Index Toggle Pages: 1