0
votes

I am trying to create a function that will take a point, and a distance and give me a radon location in the circle in that distance.. example

SpawnPlanet(PlanetToOrbitAround, Distance 200 px) returns a point in the "circle" of that 200 pixels away from the planet.

I'm also looking for the actual rotation logic, so one I spawn planet I have a method

UpdateRotation(PlanetToOrbitAround, OrbitingPlanet, 200 px distance, 5 degrees of speed)

I'm terrible at math, I've found some examples and everything I find doesn't seem to work for me(probably because of my lack of understanding of the math involved). The rotation seems to work for a planet around a sun, but not a moon around a planet

public Vector2 RotateAboutOrigin(Vector2 point, Vector2 origin, float rotation) { return Vector2.Transform(point - origin, Matrix.CreateRotationZ(rotation)) + origin; }

is the logic I'm using.. with calls as such...

        mPlanetLocation = RotateAboutOrigin(mPlanetLocation, new Vector2(GraphicsDevice.Viewport.Width / 2 - 25, GraphicsDevice.Viewport.Height / 2 - 25), .005f);


        mMoonLocation = RotateAboutOrigin(mMoonLocation, mPlanetLocation, .005f);

The moon rotates strangely and oblong . Any help would be great!

1

1 Answers

4
votes

I've been through this exact thing in my game!

enter image description here

I have a base class that I use for planets and other space objects. These contain simple properties like Position, Origin, Distance and `Angle.

The angle property uses a setter to change the position based on the desired angle, distance to sun/object and the position + origin of the center.

    public float Angle
    {
        get { return angle; }
        set
        {
            angle = value;
            position = Rotate(MathHelper.ToRadians(angle), Distance, SunPosition + Origin);

        }
    }
    public static Vector2 Rotate(float angle, float distance, Vector2 centrer)
    {
        return new Vector2((float)(distance * Math.Cos(angle)), (float)(distance * Math.Sin(angle))) + center;
    }

Using this you could easily do something like SpawnPlanet(SunPosition, Distance) and update the angle by X amount each update. (planet.Angle += X)

Pretty much the same deal that you are doing, but see if this code matches up with your algorithm. For the strange moon shape, could you show some more code and show an example of the orbit?