I'm making a game where you walk around a circle, its 2D and the circle is going to be a planet, its kind of like Angry Birds in Space in terms of gravity. At the moment I am rotating my objects to align with the gravity vector which is pointing in to the centre of the planet. This is fine for objects on the planet but then I want to have a rocket ship that leaves the planet so I don't want to set the rotation of the ship to be inline with the planet as I will need to rotate the ship to steer. I'm sure there are also other situations where I will want to rotate objects away from this set rotation.
At the moment I use this function to apply gravity to objects which is a member of my GameObject base class -
public void ApplyGravity(Vector2 planetPosition, float gravityForce, float deltaTime)
{
Vector2 gravity = m_position - planetPosition;
if (gravity.LengthSquared() != 0)
{
gravity.Normalize();
}
gravity *= gravityForce;
// Apply gravity to velocity
m_velocity -= gravity * deltaTime;
// Adjust rotation of object to stick to circle
Vector2 normalisedGravity = gravity;
normalisedGravity.Normalize();
float desiredRotation = (float)Math.Atan2(gravity.Y, gravity.X);
m_rotation = desiredRotation;
m_rotation += MathHelper.ToRadians(90);
}
So just a bit of advice would be nice,
Should I keep it like this for objects on the planet which will want to be rotated to snap to the circle in most cases and then have specific states where the objects are allowed to rotate?
Or is there something I'm missing, maybe some sort of rotational gravity force that is applied to objects to attempt to align them with the planet but still allow some movement if there is a strong enough force in the opposing direction?
Any advice is appreciated, if you would like more info or my explanation was rubbish then just say,
Thanks.