Jump to content
  • Advertisement
Sign in to follow this  
Doggolainen

Vector rotation and mirroring

This topic is 2640 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!