math & physics
c_er at October 24th, 2005 09:22 — #1
I need to create a path for an object walk. Sample: I have 2 lines:
L1: x1 = 100, y1 = 50, x2 = 200 , y2= 250
L2: x1 = 200 , y1= 250, x2 = 600, y2 = 500
What I want: the object walks in the L1 and in the end of the line, it in the L2.
I have any idea to start. Someone can send some tips, tutorials or sites when I can to study this?
I think that I'll use sin and cos, but I think, I'm not sure about this.]
Thanks for the support
nomadrock at October 24th, 2005 10:34 — #2
You just use linear interpolation. Between any two points you can make a parametric equation of the line, that means that the equation lets you take in a new parameter for the distance along the line and it spits out a new point.
ok, now suppose we have two points A and B and we create a new point C that is along that line
C.x = A.x + t * (B.x - A.x)
C.y = A.y + t * (B.y - A.y)
When t is zero, C will be the same as A, when t is one, C will be the same as B. When t is 0.5 then C will be half way between A and B. So if you want to walk between A and B in 100 steps, then you just increment t by 0.01 each step.
Of interesting note, the equations above are valid when t is less than 0 or greater than 1. The points will still be along the line, but will be further out past B if it is greater than 1, or past A in the opposite direction if t is less than 0.
Now if you wanted to walk between them at a certain speed, then you first need to figure out the distance.
distance = sqrt( (A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y))
So if you want to travel your line between points A and B at a speed of 10 units per time step, you would increment t by (10 / distance). Keep in mind you need to check to see if A and B are the same point, so you dont divide by zero.
irishfarmer at October 27th, 2005 22:43 — #3
Its too bad this formula only works well with 3d in mind. Because if I try to translate all of those numbers through windows pixels there are some mildly innaccurate results. Ah well.
nomadrock at October 28th, 2005 00:46 — #4
You just do your normal transformation like any other point in 3d space.
EDIT: wow, I completely missread you IrishFarmer. Reedbeta has the correct interpretation below
reedbeta at October 28th, 2005 01:59 — #5
The inaccuracy is due to measuring everything in integer pixel values. If you keep everything in floating-point coordinates and convert them to integer only when it's time to draw the object, you shouldn't have any problems with accuracy.