Jump to content

  • Log In with Google      Sign In   
  • Create Account

Vector rotation and mirroring


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Doggolainen   Members   -  Reputation: 115

Like
0Likes
Like

Posted 22 September 2011 - 11:16 AM

I'm making this 2D game where I want several textures move accordingly to another texture. So right now I got this body where the head has to rotate around an axis accordingly to the body when the body rotates.

The following code does the trick:

Vector2 spatialPosition; //The global position


Vector2 origin; // Origin relative the position
Vector2 position; //Local position of the texture to draw i.e. the heads position relative the body.

Vector2 bodyPosition; //The local position of the body
Vector2 bodyOrigin; //origin of the body relative the bodyPosition


public void Draw(SpriteBatch spriteBatch, bool mirror, Camera camera, Vector2 spatialPosition,Vector2 bodyPosition, Vector2 bodyOrigin, float bodyRotation)
        {
            Vector2 positionOffset = RotateAroundPoint(position, bodyOrigin, bodyRotation);

            if (!mirror)
            {
                spriteBatch.Draw(texture, positionOffset + spatialPosition - camera.Position, null, Color.White, rotation + bodyRotation, origin, 1, SpriteEffects.None, 0);
            }
	}


As mentioned earlier - the code seen above works correctly. I do however want to mirror the image depending wheter the character is facing right or left, this code on the other hand does not work. It only works if the body is not rotated. The hardcoded "100" is the width of the body texture.


            else
            {
       		spriteBatch.Draw(texture, new Vector2(100 - positionOffset.X - texture.Width + origin.X * 2, positionOffset.Y) + spatialPosition - camera.Position, null, Color.White, -rotation - bodyRotation, origin, 1, SpriteEffects.FlipHorizontally, 0);
            }


So anyone good at algebra out there, please help me solve this nut.

Sponsor:

#2 coder0xff   Members   -  Reputation: 225

Like
0Likes
Like

Posted 23 September 2011 - 06:06 PM

Sin and Cos are what you need. I'm kinda fuzzy on what each of these variables represents (what's the difference between your hard coded 100 and texture.Width?). Another way: the origin of the sprite should be at its center (since it's mirrored through the origin, I assume)
Two things are infinite: the universe and human stupidity; and Im not sure about the universe. -- Albert Einstein




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS