So assume I have a missile coming in from some location at the top of the screen, and a target to strike at the bottom of the screen, lets just assume the screen is 640x480:
missile.pos.x = 100 missile.pos.y = 0 target.pos.x = 382 target.pos.y = 480
From what I understand subtracting the missile's current position from the target's current position will provide me with a direction.
direction.x = target.pos.x - missile.pos.x direction.y = target.pos.y - missile.pos.y
So if I use that as is to move the missile it gets there on the first update cycle; as you would expect. If I multiply it by anything like delta time or some random float it slows down as I approach the target. I assume this only applies if dt is less than 1, I'm guessing it would speed up on approach if dt were greater than 1.
// Teleports to target missile.pos.x = missile.pos.x + direction.x missile.pos.y = missile.pos.y + direction.y // Slows down as it approaches target missile.pos.x = missile.pos.x + direction.x * dt missile.pos.y = missile.pos.y + direction.y * dt
Then I read about normalization, which I understand as:
// The distance between the two...or the Vector length distance = math.sqrt((direction.x * direction.x) + (direction.y * direction.y)) // The normalized Vector norm.x = direction.x / distance norm.y = direction.y / distance
Using the normalized vector I get where I want to go at a consistent rate regardless of the distance between the two points, although it's updating at the same rate as the loop so it's still pretty fast. However, if I multiply it by dt it goes painfully slow.
// Constant speed from start to target, too fast missile.pos.x = missile.pos.x + norm.x missile.pos.y = missile.pos.y + norm.y // Constant speed from start to target, too slow missile.pos.x = missile.pos.x + norm.x * dt missile.pos.y = missile.pos.y + norm.y * dt
So I assume I need to apply some kind of speed, which is what might be referred to as a scalar? How do I apply it?
// Like this? spd = 10 * dt missile.pos.x = missile.pos.x + norm.x * spd
Am I on the right track with all of this stuff, or am I way off base? Any feedback, info, etc., would be appreciated!
Edited by StarHustler, 21 September 2013 - 11:28 AM.