0
\$\begingroup\$

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));
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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));
\$\endgroup\$
9
  • \$\begingroup\$ 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? \$\endgroup\$
    – 3D-kreativ
    Commented Jul 17, 2012 at 13:06
  • \$\begingroup\$ What is spaceshipDirection? Is it an angle? \$\endgroup\$
    – Kikaimaru
    Commented Jul 17, 2012 at 13:09
  • \$\begingroup\$ That a good question! How can I see that? It could be radians!? \$\endgroup\$
    – 3D-kreativ
    Commented Jul 17, 2012 at 13:13
  • \$\begingroup\$ It's not working! There are red lines below the code and the error message is about invalid arguments!? \$\endgroup\$
    – 3D-kreativ
    Commented Jul 17, 2012 at 13:23
  • \$\begingroup\$ 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... \$\endgroup\$
    – Kikaimaru
    Commented Jul 17, 2012 at 13:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .