-
|
|
Moving an object from one point to another at a constant speed
|
I'm new to game programing, and vector related math.
Anyways, I want to move an object between 2 manipulated points in a 2D enviorment at a constant speed, ex. bullet from the player to mouse point and continue at this angle.
my code so far (exremly sloppy i know) :
//when started
wantTo = new Vector2(mousepoint.X, mousepoint.Y);
Vector2 playervect = new Vector2 (player.X,player.Y);
playervect.Normalize();
wantTo.Normalize();
//speed is the constant speed i want the bullet to move at
SpeedVect = (wantTo - playervect) * speed ;
bulletVect = new Vector2(player.X, player.Y);
//every timer tick
bulletVect= bulletVect + SpeedVect;
X = System.Convert.ToInt32(BullNowVect.X);
Y = System.Convert.ToInt32(BullNowVect.Y);
bullet = new Point(X, Y);
this makes the bullet to move in only 2 directions, to the upper right (if the mouse in on the right side) and to the lower left (if the mouse is on the left side of the player).
Hope this gives you an idea of my issue.
Any help or links to learning resources would be appreciated!
Thanks guys!
|
|
-
-
- (40)
-
premium membership
-
Posts
16
|
Re: Moving an object from one point to another at a constant speed
|
I always end up doing something like this:
I have a Vector2 for the bullets velocity, that would be your SpeedVect, then I have the position, your bulletVect.
To move the bullet and have it move smoothly, you need to take into account the amount of time which has elapsed since the last frame. You can get this in your Update methods using GameTime. (I forget the exact calls, I have it wrapped in my project.)
Then you basicly go
bulletVect += SpeedVect * elapsedTime;
That will keep the bullet moving at a constant speed even if the frame rate drops for a moment or something.
|
|
-
|
|
Re: Moving an object from one point to another at a constant speed
|
xryan509x:I'm new to game programing, and vector related math. Anyways, I want to move an object between 2 manipulated points in a 2D enviorment at a constant speed, ex. bullet from the player to mouse point and continue at this angle. my code so far (exremly sloppy i know) : //when started wantTo = new Vector2(mousepoint.X, mousepoint.Y); Vector2 playervect = new Vector2 (player.X,player.Y); playervect.Normalize(); wantTo.Normalize(); //speed is the constant speed i want the bullet to move at SpeedVect = (wantTo - playervect) * speed ; bulletVect = new Vector2(player.X, player.Y); //every timer tick bulletVect= bulletVect + SpeedVect; X = System.Convert.ToInt32(BullNowVect.X); Y = System.Convert.ToInt32(BullNowVect.Y); bullet = new Point(X, Y); this makes the bullet to move in only 2 directions, to the upper right (if the mouse in on the right side) and to the lower left (if the mouse is on the left side of the player).
Hope this gives you an idea of my issue. Any help or links to learning resources would be appreciated! Thanks guys!
//when started wantTo = new Vector2(mousepoint.X, mousepoint.Y); Vector2 playervect = new Vector2 (player.X,player.Y); //speed is the constant speed i want the bullet to move at SpeedVect = (wantTo - playervect).Normalize() * speed * (float)gameTime.ElapsedGameTime.TotalSeconds; bulletVect = new Vector2(player.X, player.Y); //every timer tick bulletVect= bulletVect + SpeedVect; X = System.Convert.ToInt32(BullNowVect.X); Y = System.Convert.ToInt32(BullNowVect.Y); bullet = new Point(X, Y); I believe that should work, as long as your mouse vector and your player vector share the same origin. Out of curiosity, why are you using ints for position, instead of floats?
|
|
-
|
|
Re: Moving an object from one point to another at a constant speed
|
Erglegrue: xryan509x:I'm new to game programing, and vector related math.
Anyways, I want to move an object between 2 manipulated points in a 2D enviorment at a constant speed, ex. bullet from the player to mouse point and continue at this angle.
my code so far (exremly sloppy i know) :
//when started
wantTo = new Vector2(mousepoint.X, mousepoint.Y);
Vector2 playervect = new Vector2 (player.X,player.Y);
playervect.Normalize();
wantTo.Normalize();
//speed is the constant speed i want the bullet to move at
SpeedVect = (wantTo - playervect) * speed ;
bulletVect = new Vector2(player.X, player.Y);
//every timer tick
bulletVect= bulletVect + SpeedVect;
X = System.Convert.ToInt32(BullNowVect.X);
Y = System.Convert.ToInt32(BullNowVect.Y);
bullet = new Point(X, Y);
this makes the bullet to move in only 2 directions, to the upper right (if the mouse in on the right side) and to the lower left (if the mouse is on the left side of the player).
Hope this gives you an idea of my issue.
Any help or links to learning resources would be appreciated!
Thanks guys!
//when started
wantTo = new Vector2(mousepoint.X, mousepoint.Y);
Vector2 playervect = new Vector2 (player.X,player.Y);
//speed is the constant speed i want the bullet to move at
SpeedVect = (wantTo - playervect).Normalize() * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
bulletVect = new Vector2(player.X, player.Y);
//every timer tick
bulletVect= bulletVect + SpeedVect;
X = System.Convert.ToInt32(BullNowVect.X);
Y = System.Convert.ToInt32(BullNowVect.Y);
bullet = new Point(X, Y);
I believe that should work, as long as your mouse vector and your player vector share the same origin.
Out of curiosity, why are you using ints for position, instead of floats?
I got it working the other day, but that code is alot neater.
Thanks!
Ha, not exactly sure. Most likely just a newbie mistake.
|
|
|