Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I wonder if it's possibel to add a value to X and Y in a Vector2? I have this code that creates a missile that use the position from the spaceship as a start position. But I would like to add some pixels so that the missile starts a little bit away from the spaceship. can I do that?

objectManager.AddNewMissileToList(new Missile(textureMissile, spaceship.spaceshipPosition, spaceship.spaceshipDirection));
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You mean something like

Vector2 offset = new Vector2(2,1);
objectManager.AddNewMissileToList(new Missile(textureMissile, spaceship.spaceshipPosition + offset, spaceship.spaceshipDirection));

if the spaceShipDirection is directional vector

Vector2 offset = spaceship.spaceshipDirection * 1.5f;
objectManager.AddNewMissileToList(new Missile(textureMissile, spaceship.spaceshipPosition + offset, spaceship.spaceshipDirection));

or if the spaceShipDirection is float angle

Vector2 offset = new Vector2((float)Math.Sin(spaceship.spaceshipDirection),
                             (float)Math.Cos(spaceship.spaceshipDirection))
                 * 1.5f;

objectManager.AddNewMissileToList(new Missile(textureMissile, spaceship.spaceshipPosition + offset, spaceship.spaceshipDirection));
share|improve this answer
    
Thanks! I tested the first alternative, and it worked fine, until I rotated the ship! Is there a way to make it work also when the spaceship is rotating? –  3D-kreativ Jul 17 '12 at 13:06
    
What is spaceshipDirection? Is it an angle? –  Kikaimaru Jul 17 '12 at 13:09
    
That a good question! How can I see that? It could be radians!? –  3D-kreativ Jul 17 '12 at 13:13
    
It's not working! There are red lines below the code and the error message is about invalid arguments!? –  3D-kreativ Jul 17 '12 at 13:23
    
I'm writing it off the top of my head. I don't even know if spaceship.spaceshipDirection is float or Vector2 :) Just get the idea that you can add vectors together and you can compute directional vector from angle, thats all you need to do what you want... –  Kikaimaru Jul 17 '12 at 13:29
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.