Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 06 Jan 2014
Offline Last Active Jun 19 2014 03:30 AM

#5122828 Rotate sprites around a point in 2D

Posted by Dobbydoo on 11 January 2014 - 06:51 AM

I got it working! Thanks a lot for your help! biggrin.png


I'm going to try to explain what I did, so that others trying to achieve the same might benefit from it smile.png


First I create a child object I place it at the mousepos

entity.childObjects.Add = new Rectangle(ms.X, ms.Y, size.X, size.Y);

Then I store the distance between the childObject and the Base of the parent (I use a list for this at the moment, but you should probably create a class for childObject and have all the information you need there)

entity.ConnectionGap.Add(new Vector2(ms.X - entity.Base.X, ms.Y - entity.Base.Y));

In my entity class, this is what I do to get the right position. As I mentioned, it's probably better to create a childObject class, so that you won't need lots of different lists.

for (int i = 0; i < childObjects.Count; i++)
                Vector2 newPos =
GetPositionOfSatellite(new Vector2(Base.X, Base.Y),
new Vector2(ConnectionGap[i].X, ConnectionGap[i].Y).Length(),
Vector2ToRadian(new Vector2(ConnectionGap[i].X, ConnectionGap[i].Y)) + Rotation); //I changed this method a little, because it inverted my coordinates for some reason

                childObjects[i] = new Rectangle((int)newPos.X, (int)newPos.Y, childObjects[i].Width, childObjects[i].Height);

private float Vector2ToRadian(Vector2 direction)
            return (float)Math.Atan2(direction.Y, direction.X);

I hope I explained the well enough, and that someone will benefit from it smile.png