1
\$\begingroup\$

I was hoping someone could explain to me, or help me understand, the math behind rotations and origins. I'm working on a little top down space sim and I can rotate my ship just how I want it.

Now, when I get my blasters going it'd be nice if they shared the same rotation.

Here's a picture.

http://weskamouse.net/help.png

and here's some code!

blast.X = ship.X+5;
blast.Y = ship.Y;
blast.RotationAngle = ship.RotationAngle;
blast.Origin = new Vector2(ship.Origin.X,ship.Origin.Y);

I add five so the sprite adds up when facing right. I tried adding five to the blast origin but no go.

Any help is much appreciated

\$\endgroup\$
4
  • \$\begingroup\$ It seems that the rotation works, it's just the blast is always bit too much at right. Why is that sprite.X+5; there? \$\endgroup\$
    – Katu
    Jul 12, 2013 at 6:49
  • \$\begingroup\$ Well i'm using 32*32 tiles, so when they over lap theres no way I can make them in the right place. Otherwise they're just hanging out on the spaceship. So I moved them, +5, but it seems I have to alter the origin as well otherwise you get the picture I uploaded. So I know I have to do something to the origin, but it's not adding five to it, and I'm not sure how to google this. \$\endgroup\$ Jul 12, 2013 at 6:56
  • \$\begingroup\$ Without adding five they rotate on the spot, but not in the right place (from the pixel cannons on either side) \$\endgroup\$ Jul 12, 2013 at 6:56
  • \$\begingroup\$ I think i figured out the problem, answered. \$\endgroup\$
    – Katu
    Jul 12, 2013 at 7:24

2 Answers 2

2
\$\begingroup\$

if you see the draw, I think is easier to understand.

You should have a ship position that is the same for the blast, and you have two sprites with different sizes. You should realize the rotation is related to the origin, so you have to pair the rotation origins.

The yellow circles are the origins for each sprite:

enter image description here

a) Ship: You have to sure that the origin point to the center of the ship. It seems to be the center of the texture.

   o1 = new Vector2(ship.Width, ship.Height) * 0.5f;

b) Blast: The blast orign is not the texture center. It seems something like:

   o2 = new Vector2(-10, ship.Height*0.5f);

That way draw them is pretty easy:

batch.Draw(ship, ship_pos, null, color, o1, angle,....)
batch.Draw(blast, ship_pos, null, color, o2, angle,...)
\$\endgroup\$
0
\$\begingroup\$

You have to rotate first, then move to rotated direction. Also, you have to use position vectors.

Set positions:

Vector2 shipPosition = new Vector2(100,100);
Vector2 blastPosition = shipPosition;

Set origins to middle of image

ship.origin = new Vector2(ship.Heigth / 2, ship.Width / 2);
blast.origin = new Vector2(blast.Heigth / 2, blast.Width / 2);

Change rotation:

blast.RotationAngle = ship.RotationAngle;

Move blast x,y forward from ship position, towards direction its rotated

float x = shipPosition.X+(float)(Math.Cos(blast.RotationAngle - MathHelper.PiOver2) * 5f);
float y = shipPosition.Y+(float)(Math.Sin(blast.RotationAngle - MathHelper.PiOver2) * 5f);

Apply new position

blastPosition  = new Vector2(x,y);

Draw ship

    spriteBatch.Draw(ship                   //the texture
                , shipPosition              //it's location as a Vector2
                , null                      //no source rectangle - use entire Texture2D
                , Color.White               //with the image's own colors
                , ship.RotationAngle        //at the current XNA radians
                , ship.origin               //rotation point of the image, origin
                , 1                         //use the original scale
                , SpriteEffects.None        //don't flip or otherwise manipulate image
                , 0                         //no changes to layer depth
                );

Draw blast

    spriteBatch.Draw(blast                  //the texture
                , blastPosition             //it's location as a Vector2
                , null                      //no source rectangle - use entire Texture2D
                , Color.White               //with the image's own colors
                , blast.RotationAngle       //at the current XNA radians
                , blast.origin              //rotation point of the image, origin
                , 1                         //use the original scale
                , SpriteEffects.None        //don't flip or otherwise manipulate image
                , 0                         //no changes to layer depth
                );

Hope this works, i can't test this, but it should work.

Difference in this is, that if you rotate first ( you are still in the spot, inside ship, but rotated correctly ), then you move blast forward ( along the rotation its facing ) 5 steps. It should now be perfectly aligned with the ship.


NOTE:

Position is x,y where texture is located on screen.

Origin is x,y in Texture, that is used as central point when rotating it.

If you move origin from center of picture, rotation gets weird ( atleast in symmetric textures).

EDIT: Added the draw commands also, to make sure its used correctly.

\$\endgroup\$
8
  • \$\begingroup\$ Not so much :(. Would how I'm getting my rotation angle effect your math? \$\endgroup\$ Jul 12, 2013 at 7:33
  • \$\begingroup\$ third edit :p. Almost works perfectly, but I think I'm getting it. \$\endgroup\$ Jul 12, 2013 at 7:52
  • \$\begingroup\$ Well with your code, if I'm following it correctly, it does change the blast position, and rotates just as it should. The ship starts facing right, and with your code the blast is shifted, but up instead of right. \$\endgroup\$ Jul 12, 2013 at 8:01
  • \$\begingroup\$ What if you rotate ship more? If you rotate it to face up, does the blast face left? \$\endgroup\$
    – Katu
    Jul 12, 2013 at 8:03
  • \$\begingroup\$ weskamouse.net/help2.png As you can see it rotates just like it should, as if its attached to the ship, but instead of the blast being moved up (when facing right) it should be moved right(when facing right) to sync up with the cannons :) It's possible I've made some errors somewhere, been a long day. I've got to call it a night and hopefully sleep can enlighten me. Thanks for all your help I'll be sure to check back and give you cred if your solution is good. \$\endgroup\$ Jul 12, 2013 at 8:07

You must log in to answer this question.

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