Rotating LaserBeam

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.

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();
  }
}
1 Like

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- 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.

  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.

  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();
  };
2 Likes