Tycho Brahe
|
|
« on: April 20, 2010, 01:33:06 PM » |
|
so, I need to find the x and y distance between two points in Cartesian space, get the distance bettween the points, and the find a unit vector of the line connecting them (ie, dividing the x/y distance by the hypotenuse). at the moment I'm using this code: float xdiff = point2x-point1x; float ydiff = point2y-point1y; float hyp = sqrt((xdiff*xdiff)+(ydiff*ydiff)); xdiff /= hyp; ydiff /= hyp;
this works when point two is at (0,0) and then as soon as it moves breaks. Its such a simple problem i feel like an idiot for not being able to work it out . also, any possible solution should work when one or both or none of the x or y's of either of the points is negative (ie, any two positions) Thanks.
|
|
|
Logged
|
|
|
|
TobiasW
|
|
« Reply #1 on: April 20, 2010, 01:47:13 PM » |
|
Well, except the points being at the same position, because then you will divide by zero.
What exactly does "break" mean, and what are the positions of the two points when it "breaks"?
|
|
|
Logged
|
|
|
|
Tycho Brahe
|
|
« Reply #2 on: April 20, 2010, 02:02:23 PM » |
|
by breaks, I mean provides the wrong unit vector, ie, it doesnt point towards point 1 from point 2
|
|
|
Logged
|
|
|
|
David Pittman
|
|
« Reply #3 on: April 20, 2010, 02:09:01 PM » |
|
by breaks, I mean provides the wrong unit vector, ie, it doesnt point towards point 1 from point 2
Did you just write that backwards or is it actually supposed to be from 2 to 1? Because (point2 - point1) gives you the vector from point1 to point2.
|
|
|
Logged
|
|
|
|
nqe
|
|
« Reply #4 on: April 20, 2010, 05:01:01 PM » |
|
Since you are looking for a unit vector, you need to do u/||u|| which is the first part of your code. But since it's possible that the center of your plane is not at the start point of the vector as assumed by the above, then you need to add that manually to take it into account. so unit vector would be gotten by float xdiff = point2x-point1x; float ydiff = point2y-point1y; float hyp = sqrt((xdiff*xdiff)+(ydiff*ydiff)); xdiff /= hyp; ydiff /= hyp; xdiff += point1x; ydiff += point1y;
Unless in the likely event that I'm completely wrong
|
|
|
Logged
|
|
|
|
st33d
Guest
|
|
« Reply #5 on: April 20, 2010, 06:07:53 PM » |
|
just sayin... float xdiff = point2x-point1x; float ydiff = point2y-point1y; float hyp = sqrt((xdiff*xdiff)+(ydiff*ydiff)); if(hyp > 0){ xdiff /= hyp; ydiff /= hyp; } else { xdiff = 0; ydiff = 0; } xdiff += point1x; ydiff += point1y;
you shouldn't really be checking for any bugs without ruling out the blatantly obvious
|
|
|
Logged
|
|
|
|
nqe
|
|
« Reply #6 on: April 20, 2010, 11:29:25 PM » |
|
Except that you might rather want to denote it as an error since just setting xdiff and ydiff to 0 would not get you a unit vector.
|
|
|
Logged
|
|
|
|
Draknek
Level 6
"Alan Hazelden" for short
|
|
« Reply #7 on: April 21, 2010, 02:42:08 AM » |
|
As far as I can see, your original code is fine. It should give you a unit vector from point 1 to point 2 (barring the degenerate case of two identical points). xdiff += point1x; ydiff += point1y; You do not want this; it will give you a position not a unit vector.
|
|
|
Logged
|
|
|
|
st33d
Guest
|
|
« Reply #8 on: April 21, 2010, 04:20:06 AM » |
|
Except that you might rather want to denote it as an error since just setting xdiff and ydiff to 0 would not get you a unit vector.
It's entirely possible that two points may have identical coordinates. But yeah, this code is kind of backwards anyway. I usually normalise and then multiply the unit vector by the speed, then add it to the position. Hence if the unit vector came back 0,0 there would be no movement - target acquired.
|
|
|
Logged
|
|
|
|
Tycho Brahe
|
|
« Reply #9 on: April 21, 2010, 11:28:08 AM » |
|
thanks for all you're help, from what I've got from all your replies it should work, but doesnt. if more information would help, it only fails when point 2 is NOT at zero. anyway, thanks, I'll try some other things to see if it fixes it.
|
|
|
Logged
|
|
|
|
TobiasW
|
|
« Reply #10 on: April 21, 2010, 11:38:51 AM » |
|
Time for brute force debug output: float xdiff = point2x-point1x; float ydiff = point2y-point1y;
std::cout << "point1x: " << point1x << std::endl; std::cout << "point2x: " << point2x << std::endl; std::cout << "point1y: " << point1y << std::endl; std::cout << "point2y: " << point2y << std::endl; std::cout << "xdiff: " << xdiff << std::endl; std::cout << "ydiff: " << ydiff << std::endl;
float hyp = sqrt((xdiff*xdiff)+(ydiff*ydiff)); xdiff /= hyp; ydiff /= hyp;
std::cout << "hyp: " << hyp << std::endl; std::cout << "xdiff/hyp: " << xdiff << std::endl; std::cout << "ydiff/hyp: " << ydiff << std::endl; Could you please post the output for the "broken" call here?
|
|
|
Logged
|
|
|
|
Tycho Brahe
|
|
« Reply #11 on: April 21, 2010, 11:49:30 AM » |
|
FOUND IT!!!!! sorry for my elation, I've finally fixed it, I've found the problem.
basically because of how I draw stuff using opengl one of the points (point 2) was in fact negative when it was actually positive and vice versa, in both the x and y axis. I've fixed it by actually ADDING the location of the two points to find the vector between them.
thanks for all your help guys, it was actually a problem with the computing side of it, not the theory. YAY!
|
|
|
Logged
|
|
|
|
TobiasW
|
|
« Reply #12 on: April 21, 2010, 11:54:16 AM » |
|
I don't exactly get the WHY, but: Gratulation!
|
|
|
Logged
|
|
|
|
David Pittman
|
|
« Reply #13 on: April 21, 2010, 04:41:00 PM » |
|
You're adding vectors to get their difference? Math doesn't work that way.
|
|
|
Logged
|
|
|
|
Zaphos
Guest
|
|
« Reply #14 on: April 23, 2010, 11:15:27 AM » |
|
I'm glad it's working, but, for your own sanity I suggest putting the points in the same coordinate system ...
|
|
|
Logged
|
|
|
|
Tycho Brahe
|
|
« Reply #15 on: April 23, 2010, 11:20:37 AM » |
|
yeah, I suspect its a problem with everything I do with opengl, you see, when I draw something like the player or an enemy I use:
glTranslate(-x,-y,-z); //then any drawing code glTranslate(x,y,z);
wheras, now I'm thinking I should do:
glTranslate(x,y,z); //drawing code glTranslate(-x,-y,-z);
|
|
|
Logged
|
|
|
|
nqe
|
|
« Reply #16 on: April 23, 2010, 11:41:50 AM » |
|
Yes, you're translating it in the reverse order. As far as I can see, your original code is fine. It should give you a unit vector from point 1 to point 2 (barring the degenerate case of two identical points). xdiff += point1x; ydiff += point1y; You do not want this; it will give you a position not a unit vector. What I meant by the above code is what the glTranslate() function does, that is set the origin of the cartesian plane in the right place.
|
|
|
Logged
|
|
|
|
Zaphos
Guest
|
|
« Reply #17 on: April 23, 2010, 12:45:44 PM » |
|
Why do you manually undo the translation instead of using glPushMatrix and glPopMatrix? Is it an efficiency thing?
|
|
|
Logged
|
|
|
|
raigan
|
|
« Reply #18 on: April 23, 2010, 01:14:12 PM » |
|
You're adding vectors to get their difference? Math doesn't work that way. “A correct graphics program has an even number of sign errors...” -Jim Blinn.
|
|
|
Logged
|
|
|
|
Tycho Brahe
|
|
« Reply #19 on: April 23, 2010, 01:19:20 PM » |
|
Why do you manually undo the translation instead of using glPushMatrix and glPopMatrix? Is it an efficiency thing?
ummmm. no reason I guess, I suppose its what I've learned. It would be interesting to find out whats faster though, my moneys on pushmatrix/popmatrix, as (I assume) it wouldn't have to recalculate the view matrix.
|
|
|
Logged
|
|
|
|
|