#### Archived

This topic is now archived and is closed to further replies.

# Vectors.. probably a simple question for you gurus out there

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

## Recommended Posts

okay, here''s the situation: i''ve built a fairly robust sprite class, and up until recently i''ve used x and y coords to keep track of where the sprite is located. that works fine and dandy, although im wanting to do somethign that''s a bit more intuitive: i''d like to store a vector for the direction the sprite is facing/traveling, and a variable for the speed it''s traveling at. the only problem is this: i suck at vector math and have no idea where to start for example: i''m aware of how to store a direction vector and how to change it, but im not quite sure how to translate this information into useful x/y coordinates. and since i''ve obviously never tried something along these lines, i dont even know if it''s a good means of moving a sprite. in my class i want to basically control a sprite''s movement with three public members:
CVector direction;
double  speed;
void CSprite::Update(); // which would translate the vector information into usable x/y coordinates

sorry if this post is a bit of a mess, my thoughts aren''t 100% on how to go about this. anybody got any leads? -eldee ;another space monkey; [ Forced Evolution Studios ]

##### Share on other sites
It's actually pretty simple...

    void CSprite::Update(){    position.x += speed * direction.x;    position.y += speed * direction.y;}

Just make sure your direction vector is of unit length.
A very simple example, but you get the idea.....

-Crawl

------------
"You know you're cool when you use MacGuyver as a verb..." <--- press here

[edited by - Crawl on May 3, 2002 11:49:16 AM]

##### Share on other sites
As Crawl has shown you always need the position in addition to the velocity vector. In addition you could use the magnitude of the velocity vector for the speed.
Therefore you would only need two vectors:
- the vector for the current position
- the velocity vector
if dt is the time that has passed since the last update, the new position pn is given by:
pn = po + dt * v
here po ist the old position and v is the velocity vector.

##### Share on other sites
yeah, i messed around with it a bit today on
my lunch break.. seems to be working fine..
gonna take some tweaking though.
i''ve noticed that if i increase the direction
vectors coordinates, but leave the velocity
the same, the sprite''s speed increases
anyway.. dunno why that is, im sure it''s a
well documented thinger

-eldee
;another space monkey;
[ Forced Evolution Studios ]

##### Share on other sites
If I am moving east at a rate of 45 mph and north at a rate of 30 mph, then how fast am I moving? I''m moving at a rate of about 54 mph. (45,30) is a vector representing my velocity. My speed is the magnitude of that velocity. The magnitude of the vector (x,y) is sqrt(x^2+y^2). So let ||x|| be the magnitude of a vector x. What is the magnitude of ||(c*x,c*y)||? It is c*||(x,y)||. sqrt((c*x)^2+(c*y)^2) = sqrt(c^2*x^2+c^2*y^2) = sqrt(c^2*(x^2+y^2)) = c*sqrt(x^2+y^2) = c*||(x,y)||. Well actually it is the absolute value of c since the magnitude is always positive.

The differance between speed and velocity is that speed does not have a direction. If I am at mile marker 100 at 3PM travelling at 60mph and I continue travelling on that road at 60mph for the next hour then what mile marker will I be at? Will it be 40 or 160? You have to have a direction to say. If I am at mile marker 100 travelling toward lower numbers at 1,609 m/min then where will I be in an hour? Mile marker 40. I either have to convert meters per minute into miles per hour or convert the hours to minutes, calculate the meters and convert that to miles to find that out though. That isn''t exactly what you failed to do.

Rather you changed directions by increasing your speed in one direction. If I''m travelling north at 45mph and east at 30mph then change direction by going 45mph north and 60mph east I''m going to cover more ground per unit of time. Instead of being 30 miles east of my current position in an hour I''ll be 60 miles east. I will also be 45 miles north, but I won''t be 105 miles from where I started. Instead I''ll be 75 miles from where I started. That is 75mph versus 54mph so I''m moving faster.

##### Share on other sites
thanks, that brings things into perspective nicely

-eldee
;another space monkey;
[ Forced Evolution Studios ]

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 33
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631745
• Total Posts
3002000
×