 mersenneforum.org Moving a point from A to B
 Register FAQ Search Today's Posts Mark Forums Read  2007-08-23, 21:50 #1 kuratkull   Mar 2007 Estonia 2·67 Posts Moving a point from A to B Hey, I have a problem. I wrote simple simulation in python where 2 different coloured dots(2 armies) find their closest enemy and start moving towards it. But theres a problem with the moving algorythm: A(x,y) B(x, y) # the locations are in pixels My current method is by constructing a triangle between the two points: Ay - By = absolute(yy) Ax - Bx = absolute(xx) yy/xx = y_x formula for moving: y = y_x*x So, to get to the target, it moves y pixels for every x pixels. The x value is static, and it moves correctly in x dimesion, but for example, when the two pixels are almost in a vertical position, the "triangle" becomes very narrow, and it jumps several hundred pixels at a time for every x. The closer the x values are the larger the y value gets. I'm no mathematician(as you can see from my wording)[I'm 18], so I would appreciate if comments on my wording would be left out. Anyway, can anyone suggest a better method for moving. Desired effect: for example, if two points are vertically aligned, and x is 5, then y should be around the same size, but still moving towards the target. Pseudo-codes and pseudo-formulas would be good too. Big thanks in advance :) kuratkull Last fiddled with by kuratkull on 2007-08-23 at 21:51   2007-08-23, 22:17 #2 VolMike   Jun 2007 Moscow,Russia 7·19 Posts What exact problem with moving algorithm? You suppose that first point moves to second on straight line with some constant speed V and you want to define the position of first point after some time t of moving? Last fiddled with by VolMike on 2007-08-23 at 22:23   2007-08-23, 22:46   #3
kuratkull

Mar 2007
Estonia

2·67 Posts Quote:
 Originally Posted by VolMike What exact problem with moving algorithm? You suppose that first point moves to second on straight line with some constant speed V and you want to define the position of first point after some time t of moving?
With every round/loop the program moves x by 5 pixels and the according value of y on a straight line towards the point b.
The way I'm using it now: it moves "x" by 5 pixels every round, and y is dependant on the y difference of the two points.

The catch is that both points move. In the beginning, when team1 is on the left, and team2 is on the right edge of the screen, the movement is almost normal.(x moves 5 and y is not so different). Then[when they meet in the centre of the screen] they start "killing" eachother, and in the end, when only 2 dots remain, and if they happen to have a very large difference in their y values and their x values are close, they still move 5 "x" values towards eachother, but the y value is huge(100-200pxl).

I don't know if this makes any sense to you.

I would need a way to make all the points move in the exact same speed[in the same amount of time], not depending on their relative location to eachother.

Last fiddled with by kuratkull on 2007-08-23 at 22:47 Reason: details, details   2007-08-23, 23:05 #4 VolMike   Jun 2007 Moscow,Russia 2058 Posts It is better for you to make a paint of points with some first steps they do. If you speak Russian, we can discuss it more neatly.   2007-08-23, 23:13 #5 VolMike   Jun 2007 Moscow,Russia 7×19 Posts As I see in each loop you increase (decrease for second point) value x to 5 pxl and you must calculate y-displacement to stay your points on straight line (A-B). So one of your's aims is to find y-displacement. Am I right?   2007-08-23, 23:26   #6
kuratkull

Mar 2007
Estonia

2·67 Posts Quote:
 Originally Posted by VolMike As I see in each loop you increase (decrease for second point) value x to 5 pxl and you must calculate y-displacement to stay your points on straight line (A-B). So one of your's aims is to find y-displacement. Am I right?
Exactly(although this is a very simple version of this, actually the points move each round, but that doesn't change the principle).
But the method I'm using atm gets the two points together(so, it's in 50% working condition), but as I said before, the y value changes dramatically depending on the locations.

R He ckazal no pyccku :)

Last fiddled with by kuratkull on 2007-08-23 at 23:29   2007-08-23, 23:52 #7 VolMike   Jun 2007 Moscow,Russia 8516 Posts Mogesh pisat v translite ili v russkoy raskladke :) Yes, the main idea is to find new Y coordination for one point after X=X+5 . F.e. A = (xA,yA),B = (xB,yB), xA < xB (if xA = xB - I think easy). First, you should find en equation of line AB- a and b parameters which satisfy Code:  {a*xA+b==yA and a*xB+b==yB} The result is Code:  {a=(-yA + yB)/(xA - xB),b=-(xB yA - xA yB)/(xA - xB)} Thus, any point Z on AB with (xZ,yZ) coordinates must satisfy Code:  yZ =xZ*a+b =xZ*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB) On first step of point A you have xANew1 = xA+5 and you can find yANew1: Code:  yANew1 =xANew1*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB)= (xA+5)*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB) On step k of A xANewk=xA+5*k and Code:  yANewk=(xA+5*k)*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB) The same idea with point B but xBNewk=xB-5*k and Code:  yBNewk=(xB-5*k)*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB) To, 4to doktor propisal? :) Last fiddled with by VolMike on 2007-08-23 at 23:58   2007-08-24, 00:36 #8 kuratkull   Mar 2007 Estonia 2·67 Posts Я сказал вас, я не говорю русского. "я не говорю русского" был от памяти, и этот текст путем использование babelfish:) Code: def move_units(d_1,d_2): yA = d_1 xA = d_1 yB = d_2 xB = d_2 if xA <= xB: ynew = xA+size*(-yA + yB)/(xA - xB)-(xB * yA - xA * yB)/(xA - xB) #= (xA+5)*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB) return [d_1+size, ynew] elif xA >= xB: ynew = xA-size*(-yA + yB)/(xA - xB)-(xB * yA - xA * yB)/(xA - xB) #= (xA+5)*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB) return [d_1-size, ynew] The code should be understandable...but it isn't working: The first line is for point A , second for B, third for A and so on. I'm using the first line as an exampe of output locations: BTW: size is 2 in my prog A_before[190, 200] B[390, 200] A_now 188(x) 782(y) [390, 200] [192, 200] 388 1732 [192, 200] [388, 200] 190 34 [388, 200] [194, 200] 386 -1258 [194, 200] [386, 200] 192 1547 [386, 200] [196, 200] 384 4798 [196, 200] [384, 200] 194 -1681 [384, 200] [198, 200] 382 -8124 [198, 200] [382, 200] 196 5521 [382, 200] [200, 200] 380 20749 [200, 200] [380, 200] 198 -11369 [380, 200] [202, 200] 378 -47076 I also wanted to say that I am immensely thankful for your help until now :) Last fiddled with by kuratkull on 2007-08-24 at 00:38   2007-08-24, 03:07   #9
wblipp

"William"
May 2003
New Haven

2×32×131 Posts Quote:
 Originally Posted by kuratkull I would need a way to make all the points move in the exact same speed[in the same amount of time], not depending on their relative location to each other.
Make the sum of the x and y values for each move be approximately 7.

Do this by calculating the delta_x and delta_y like you do now. Then calculate new values as

new_delta_x = round(delta_x * 7.0 / (abs(delta_x) + abs(delta_y))

new_delta_y = round(delta_y * 7.0 / (abs(delta_x) + abs(delta_y))   2007-08-24, 08:26   #10
VolMike

Jun 2007
Moscow,Russia

7·19 Posts Quote:
 Originally Posted by kuratkull Я сказал вас, я не говорю русского. "я не говорю русского" был от памяти, и этот текст путем использование babelfish:) Code: def move_units(d_1,d_2): yA = d_1 xA = d_1 yB = d_2 xB = d_2 if xA <= xB: ynew = xA+size*(-yA + yB)/(xA - xB)-(xB * yA - xA * yB)/(xA - xB) #= (xA+5)*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB) return [d_1+size, ynew] elif xA >= xB: ynew = xA-size*(-yA + yB)/(xA - xB)-(xB * yA - xA * yB)/(xA - xB) #= (xA+5)*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB) return [d_1-size, ynew] The code should be understandable...but it isn't working: The first line is for point A , second for B, third for A and so on. I'm using the first line as an exampe of output locations: BTW: size is 2 in my prog A_before[190, 200] B[390, 200] A_now 188(x) 782(y) [390, 200] [192, 200] 388 1732 [192, 200] [388, 200] 190 34 [388, 200] [194, 200] 386 -1258 [194, 200] [386, 200] 192 1547 [386, 200] [196, 200] 384 4798 [196, 200] [384, 200] 194 -1681 [384, 200] [198, 200] 382 -8124 [198, 200] [382, 200] 196 5521 [382, 200] [200, 200] 380 20749 [200, 200] [380, 200] 198 -11369 [380, 200] [202, 200] 378 -47076 I also wanted to say that I am immensely thankful for your help until now :)
Having a look-see I found one mistake.Your commentary (after # symbol I think) code is correct but calculation formula is not. You forgot to put (xA+(-)size) in parentheses. New code:
Code:
def move_units(d_1,d_2):
yA = d_1
xA = d_1
yB = d_2
xB = d_2

if xA <= xB:
ynew = (xA+size)*(-yA + yB)/(xA - xB)-(xB * yA - xA * yB)/(xA - xB) #= (xA+5)*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB)
return [d_1+size, ynew]

elif xA >= xB:
ynew = (xA-size)*(-yA + yB)/(xA - xB)-(xB * yA - xA * yB)/(xA - xB) #= (xA+5)*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB)
ynew = (xA-size)*(-yA + yB)/(xA - xB)-(xB * yA - xA * yB)/(xA - xB) #= (xA+5)*(-yA + yB)/(xA - xB)-(xB yA - xA yB)/(xA - xB)
return [d_1-size, ynew]
Also recheck the sequence of logic steps in you program (you duplicate string for evaluating ynew in second condition).

Last fiddled with by VolMike on 2007-08-24 at 08:31   2007-08-24, 12:15 #11 kuratkull   Mar 2007 Estonia 8616 Posts It still doesn't work Mike: Code: def move(d_1,d_2): try: yA = d_1 xA = d_1 yB = d_2 xB = d_2 if xA <= xB: ynew = (xA+size)*(-yA + yB)/(xA - xB)-(xB * yA - xA * yB)/(xA - xB) return [xA+size, ynew] elif xA >= xB: ynew = (xA-size)*(-yA + yB)/(xA - xB)-(xB * yA - xA * yB)/(xA - xB) return [xA-size, ynew] left or right, the A coord, B coor, new A coord, the change in y l [138, 100] [400, 203] 143 89 -11 r [400, 203] [143, -11] 395 -256 -459 l [143, -11] [395, -459] 148 496 507 r [395, -459] [148, 507] 390 2152 2611 l [148, 507] [390, 2611] 153 -1603 -2110 r [390, 2611] [153, -2110] 385 -10216 -12827 l [153, -2110] [385, -12827] 158 10146 12256 r [385, -12827] [158, 12256] 380 58877 71704 l [158, 12256] [380, 71704] 163 -61446 -73702 The Delta fuction? Google didn't answer me :( Last fiddled with by kuratkull on 2007-08-24 at 12:23   Thread Tools Show Printable Version Email this Page Similar Threads Thread Thread Starter Forum Replies Last Post ewmayer Mlucas 13 2018-04-05 21:16 flashjh Lounge 23 2013-03-05 15:28 Primeinator Information & Answers 1 2010-06-17 03:07 crash893 PrimeNet 14 2008-10-01 01:45 Unregistered PrimeNet 2 2004-09-01 22:39

All times are UTC. The time now is 14:27.

Sat Jul 4 14:27:03 UTC 2020 up 101 days, 12 hrs, 2 users, load averages: 2.72, 2.78, 2.64