Vector Question (using vector class from boards)

Monkey Forums/Monkey Programming/Vector Question (using vector class from boards)

xzess(Posted 2011) [#1]
Hi,

im using the vector class, posted in the boards.


I have 2 Vectors, Postion and Movement

I set the Movement and the Position will smoothmove to it by dividing the distance.

My question is:
How can i move to a destination without changing the length/speed?
Or how can i get the direction to the movement and then apply a x=1 force so it will move to the direction of the destination with a 1.0 speed

Thanks in advice!


Jesse(Posted 2011) [#2]
you need to figure out the unit movement:
vx -> is the component x for movement
vy -> is the component y for movement
dx -> unit movement for x (x normalized)
dy -> unit movement for y (y normalized)

length:float = sqrt(vx*vx+vy*vy)
dx = vx/length
dy = vy/length

dx and dy will produce a 1 unit movement if added to the position.

note that if length is zero you will get a division by zero error(NAN).

there should be something that do that in one of the functions.


Tibit(Posted 2011) [#3]
If you want to have something move from A to B over certain time, without considering the distance. It will always take the same time, then what you probably want is to interpolate:



You input StartPosition and EndPosition, then you pick a speed, let us say 10% of this distance over time, meaning you will reach the end (100%) in 10sec then.

If that was not what you meant, what Jesse said (above) in Vector Math is:
Velocity.Normalize()
Note: It will not crash on zero (assuming you are using my Vector class), instead if you attempt to normalize a Vector with a length of 0, nothing will happen.

This will give you a Velocity of 100 for example:
Velocity.Normalize().Multiply( 100 )

You can then use Delta-Time to add Velocity to Position:
Velocity.Multiply( DeltaTime ) 'Use pixels/sec instead of pixels/fps
Position.Add( Velocity )


xzess(Posted 2011) [#4]
Thank you both very much and thanks for sharing!