vector movement

Started by
4 comments, last by ligh 21 years, 4 months ago
lets say i have a struct that has a 2d position vector and a 2d velocity vector, to project it to the screen, i would use the x, y coords from the position vector (right?), then how would i go about calculating it''s next position such that it only moves 1 pixel per frame? thanks
Advertisement
If that is the case you take your 2d velocity vector, make it a unit vector (so it only moves your position vector 1 pixel/unit at a time) and add it to your position vector.
So:
V(velocity vector)=(x,y)
|U|(unit velocity vector)=|V|/a=1
a- some scalar

P(position vector)=(x1,y1)
P(new position)=P+U


EPHERE

[edited by - EPHERE on December 5, 2002 12:36:05 PM]
----------------------- }EPHERE{-----------------------
Your velocity vector should have a magnitude of one. The magnitude is its length and given by sqrt(x*x+y*y). To set a vector a desired length you multiply by the desird length divided by the current length. Once your velocity vector has the right length you just add it to the position. Strictly speaking for linear motion at a constant velocity it is position+velocity*time, but here your velocity is in pixels per frame and your time is measured in frames.
Keys to success: Ability, ambition and opportunity.
ok, so if my data is,
Pos(2,3)
Vel(2,3)

my |V| is 2*2+3*3 = sqrt(13)

then i get stuck..
what next?

thanks

[edit]

ah., ok, so i would then go
U = V/|V| to give me the unit vector..?
and add U+P.. ?


[edited by - ligh on December 5, 2002 6:03:54 PM]

ah ha, would u look at that.,
thanks for all the help EPHERE and LilBudyWizer!!

[edited by - ligh on December 5, 2002 6:10:04 PM]
Pixels are discrete units. If you direction is (1,1), normalizing it won''t make you move one pixel/frame... What do you want exactly?

Cédric
if i have an object, with an x,y for position and x,y for velocity, it will move across the screen at the given velocity, but smooth pixel/pixel movement..

eg,
struct vector{   float x,y}struct obj{   vector pos   vector vel}obj p;p.pos.x = 100;p.pos.y = 100;p.vel.x = 10;p.vel.y = -1;   


after a time it should end up at 110,99 but i want it to visit all the places on the line along the way

and.. if i have two objects, p1, p2 given
p1.pos.x = 100
p1.pos.y = 50
p1.vel.x = 10
p1.vel.y = 0
p2.pos.x = 100
p2.pos.y = 150
p2.vel.x = 5
p2.vel.y = 0
the p1 shoudl move faster then p2..

[edited by - ligh on December 7, 2002 3:24:59 AM]

This topic is closed to new replies.

Advertisement