Sign in to follow this  
watermelonChris

Manipulating Vectors in XNA

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?

Thanks for your help :)

Share this post


Link to post
Share on other sites
L. Spiro    25638
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:
[code]vSpritePos += vSpriteDir * fSpeed * fTime;[/code]
fTime is the time since the last update.


L. Spiro

Share this post


Link to post
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:
[code]vSpritePos += vSpriteDir * fSpeed * fTime;[/code]
fTime is the time since the last update.


L. Spiro
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='Watermelon Chris' timestamp='1313344446' post='4849039']
[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:
[code]vSpritePos += vSpriteDir * fSpeed * fTime;[/code]
fTime is the time since the last update.


L. Spiro
[/quote]

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 this post


Link to post
Share on other sites
Ah I think I've figured out this vector2 stuff somewhat :D. 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:
[code]
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++;
}
[/code]

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 this post


Link to post
Share on other sites
NightCreature83    5006
[quote name='Watermelon Chris' timestamp='1313410403' post='4849341']
Ah I think I've figured out this vector2 stuff somewhat :D. 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:
[code]
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++;
}
[/code]

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...
[/quote]

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 this post


Link to post
Share on other sites
[quote name='NightCreature83' timestamp='1313414028' post='4849363']
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.
[/quote]

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 this post


Link to post
Share on other sites
Ahrhrhrhrh!
[code]
spritePos = new Vector2(100,200);
spritePos.Normalise();
[/code]
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

Share this post


Link to post
Share on other sites
laztrezort    1058
[quote name='Watermelon Chris' timestamp='1313422564' post='4849420']
Ahrhrhrhrh!
[code]
spritePos = new Vector2(100,200);
spritePos.Normalise();
[/code]
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
[/quote]

Normalizing a vector makes it a unit vector - a vector which is exactly of length one. So, a normalized vector basically represents a direction. Each tick you are multiplying that unit vector (think of it as an arrow with a length of 1) by the distance it needs to move during that update (think of this as increasing the length of that vector but keeping it "pointing" in the same direction).

I would recommend googling some introductory tutorials/guides on vector math, matrix math, and linear algebra. If you have some background with geometry and trigonometry, it isn't too hard to grasp the fundamentals.

Share this post


Link to post
Share on other sites
[quote name='laztrezort' timestamp='1313449878' post='4849617']
[quote name='Watermelon Chris' timestamp='1313422564' post='4849420']
Ahrhrhrhrh!
[code]
spritePos = new Vector2(100,200);
spritePos.Normalise();
[/code]
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
[/quote]

Normalizing a vector makes it a unit vector - a vector which is exactly of length one. So, a normalized vector basically represents a direction. Each tick you are multiplying that unit vector (think of it as an arrow with a length of 1) by the distance it needs to move during that update (think of this as increasing the length of that vector but keeping it "pointing" in the same direction).

I would recommend googling some introductory tutorials/guides on vector math, matrix math, and linear algebra. If you have some background with geometry and trigonometry, it isn't too hard to grasp the fundamentals.
[/quote]

Thank you! It was the 'unit length' that was confusing me. Thank you for your time lol... :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this