HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Moving units by vectors

07-02-2009, 10:41 AM#1
wraithseeker
So I have vector A which is x y z of initial unit position available and vector B x y z of final unit position available, how do I move them?
07-02-2009, 10:53 AM#2
Blubb-Tec
how about SetUnitX/Y? and for Z the usual flying height trick..
07-02-2009, 11:02 AM#3
wraithseeker
The problem is how to do the calculations, not the function calls.
07-02-2009, 11:43 AM#4
moyack
parametric functions for each of the XYZ components respect time.
07-02-2009, 11:50 AM#5
wraithseeker
Collapse JASS:
// initial vector
set d.zx = GetUnitX(target) * Cos(angle)
        set d.zy = GetUnitY(target) * Sin(angle)
        call MoveLocation(L,d.x,d.y)
        set d.zz = GetLocationZ(L) // I am not using Z axis for now, ignore it same for the rest
Collapse JASS:
// speed vector

 set d.x = speed * Cos(angle) * TIME
        set d.y = speed * Sin(angle) * TIME
        set d.z = 0 * TIME

Collapse JASS:
//decrement vector

  set d.dx = decrement * Cos(angle) * TIME
        set d.dy = decrement * Sin(angle) * TIME
        set d.z = 0 * TIME

I am new to vectors so don't blame me for doing stupid stuff, this is deacclerating and not accelerating. They start from a variable of x speed and then x decrement and end at 0 speed.

Collapse JASS:
 // calculations
  set d.x = d.x - d.dx
        set d.y = d.y - d.dy
        set d.zx = d.zx - d.x
        set d.zy = d.zy - d.y
        call SetUnitPosition(d.target,d.zx,d.zy)

Am I doing any of the calculations wrongly? I sort of followed anitarf's vector system test map example.

EDIT: What is a parametrix function?
07-02-2009, 12:25 PM#6
moyack
Parametric functions: http://en.wikipedia.org/wiki/Parametric_equation
07-02-2009, 12:31 PM#7
wraithseeker
I don't understand it very well, could you explain more?

EDIT: I did something like this now

Collapse JASS:
 set d.x = speed * Cos(angle) * TIME
        set d.y = speed * Sin(angle) * TIME
        set d.z = 0 * TIME


Collapse JASS:
set d.x = d.x - d.dx
        set d.y = d.y - d.dy
    call SetUnitPosition(d.target,GetUnitX(d.target)+d.x,GetUnitY(d.target)+d.y)

Am I correct?
07-02-2009, 01:21 PM#8
moyack
parametric functions is represent how change XYZ corrdinates respect a parameter (in this case, time).

So movement in X,Y,Z are expressed sa independent functions respect time.

X(t) = a function
Y(t) = another function
Z(t) = another function

Parabolic movement is a good example, a parabola can be expressed in parametric functions in this way:

X(t) = X0+Vx*t
Y(t) = Y0+Vy*t+0.5gt^2

So in your examples, yes, you're doing it in a parametric way.
07-02-2009, 01:25 PM#9
wraithseeker
+0.5gt^2

Can be skipped right?'

So in your examples, yes, you're doing it in a parametric way.


The second example?
07-02-2009, 01:33 PM#10
Anitarf
Collapse JASS:
    set v=vector.difference(B,A) //get the difference between the start and the end point
    call v.scale(TIME/(v.getLength/speed)) //get the velocity that matches the desired speed

//...then, in a function run by a periodic timer
    call position.add(v)
    call SetUnitX(u, position.x)
    call SetUnitY(u, position.y)
    call MoveLocation(l, position.x, position.y)
    call SetUnitFlyHeight(u, position.z-GetLocationZ(l), 0)
This is pseudocode for simple linear movement. If you want it to slow down or accelerate, it's a simple matter of one additional vector.
07-02-2009, 01:43 PM#11
moyack
Quote:
Originally Posted by wraithseeker
+0.5gt^2

Can be skipped right?'
if you don't want parabolic shape, yes.


Quote:
The second example?
yes, the second one.
07-02-2009, 02:13 PM#12
wraithseeker
@Anitarf
First Attempt


Collapse JASS:
        set d.x = GetUnitX(target) * Cos(angle) * TIME
        set d.y = GetUnitY(target) * Sin(angle) * TIME
        set d.vy =  GetUnitY(target) + d.distance * Sin(angle)
        set d.vx = GetUnitX(target) + d.distance * Cos(angle)
        set d.dx = d.vx - d.x
        set d.dy = d.vy - d.y
        set d.dx = d.dx * (TIME/SquareRoot(d.dx*d.dx + d.dy*d.dy + d.dz*d.dz)/speed) // is the TIME variable the TIMEOUT of the periodic function?
        set d.dy = d.dx * (TIME/SquareRoot(d.dx*d.dx + d.dy*d.dy + d.dz*d.dz)/speed) // or just the time of the knockback? If it's the time of the knockback, how do I find it?
// periodic function
        set d.x = d.x + d.dx
        set d.y = d.y + d.dy
        call SetUnitPosition(d.target,d.x,d.y)



Didn't work for me. How would I use that one vector to make it slow down?
Second Attempt


Collapse JASS:
        local real x
        local real y
        local real z
        set d.distance = -1 * speed * speed / (2 * -decrement / TIME)
        set x = GetUnitX(target) * Cos(angle) * TIME
        set y = GetUnitY(target) * Sin(angle) * TIME
        set d.vi = vector.create(x,y,0)
        set y =  GetUnitY(target) + d.distance * Sin(angle)
        set x = GetUnitX(target) + d.distance * Cos(angle)
        set d.vf = vector.create(x,y,0)
        set d.V = vector.create(0.,0.,0.)
        set d.V =vector.difference(d.vf,d.vi) //get the difference between the start and the end point
        call d.V.scale(TIME/(d.V.getLength()/speed)) //get the velocity that matches the desired speed
         // periodic function
        set v = vector.create(x,y,0)
        call v.add(d.V)
       call SetUnitPosition(d.target,v.x,v.y)




@Moyack

Everything works good except when I add in * TIME in the periodic function.

Collapse JASS:
   
     set d.zx = speed * Cos(angle) * TIME
     set d.zy = speed * Sin(angle) * TIME
     set d.zz = 0 * TIME
     set d.dx = decrement * Cos(angle) * TIME
     set d.dy = decrement * Sin(angle) * TIME
     set d.zz = 0 * TIME
     // periodic function
     set d.zx = d.zx - d.dx // when I put * time for both of this, they bug I guess it is already because I converted it to a timeframe?
     set d.zy = d.zy - d.dy

So which method should I use which is more user friendly?
07-02-2009, 02:39 PM#13
Anitarf
I'm sorry, I don't really understand your code. Perhaps post a more detailed description of what you're trying to do?
07-02-2009, 02:40 PM#14
wraithseeker
Updated the post.

EDIT: The second approach works but I am not supposed to be getting knocked back at a constant speed but speed is constantly getting reduced y a decrement.
07-02-2009, 03:26 PM#15
Anitarf
Well, you don't really need to use a 3D vector library for simple 2D knockback. Also, looking at your code it seems you want to move your unit for a certain distance, not from point A to B (sure, one can be translated into the other, but it's pointless if the code then has to translate it back).
Anyway, there are knockback systems out there that do all this stuff for you.