Average of two vectors

Started by
6 comments, last by BradDaBug 21 years, 11 months ago
I have this sprite, and it has a motion vector. I want it to randomly wander around, so every 500 ms or so it randomly creates a new vector. I want it to slowly move from the current vector to the new vector, so it looks smooth, not erratic. Right now I''m just finding the average of the X and Y components of the vectors and setting the motion vector to that, like this:
  	
if (SDL_GetTicks() - TimeAtLastVectorChange > 200)
{

TimeAtLastVectorChange = SDL_GetTicks();

MovementVector.SetLength(200);	//200 pixels per second!

MovementVector.SetAngle(rand() % 360);

}

//Add the new vector and the old vector

int x = MovementVector.GetX();
int y = MovementVector.GetY();

int oldx = CurrentVector.GetX();
int oldy = CurrentVector.GetY();

CurrentVector.SetX((oldx + x) / 2);
CurrentVector.SetY((oldy + y) / 2);

  
It doesn''t seem to be working. The sprite still jerks around each time the vector changes. What''s wrong with it?
I like the DARK layout!
Advertisement
Lookup parametric interpolation.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
Try (1-t)*u+t*v while varying t from 0 to 1.
Keys to success: Ability, ambition and opportunity.
What does that give me? X? Y? i don''t see right off how to use it.
I like the DARK layout!
u is startpos (vector)
v is endpos (vector)
t is time (float)

(1-t)*u + t*v

for t = 0 results in (1-0)*u + 0*v = u => you''re at the startpos
for t = 1 results in (1-1)*u + 1*v = v => you''re at the endpos
for t = .5 results in .5u + .5v => you''re in the middle..

this function is often called lerp

vector lerp(vector start,vector end,float t) {
return (1-t)*start + t*end;
}

lerp is for Linear intERPolation.



"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Those lerp functions look like they deal more with position than velocity - they''re just a way to find what the current position should be when you know the start position, the end position, and the time elapsed. Not real appropriate for your situation, as far as I can tell, Brad.

That is, assuming CurrentVector is the current movement vector and MovementVector is the new movement vector. Right? I think the previous posters assumed CurrentVector was position and MovementVector was movement. Before I go on, you should clarify this.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
CurrentVector is where the sprite is moving right now. MovementVector is the new direction the sprite should move in.

[edited by - BradDaBug on May 4, 2002 5:27:08 PM]
I like the DARK layout!
quote:Original post by CGameProgrammer
Those lerp functions look like they deal more with position than velocity

You can interpolate vector quantities, which makes them appropriate for the velocity as well.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement