简体   繁体   中英

Rotate a Vector2 fixated on a sprite

I'm making an asteroids clone, the laser needs to shoot out the front of the ship, however when I try and rotate the Vector using Rotation Matrices it goes haywire, flying all over the screen, I need the laser to shoot from the front of the ship, and have the origin point stay with the ship for a full 360 degrees. At the moment it only shoots straight at a 90 degree angle, when the ship faces direct East.

Here is what I have at the moment:

lLasers.Add(new Laser(Vector2.Transform(new Vector2((vPlayerPosition.X + 35), (vPlayerPosition.Y)), Matrix.CreateRotationZ(angle))));

Where angle is

Vector2 direction = mouseLoc - vPlayerPosition;
angle = (float)(Math.Atan2(direction.Y, direction.X));

Included some images to better explain my problem

Origin in Bottom Left Corner

Shooting Straight at 90 degrees

You are using the Vector2.Transform() wrong. The first parameter is your "reference" vector you want to transform with your Matrix.

In your case if you want the function to return the position of the laser starting position, you need to give Vector2(shipWidth / 2f, 0) as parameter.

So :

Vector2 laserStart = vPlayerPosition + Vector2.Transform(new Vector2(shipWidth / 2f, 0), Matrix.CreateRotationZ(angle));

Then you can start to draw your laser from this position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM