I don't really want to give you all the hints, but it should be easier than you think when working with vectors. This will mean forgetting about cartesian coordinate based systems and reverting to good all position vectors. Point2D may also work if you want to use premade classes, however I recommend you make your own using float vectors. This is all that is required for the class shell!
class Vector2D
{
float x;
float y;
Vector2D()
{
x = 0.0F;
y = 0.0F;
}
float distance(Vector2D other)
{
return (float)Math.sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}
}
First, you need to implement two new properties of the moving object, a move angle and a face angle. I think the names are self explanatory! You can alter the face angle through user input, assuming the right and left keys alter the direction in which the Spaceship is pointing for example. Yet if there is no intended delay on key press, all you need to do is alter the move angle directly (and forget about the face angle), then recalculate using the follow algorithm:
float ang = Spaceship.moveAngle - 90;
float tempVelx = calcAngleMoveX(ang);
float tempVely = calcAngleMoveY(ang);
Spaceship.position.x += tempVelx;
Spaceship.position.y += tempVely;
public float calcAngleMoveX(float angle)
{
return (float)(Math.cos(angle * Math.PI / 180));
}
public float calcAngleMoveY(float angle)
{
return (float) (Math.sin(angle * Math.PI / 180));
}
You can test it using a simple random call, to ensure your ship moves appropriately!
molecule.faceAngle = rand.nextInt(360); //forget if not working with a faceAngle, and directly altering moveAngle
molecule.moveAngle = rand.nextInt(360);
Sorry if there is weird code formatting, my tab button is on some kind of manic lock! Feel free to reply with questions
Regards,
BoxCat