Jump to content
  • Advertisement
Sign in to follow this  
watermelonChris

Manipulating Vectors in XNA

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

If you intended to correct an error in the post then please contact us.

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


Link to post
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 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:
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 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:

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

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


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


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!