Ok here we go:
Imagine you have a ship with a position and a speed
class Ship
{
public:
vec2 position;
vec2 direction;
vec2 velocity;
}
Ship ship;
Ignoring the bad OO setup, we now want to rotate the ship, and the move the ship.
// Rotate vector by an angle
// remember to either create a new vector by doing this, or make a copy of the old one, as you require both x and y
ship.direction = vec2(cos(angle) * ship.direction.x - sin(angle) * ship.direction.y,
sin(angle) * ship.direction.x + cos(angle) * ship.direction.y);
// .. and we don't need to normalize, because cos() and sin() already makes it unit length 1
// If we are using the ships engines, we are adding thrust
// Add speed towards the direction the ship is facing
float thrust = 0.1f;
ship.velocity += ship.direction * thrust;
// And move the ship by its constant velocity each 'tick'
ship.position += ship.velocity;
// Let's check if our round ship is too close to a round object
float ship_radius = 2.0f;
float object_radius = 2.0f;
// create new vector which we will be measuring the length off of
// another method is to use an existing distance() function from a library
float dist = (ship.position - object.position).length();
// or..
float dist = distance(ship.position, object.position);
if (dist < ship_radius + object_radius)
{
// the ship is too close to the object
// here we can calculate a tangent and bounce the ship if we wanted too
}
I just wrote that up, so there may be errors
It's also not the best way to rotate a ship, admittedly. It works though!
Edited to fix some issues.