Manipulating Vectors in XNA

Started by
9 comments, last by watermelonChris 12 years, 8 months ago
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 :)
Advertisement
Here is a diagram..
Here is a diagram:
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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


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 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 =/
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:

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...

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:

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion


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.
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

This topic is closed to new replies.

Advertisement