• ### Popular Now

• 11
• 9
• 10
• 9
• 10

#### Archived

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

# 2D Vectors (I think)

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

## Recommended Posts

Hello all, here comes a probably totally stupid question, but it''s been bothering me for absolutley ages, so here goes... Say we have, in a 2D world, a ball. We want this ball to move somewhere, let''s say where the user clicks the mouse on the screen. We know the X and Y of our ball, and the X and Y of where the mouse was clicked... So, in order to get our Ball from its origin (let''s say x1, y1) along a line to it''s destination (x2, y2), we can find the difference in Y and X points, like so: (x2 - x1, y2 - y1). If we add our distance_x to the X of our ball, and the distance_y to the Y of our ball, we get to our destination, right? (Yes, i''m long winded, I know ). My problem comes with actually moving the buggering thing at a constant speed along this line... I know how to get the length of the line... well, sqrt(xdist*xdist + ydist*ydist) seems to work pretty 100% fine . BUT, simply moving the ball at a constant speed along the line every loop is causing me a real headache... I know it''s probably very very easy, seeing as 2D game ever uses it (I dunno, like a bullet in... er... Turrican, on the Amiga??). Is it also something to do with angles, and therefore trigonometry? I''m sure I have a hazy view of what to do, but everything i try results in failure . All i need to know is the methods of implementing it, which surely aren''t too language & code specific... it''s just maths... seeing as I suck at maths, i''m stuck . I hope I''ve made some sense here, and if not, i''ll just have to take the flak and try again Cheers for trying to understand my stupid ways! Nick - Head Designer, Llamasoft.net -- Visit our website... Llamasoft.net Games, goodies and ingenuity

##### Share on other sites
  #include int xDest;int yDest;float x;float y;slope = (float)(yDest-y)/(float)(xDest-x);while(you haven't reached your destination){ if (x < xDest && y < yDest) { y += 3*(float)sin(atan(slope)); x += 3*(float)cos(atan(slope)); } if (x < xDest && y > yDest) { y += 3*(float)sin(atan(slope)); x += 3*(float)cos(atan(slope)); } if (x > xDest && y > yDest) { x -= 3*(float)cos(atan(slope)); y -= 3*(float)sin(atan(slope)); } if (x > xDest && y < yDest) { x -= 3*(float)cos(atan(slope)); y -= 3*(float)sin(atan(slope)); }

"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Edited by - ncsu121978 on June 13, 2000 5:51:48 PM

##### Share on other sites
replace the 3 by how fast you want to move

"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

##### Share on other sites
NewX = x1+(x2-x1) will put your ball at the second point right? Well, to move it at a continuous speed to the new location you need to add time to the equation like so,NewX=x1+(x2-x1)*Time, where Time is a value starting at 0 and incrementing towards 1. When it reaches 1 your ball is at the new point. So say you wanted it to take 30 frames to reach the new point, then Time would equal (1/30)*FrameNum and you would just need to add 1 to FrameNum every frame and stop the animatation after 30 frames have ellapsed.

(Its late and those equations might not be exactly right, but you get the idea )

##### Share on other sites
Thanks for the quick and informed response, ncsu121978, but i fear because of my lack of comprehension, when i adapted your source, the ball *almost* went correctly, but often got to the x or y destination first... not at the same time... i put this down to loss of accurracy with some variables (i was using alot of ints) so i convetred them all to floats... but still the same
I fear that something''s going wrong somewhere, and it''s most probably me... I don''t fully understand the source you posted (I suck at maths, badly) and so I don''t really know how to best rework the source . Thanks anyway...
And ''Anonymous Poster'', I thought of that before, jsut say dividing the destinations (x and y) by a figure, say, 10, but then the ball will ALWAYS hit the destination after 10 ''steps'' as opposed to moving 10 units until it hits the destination...
Thanks anyway guys, and any alternatives are appreciated from anyone

--

Llamasoft.net
Games, goodies and ingenuity

##### Share on other sites
did you declare slope as a float ?
i forgot to put that on there
and make all the casts just like I did because if you don''t have some of those casts then it will mess it up a little
make sure you copied it exactly b/c i use this code to move a space ship around the screen and it works fine
i would have the spacewhip at x, y then xDest and yDest would be where I clicked the mouse

Oh well

"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

##### Share on other sites
Hmm... yeah I made slope a float... I did everything as you suggested first time round, but no success... thanks anyway (Im sure in the end i'm going wrong ).
Just a side note though, what if you clicked, say, 200 along on the x axis, but 0 on the y axis... they way you do it, surely you're going to raise en error with 'DivideByZero' (well my crapped out old Borland does ) when you divide 200/0 for the slope... just an enquiry...

--

Llamasoft.net
Games, goodies and ingenuity

Edited by - Llamasoft.net on June 13, 2000 7:14:34 PM

##### Share on other sites
Aha! Success!
Thanks alot man, I got it in the end
Cheers !

--

Llamasoft.net
Games, goodies and ingenuity

Edited by - Llamasoft.net on June 13, 2000 7:47:23 PM

##### Share on other sites
yea you are right about the divide by 0
i have accounted for that in my code but forgot to account for it in the example i posted here......u should definitely check for it......and if the bottom of the slope equation is 0 then just add your speed to the y coordinate
y+=speed;
or
y-=speed;

"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

##### Share on other sites
Just a note.. that code is mathematically correct, but I don''t think you want to have it exactly like so as final code, as its (at least on non-optimizing compilers) quite inefficient.

At the least, your calculation of what you add to the X and Y are not changing (its a line -- one of the features of linearity is that the slope is constant). So you can do that once outside the loop and store it in a variable. Or a pair of variables, actually.

There are a million ways to do what you want to do (effectively, draw a line). Some are faster than others. if its going to be called a LOT in your code, you might want to look into a better solution.

- Remnant
- (Steve Schmitt)