Vector Question (using vector class from boards)
Monkey Forums/Monkey Programming/Vector Question (using vector class from boards)
| ||
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! |
| ||
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. |
| ||
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 ) |
| ||
Thank you both very much and thanks for sharing! |