Sunday, April 1, 2007

I win.

I've been working hard at solving this damned trigonometry problem thats been troubling me so much, just the other day Pete put together a simple 2D Swing Java app that followed the mouse around the screen and output the angle, it is what I was looking for, but the method he used is a LOT different to how I've got it working in OpenGL and the coordinate system. (Below is Pete's Example).

But sure enough, using a combination of both of our code I finally got it working as expect! Now I've got the vector following the mouse around the screen and pointing in the right direction, another hurdle down, I'm sure their will be a lot more hurdles to come, but hey, thats what studio's is all about isn't it? Learning new things and pushing boundaries?

How did I do it? Well it was actually pretty simple, as I suggested it would be in a previous blog. I had been trying this type of algorithm for a while but just wasn't getting anywhere with it until I picked some bits and pieces out of Pete's code (Look below for the code).

public void mouseMoved(MouseEvent e) {
double deltaX = e.getX() - 250.0;
double deltaY = (500 - e.getY()) - 250.0;
v.x = deltaX;
v.y = deltaY;

Whats happening is when the mouse is moved across the screen, its retrieving the X and Y position of the mouse and subtracting the start position (250.0) which is in the center of the screen. Then the vectors X and Y is set to the delta X and Y to give it its direction.


No comments: