Jump to content
  • Advertisement
Sign in to follow this  

Need beginner level help with 2d Vectors

This topic is 2123 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I started messing around with Missile Command and am trying to recreate the movement of the incoming missiles, and from what I can gather that requires the use of Vectors. I spent a fair amount of time last night on the internet, but I need some clarification and/or help and/or extra knowledge.
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

Share this post

Link to post
Share on other sites
I think you have a pretty good grasp already, so congrats on that. Only the last part is missing something:
spd = 10 * dt
missile.pos.x = missile.pos.x + norm.x * spd
missile.pos.y = missile.pos.y + norm.y * spd
A normalized vector is a "direction only" vector. Multiplying it with a speed (both components!) gives you the velocity. Multiplying it with dt gives you the distance to travel for the current frame. You do it a bit differently: first calculating the distance travelled for the current frame and the applying the direction. The result is the same (and might save you a multiplication).

It helps to think about this using actual physical units. A position here e.g. is in pixels, velocity (or speed) is in pixels per second, time in seconds. Always use the same units (don't mix meters and kilometers e.g.), so you don't to need adjust in every calculation anew (which is very error prone). Imagine something similar in one dimension first: A car on a (straight) track: meters for position, meters per second for speed and seconds for time.

When you are familiar enough, you can drop the explicit calculation for every component. Vector libraries usually let you write these things mathematically, e.g.:
missile.pos = missile.pos + norm * spd
Introductory courses for vector math usually make the difference between vectors and scalars explicit by using different styles (bold or sometimes even with an arrow above the letter), e.g.

pos + norm * spd

As an exercise: Can you draw lines easily with your graphics layer ? If so try to visualize an object's velocity with a "arrow". This is quite useful for debugging actually.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!