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));
}
}