# Rotating LaserBeam

**URL:** <https://discourse.processing.org/t/rotating-laserbeam/12134>\
**Category:** Coding Questions\
**Created:** [June 17, 2019, 7:44pm UTC](https://discourse.processing.org/t/rotating-laserbeam/12134 "2019-06-17T19:44:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Not\_River](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@Not\_River](https://discourse.processing.org/u/Not_River)\
**Post date:** [June 17, 2019, 7:44pm UTC](https://discourse.processing.org/t/rotating-laserbeam/12134/1 "2019-06-17T19:44:46Z")

</div>

In my class we are trying to design a video game, and I’m trying to use code a laser beam that an enemy shoots. The laser is meant to start at the eye, and rotate 90 degrees in the direction of of player. In my code, I’ve been able to make the laser rotate like I want to, but the hit box of the image stays in place. This is the code I’ve gotten so far.

```auto
abstract class Projectile extends GameObject
{

  Projectile(float x, float y, float w, float h, float xSpeed, float ySpeed)
  {
    super(x, y, w, h);
    this.xSpeed = xSpeed;
    this.ySpeed = ySpeed;
  }
}

abstract class EnemyProjectile extends Projectile
{
  EnemyProjectile(float x, float y, float w, float h, float xSpeed, float ySpeed)
  {
    super(x, y, w, h, xSpeed, ySpeed);
  }

  void reactions()
  {
    for (int x = 0; x < collisions.size (); x++)
    {
      GameObject o = collisions.get(x);
      if (o instanceof Player)
      {
        die();
      }
      if (x<0 || x > width || y < 0 || y > height)
      {
        die();
      }
    }
  }
  void act()
  {
    super.act();
    if (x<0 || x > width || y < 0 || y > height)
    {
      die();
    }
  }
} 

class IceBeam extends EnemyProjectile
{
  private float cx;
  private float cy;
  private float r;
  private int beamTimer;
  private float rotate;
  IceBeam(float x, float y)
  {
    super(x, y, iceBeam.width, iceBeam.height, 0, 0);
    image = iceBeam;
    beamTimer = 0;
    rotate = 0;
    cx = x;
    cy = y;
  }
  void act()
  {
    super.act();
  }
  void render()
  {
    if (beamTimer < 75)
    {
      pushMatrix();
      for (GameObject o : objects)
      {
        if (o instanceof IceCreature)
        {
          translate(o.x+(iceCreature.width/2), o.y+(iceCreature.height/2) - 45);
        }
      }
      for (GameObject b : objects)
      {
        if (b instanceof Player)
        {
          if (b.x >= x)
          {
            rotate-=.02;
          } else if (b.x < x)
          {
            rotate+=.02;
          }
        }
      }
      rotate(rotate);
      image(iceBeam, x +(iceBeam.width/2), y + (iceCreature.height/2));
      popMatrix();
    }
    if (beamTimer >= 75)
    {
      die();
    }
    beamTimer++;
  }

  void reactions()
  {
    super.reactions();
  }
}

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 17, 2019, 9:22pm UTC](https://discourse.processing.org/t/rotating-laserbeam/12134/2 "2019-06-17T21:22:02Z")

</div>

First welcome to the community, hopefully you will find it enjoyable here.

Second as a quick fyi, you can format your code for improved readability using CTRL+SHIFT+C.

Not sure how you are updating your x,y values, normally they would have to match the angle of rotation so your hit box can be calculated accordingly.

Please see, [https://editor.p5js.org/paulgoux/sketches/0lem09rp-](https://editor.p5js.org/paulgoux/sketches/0lem09rp-) for an example;

it doesnt make use of the Pvector method as I wanted to understand the mechanics of the Pvector method, but the logic is still the same.

```auto
  this.update = function () {
      var mouse = new PVector(mouseX, mouseY);
      var mx = mouseX;
      var my = mouseY;
      var dy = (my - this.y)*0.0005;
      var a = 1.00;
      var dx = (mx - this.x )*0.0005;
      var dir = atan2(dy,dx);
      this.dir = (map(dir,-180,180,0,360)-1)*a;

      if(my>this.y){
          dy = -dy;
      }
      if(mx>this.x){
          dx = -dx;
      }
      this.ax = dx;
      this.ay = dy;

      if(this.vx>=4){
       this.vx = 4;
      }
      if(this.vx<=-4){
       this.vx = -4;
      }
      if(this.vy>=4){
       this.vy = 4;
      }
      if(this.vy<=-4){
       this.vy = -4;
      }
      this.vx += this.ax;
      this.vy += this.ay;

      this.x += this.vx*cos(this.dir);
      this.y += this.vy*sin(this.dir);
  };

```

note at the bottom the “x,y” values are updated with the direction.

```auto
  this.display = function () {

      var angle = this.dir;

      stroke(0, 0, 0);
      //strokeWeight(-1);
      //noStroke();
      fill(127, 127, 127);
      push();
      rectMode(CENTER);
      translate(this.x, this.y);
      rotate(angle+90);
      rect(15,0,10,45);
      rect(-15,0,10,45);
      fill(0, 72, 255);
      rect(0, 0, 30, 40,15);
      fill(255, 0, 0);
      rect(-10,10,10,10,50);
      fill(186, 180, 180);

      pop();
  };

```
