This discussion is archived
2 Replies Latest reply: Aug 10, 2011 2:20 PM by 827803

# Spaceship rotation vector

Currently Being Moderated
Hi

I am trying to make an asteroids clone, but i`m having problems making the spaceship move freely in space.

The spaceship is made of a vector which has a length that increases or decreases as the player presses up or down on the keyboard, and an angle which increases or decreases as the player presses left or right.

The position of the spaceship is then calculated by converting from polar to cartesian coordinates:
``````x += length * Math.cos(Math.toRadians(angle));
The problem is that the spaceship can only move in 45 degree angles, since (x,y) will always be either: (1,0),(0,1),(1,1),(-1,0),(0,-1) or (-1,-1)

How can I get the spaceship to move in all directions?
Thanks!
• ###### 1. Re: Spaceship rotation vector
Currently Being Moderated
843870 wrote:
Hi

I am trying to make an asteroids clone, but i`m having problems making the spaceship move freely in space.

The spaceship is made of a vector which has a length that increases or decreases as the player presses up or down on the keyboard, and an angle which increases or decreases as the player presses left or right.

The position of the spaceship is then calculated by converting from polar to cartesian coordinates:
``````x += length * Math.cos(Math.toRadians(angle));
The problem is that the spaceship can only move in 45 degree angles, since (x,y) will always be either: (1,0),(0,1),(1,1),(-1,0),(0,-1) or (-1,-1)

How can I get the spaceship to move in all directions?
Thanks!
I don't see your problem, from your description you have the correct answer for movement with vector rotation. If you are incrementing your X and Y variables correctly, then you will have rotation from 0 to 2*PI, this is what is needed. If you are using your arrow keys for nothing more than assigning 1 or -1, then you are making a very large mistake there.

If you want to increment by some fractional value, say 1 degree per second and broken into 1/10 increments for implementation, then what you need to do is start a timer each time you have a keydown and set a flag showing which key was put down. Each time your timer goes off, then you need to upgrade your position until your keyup event.
• ###### 2. Re: Spaceship rotation vector
Currently Being Moderated
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

#### Legend

• Correct Answers - 10 points