Shoot bullet at angle

edited November 2016 in Questions about Code

Hello,

As the title says I want to shoot bullets from an object (the triangle) and I know how to shoot and accelerate the bullet straight forward by just increasing Y every time but now I wanted to make a game where you're supposed to shoot enemies that are approaching the center, but I was wondering how I could shoot the bullet in at the angle that the player is and when the bullet is shot

int debug;
int basesize = 250;
int speedfactor = 1;
int direction = 0;
int growth;
int r = 200;
float t = PI+PI/2;

void setup() {
  frameRate(90);
  fullScreen();
  noStroke();
  smooth();
}

void draw() {
  background(255);

  stroke(0);
  fill(255);
  ellipseMode(CENTER);

  if (growth == 1) basesize += 2 * speedfactor;
  if (growth == 0) basesize -= 2 * speedfactor;

  if (basesize > 275) growth = 0;
  if (basesize < 225) growth = 1;

  ellipse(width/2, height/2, basesize, basesize);

  t += direction * speedfactor * 0.03;

  int x = (int)(width/2+r*cos(t));
  int y = (int)(height/2+r*sin(t));

  pushMatrix();
  translate(x, y);
  rotate(t-PI/2);
  stroke(0);
  fill(255);
  triangle(-20, -10, 0, +50, +20, -10);
  popMatrix();

  textAlign(LEFT);
  if (debug > 1) debug = 0;
  if (debug == 1) {
    fill(0);
    textSize(12);
    text("health: " + "unknown", 10, 15); 
    text("speedfactor: " + speedfactor, 10, 30); 
    text("Framerate: " + round(frameRate), 10, 45);
    text("Framecount: " + frameCount, 10, 60);
    text("Runtime: " + millis()/1000 + " sec", 10, 75);
    text("Base size: " + basesize, 10, 90);
    text("Mouse cordinates:", 10, 115); 
    text("X:  ", 30, 130);
    text(mouseX, 45, 130);
    text("Y:  ", 30, 145);
    text(mouseY, 45, 145);
    text("Screensize: ", 10, 175);
    text("X:  ", 30, 190);
    text(width, 45, 190);
    text("Y:  ", 30, 205);
    text(height, 45, 205);
    text("Player cordinates:", 10, 235); 
    text("X:  ", 30, 250);
    text(x, 45, 250);
    text("Y:  ", 30, 265);
    text(y, 45, 265);
  }
}

void keyPressed() {
  if (key == 'c' || key == 'C' ) debug += 1;
  if (key == 'a' || key == 'A' || keyCode == DOWN) direction = -1;
  if (key == 'd' || key == 'D' || keyCode == UP) direction = 1;
}

void keyReleased() {
  if (key == 'a' || key == 'A' || keyCode == DOWN) direction = 0;
  if (key == 'd' || key == 'D' || keyCode == UP) direction = 0;
}

This is what I've got so far

I hope someone can help me

Answers

  • Thank you, after looking into the code I think I understands the basics but when I try to remake the code and turn al the circles into 1 square and don't spawn them from the circle that is spinning around and just spawn them randomly but I can't get it to work. Do you maybe know another code that is a bit more barebone without multiple circles etc so it's a bit easier to understand the code?

    Thank you anyway :)

  • Answer ✓

    links to code with no explanation are more likely to flood and confuse the OP rather than help.

    quickly, if something is being fired at an angle then the x position changes according to the speed * the cos of the angle, the y position with the speed * the sin of the angle.

    your x and y in lines 33 and 34 are doing this already. that would be a good start point for a bullet. the bullet's velocity (x and y) will be similar. with every step just add on the velocity to the current position.

    your code has no shooting yet. try adding some.

  • (those 3 links posted above give me two untrusted connections:

    "Your connection is not secure The owner of studio.processingtogether.com has configured their web site improperly. To protect your information from being stolen, Firefox has not connected to this web site."

    and an archived sketch that doesn't run)

  • edited November 2016

    Those 3 links posted above give me two untrusted connections

    My SlimJet (Chromium-based) browser can't even connect to the running sketch there! :(
    However, Firefox-based browsers can run them as long as we authorize it. :ar!
    Problem I guess the site is using an unsafe & deprecated cipher. :-<

  • The two SketchPad sketches are still running fine for me on Chrome 49 (an old version, I know).

    Re: OpenProcessing -- I do also sometimes share links to archived sketches on OpenProcessing. We should probably be clear about saying "this won't run online, but view the source code and try it out in PDE."

  • @koogs thank you so much!

    I got it working now

  • continued here, although i'm not sure why it just couldn't continue with this one...

    https://forum.processing.org/two/discussion/19353/pvector-bullets

This discussion has been closed.