# Manipulating Vectors in XNA

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

## Recommended Posts

Hey guys... I'm trying to move a a sprite to a point basically I have a direction and the 'speed' but I'm not sure how to add this 'component' to a vector 2.
I basically have this 'cannon' in the bottom middle of the screen, and depending on the angle of this cannon, I'd like a bullet to fire out and keep going in that direction untill off screen.

So erm what do I do?

I'm not asking for code I'm just not sure on how to update the bullet vector/point on each update?

##### Share on other sites
Hidden
Here is a diagram..

Here is a diagram:

##### Share on other sites
I assume your direction is normalized and you have math operators on your vectors that allow you to perform vecA + vecB and such.

Each frame:
vSpritePos += vSpriteDir * fSpeed * fTime;
fTime is the time since the last update.

L. Spiro

##### Share on other sites

I assume your direction is normalized and you have math operators on your vectors that allow you to perform vecA + vecB and such.

Each frame:
vSpritePos += vSpriteDir * fSpeed * fTime;
fTime is the time since the last update.

L. Spiro

Can you explain this? What do I assign fTime as? Thank you

##### Share on other sites

[quote name='YogurtEmperor' timestamp='1313338778' post='4849016']
I assume your direction is normalized and you have math operators on your vectors that allow you to perform vecA + vecB and such.

Each frame:
vSpritePos += vSpriteDir * fSpeed * fTime;
fTime is the time since the last update.

L. Spiro

Can you explain this? What do I assign fTime as? Thank you
[/quote]

I think I get it... Your saying add (vSprite * Speed per unit time * amount of time) to the sprite position.

I but... If you have Vector A that is 10 units north. Lets say we have speed of 1 unit per second. If one second has passed:

(10) * 1 * 1 = 10 units up.. So fSpeed is actually the percentage of the vector to move per unit time?

This is doing my head in =/

##### Share on other sites
Ah I think I've figured out this vector2 stuff somewhat . If anyone wants to know how a vector2 is a vector when it actually only has 1 point. Think of it as 0, 0 -> xValue, yValue...
So if we have a vector of 10, 10 it's actually an 'line' going from 0,0 to 10,10...

Please correct me if I'm wrong anyone...

So I tried playing around with this:
 if (bob == 15) // Update every 15 updates which is 1/4 of a second { bob = 0; conPos = new Vector2(10, 30); GamePadState gamePadState; gamePadState = GamePad.GetState(PlayerIndex.One); spritePos += conPos; } else { bob++; } 

This works as expected with it moving every 1/4 second....
However if conPos.Normalise(), the sprite moves very smoothly (updating every frame I'm guessing)... Why does that happen?

All I can read up on is that you Normalise() for the 'velocity' to stay the same while going diagonally...

##### Share on other sites

Ah I think I've figured out this vector2 stuff somewhat . If anyone wants to know how a vector2 is a vector when it actually only has 1 point. Think of it as 0, 0 -> xValue, yValue...
So if we have a vector of 10, 10 it's actually an 'line' going from 0,0 to 10,10...

Please correct me if I'm wrong anyone...

So I tried playing around with this:
 if (bob == 15) // Update every 15 updates which is 1/4 of a second { bob = 0; conPos = new Vector2(10, 30); GamePadState gamePadState; gamePadState = GamePad.GetState(PlayerIndex.One); spritePos += conPos; } else { bob++; } 

This works as expected with it moving every 1/4 second....
However if conPos.Normalise(), the sprite moves very smoothly (updating every frame I'm guessing)... Why does that happen?

All I can read up on is that you Normalise() for the 'velocity' to stay the same while going diagonally...

By having the direction as a unit vector you can use this in collision detection as it tells you which direction the object is moving in but not the speed of it which you don't need in the intersection test of your collsions. Then with the speed variable you can determine the time when this collision is happening.

##### Share on other sites

By having the direction as a unit vector you can use this in collision detection as it tells you which direction the object is moving in but not the speed of it which you don't need in the intersection test of your collsions. Then with the speed variable you can determine the time when this collision is happening.

I'm not doing any collision as yet?... I just wanna know why when I normalise that variable the sprite moves very slowly but smoothly instead of moving every 1/4 seconds.

##### Share on other sites
Ahrhrhrhrh!
 spritePos = new Vector2(100,200); spritePos.Normalise(); 
After it is normalised the value of spritePos is: {X:0.4472136 Y:0.8944272}.

Which explains why it is moving so slowly... What is it actually doing to the spritePos vector?
Normalise should output(?) the legnth of the vector... Or am I wrong?

So sqroot( 200^2 + 100^2) = 223.6

1. 1
Rutin
30
2. 2
3. 3
4. 4
5. 5

• 13
• 14
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632961
• Total Posts
3009496
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!