# Concept for moving in a direction for ship in game asteroids

**URL:** <https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456>\
**Category:** Coding Questions\
**Created:** [May 4, 2020, 7:50am UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456 "2020-05-04T07:50:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arsenii](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Arsenii](https://discourse.processing.org/u/Arsenii)\
**Post date:** [May 4, 2020, 7:50am UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456/1 "2020-05-04T07:50:08Z")

</div>

I want to recreate asteroids game but don’t know how to program ship moving in the direction of it’s aim.

---

<div class="post-metadata">

**Author:** ![timAllan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timallan/32/8946_2.png) [@timAllan](https://discourse.processing.org/u/timAllan)\
**Post date:** [May 4, 2020, 11:58am UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456/2 "2020-05-04T11:58:05Z")

</div>

Hi Arsenii, Probably the best way to understand your issue is to post your code.

---

<div class="post-metadata">

**Author:** ![Arsenii](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Arsenii](https://discourse.processing.org/u/Arsenii)\
**Post date:** [May 4, 2020, 12:50pm UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456/3 "2020-05-04T12:50:14Z")

</div>

I want my ship to move in the direction that it aims but all my variants do not work at all.Thank you in advance.

```Processing
spaceShip ship;
boolean left = false;
boolean right = false;
boolean forward = false;
float maxSpeed = 0.25;
float accelL = -0.45;
float accelR = 0.45;
float accelF = -0.05;
float inercia;
void setup() {
  size(1200, 700);
  ship = new spaceShip();
}

void draw() {
  background(0);
  ship.render();
  ship.edges();
}

void keyPressed() {
  if (keyCode == RIGHT) {
    ship.turn(accelR);
  }
  if (keyCode == LEFT) {
    ship.turn(accelL);
  }
  if (keyCode == UP) {
    for (float i = 0.1; i < maxSpeed; i+=0.05) {
      accelF -= i; 
      ship.forward(accelF);
    }
    forward = true;
    accelF =-0.05;
  }
}
class spaceShip {
  PVector pos = new PVector (width/2, height/2);
  float r = 10;
  float heading = 0;
  spaceShip() {
  }

   void render() {
    translate(pos.x, pos.y);
    noFill();
    stroke(255);
    rotate(heading);
    triangle(-r, r, r, r, 0, -r);
  }
  void turn(float angle) {
    heading += angle;
  }
  void forward(float yspeed) {
    float radians = (float)Math.PI * heading / 180;
    pos.y += Math.sin(radians) * yspeed;
    pos.y -= Math.cos(radians) *yspeed;
  }
  void edges() {
    if (pos.x > width)
      pos.x = 0;
    if (pos.x < 0)
      pos.x = width;
    if (pos.y > height)
      pos.y = 0;
    if (pos.y < 0)
      pos.y = height;
  }
}

```

---

<div class="post-metadata">

**Author:** ![timAllan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timallan/32/8946_2.png) [@timAllan](https://discourse.processing.org/u/timAllan)\
**Post date:** [May 5, 2020, 7:24pm UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456/4 "2020-05-05T19:24:00Z")

</div>

I’m a novice myself, but I notice your `forward()` method is only translating the `y` coordinate. If you want the ship to move with the heading, i.e. ‘move in the direction of its aim’, you’ll need to do something with the `x` coordinate.

```auto
  void forward(float yspeed) {
    float myRadians = (float)Math.PI * heading / 180;
    pos.y += Math.sin(myRadians) * yspeed;
    pos.y -= Math.cos(myRadians) *yspeed;
  }

```

I hope someone else can be of more help.

EDIT - In the code above, I changed your variable name `radians` to `myRadians` because `radians` is a built-in function in Processing. [https://processing.org/reference/radians\_.html](https://processing.org/reference/radians_.html)

---

<div class="post-metadata">

**Author:** ![noahbuddy](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@noahbuddy](https://discourse.processing.org/u/noahbuddy)\
**Post date:** [May 6, 2020, 2:36am UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456/5 "2020-05-06T02:36:12Z")

</div>

@timAllan, you are correct. Set the following line

```auto
pos.y -= Math.cos(myRadians) *yspeed;

```

to

```auto
pos.x += Math.cos(myRadians) *yspeed;

```

Also, @Arsenii, it appears that you are using both radians and degrees in your sketch.  
The values accelR and accelL are quite small for degrees.

Your overall logic is sound but your ship will have to spin a lot to orient to the correct direction and that would make the pilot dizzy. 😉

---

<div class="post-metadata">

**Author:** ![jsidell](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@jsidell](https://discourse.processing.org/u/jsidell)\
**Post date:** [February 26, 2025, 6:09pm UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456/6 "2025-02-26T18:09:08Z")

</div>

I am working on a similar problem. I am trying to make an asteroid type game where the ship shoots in the direction of the mouse. I do this by making the ship instance add bullet instances to an array list. In the bullet constructor I have a direction parameter. However, my calculations for the direction are incorrect. Here is my aimDirection() method:

```auto
float aimDirection(){
    float opposite = mouseX-x;
    float adjacent = mouseY-y;
    return atan( opposite/adjacent ) ; // tan(theta)=opposite/adjacent
  }

```

I then set the bullet vectors with this code:

```auto
hspeed = speed * cos( direction );
vspeed = speed * sin( direction );

void move(){
    x += hspeed;
    y += vspeed;
    display();
  }

```

The result is the bullet does not move in the correct direction. Can anyone find my mistake?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 27, 2025, 8:53am UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456/7 "2025-02-27T08:53:02Z")

</div>

> [@jsidell](#):
>
> `return atan( opposite/adjacent ) ; // tan(theta)=opposite/adjacent`

As `adjacent` approaches zero then `opposite/adjacent` approaches infinity and the sketch will crash. Also tan(a) == tan(a+180) so the angle returned might be 180° in the wrong direction. The solution is to use the `atan2(deltaY, deltaX)` function like this -

```auto
return atan2( adjacent , opposite) ;

```

It will return an angle in the range \> -π and \< +π radians ( \> -180° and \< +180°)

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [February 27, 2025, 2:53pm UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456/8 "2025-02-27T14:53:14Z")

</div>

`atan2( y, x )` returns `-PI` to `PI`. But, in any case, you can feed that angle back into `sin()` and `cos()` and it will be correct.

The angle returned has 0 to the right (towards `+x`) and increases from there in the `+y` direction which, for Processing, is down. If you have your ship oriented so that it points up and your shots are going off to the right instead, then you could either subtract `PI/2` from the angle returned from `atan2()` or you could swap the order of the inputs and flip a sign: `atan2( x, -y )` so that the angle has 0 along the up axis.

An alternative to using angles at all would be to treat your `(mouseX-x, mouseY-y)` as a vector, normalize it by dividing each term by its length, and multiplying that by the speed directly to get your velocity.

```auto
   float velx = mouseX - x;
   float vely = mouseY - y;
   float s = speed / sqrt( velx*velx + vely*vely );
   velx *= s;
   vely *= s;

   // in `move()`:
   x += velx;
   y += vely;

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 27, 2025, 3:18pm UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456/9 "2025-02-27T15:18:31Z")

</div>

> [@scudly](#):
>
> `atan2( y, x )` returns `-PI` to `PI`

True I will correct my post. Your other points are well taken though I recommend that users treat the positive X direction as 0° for _ **all** _ objects because it saves having to make corrections in the code.

---

<div class="post-metadata">

**Author:** ![jsidell](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@jsidell](https://discourse.processing.org/u/jsidell)\
**Post date:** [February 27, 2025, 8:15pm UTC](https://discourse.processing.org/t/concept-for-moving-in-a-direction-for-ship-in-game-asteroids/20456/10 "2025-02-27T20:15:53Z")

</div>

@scudly @quark  
Thanks guys! That fixed it. I am adding Asteroids to my high school Programming class and I’m pretty excited about it. Getting these controls right was a big step!
