0

I'm using Java Processing 3 and making a two player tank game. Below is my code for a turret. Currently i have the turret's aim following the mouse in tDir, i wanted to be able to use the up and down arrows to move the aim upwards and downwards from 0 to 90 degrees.

How would I go about this? Thanks.

/*
Uzair
*/

PVector mPos; //mouse position
PVector tPos; //position of turret
PVector tDir; //the firing direction of the turret
 int gravMult = 3;

void setup() {
  size(1200, 600);
  init();
}

void init() {
  //PVector initializations
  mPos = new PVector(); //zero til mouse x and y exist
  tPos = new PVector(width/8, height);
  tDir = new PVector(); //
}
void draw() { 
  //clear last frame
  background(100,100,140);

  //check keys to see if there is new key input for turret
  if (keyPressed){
    if (key == 'w'){
      tDir.y -= 10;
    }
    else if (key == 's'){
      tDir.y += 10;
    }
  }

  mPos.set(mouseX, mouseY);
  tDir = PVector.sub(mPos, tPos);
  tDir.normalize();
  tDir.mult(50);

  //draw
  fill(255);  
  ellipse(tPos.x, tPos.y, 40, 40);
  strokeWeight(5);
  line(tPos.x, tPos.y, tPos.x + tDir.x, tPos.y + tDir.y);
  fill(255, 0, 0);
  ellipse(tPos.x + tDir.x, tPos.y + tDir.y, 10, 10);
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • 1
    Java isn't Processing. You're gonna get a whole bunch of folk who are going to be in Java-mode instead of Processing-mode, so I'm going to encourage you to remove references to Java, since this isn't a Java-specific problem. – Makoto May 22 '17 at 22:58

2 Answers2

1

Stack Overflow isn't really designed for general "how do I do this" type questions. It's for specific "I tried X, expected Y, but got Z instead" type questions. That being said, I'll try to help in a general sense.

You need to break your problem down into smaller steps and take on those steps one at a time. Can you create a separate standalone sketch that just prints something to the console when you press the arrow keys?

Separately from that sketch, can you create another sketch that stores a position or angle in variables at the top of your sketch? Draw the scene using those variables, and again get that working by itself.

When you have them working by themselves, you can think about combining them by changing the sketch-level variables when the user presses the arrow keys.

If you get stuck on a specific step, please post a MCVE in a new question and we'll go from there. Good luck.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
1

aim upwards and downwards from 0 to 90 degrees

This sounds like incrementing an angle/rotation instead of incrementing the y position as you are currently.

You need to do polar coordinates(angle/radius) conversion to cartesian coordinates (x,y).

This can be be done using the formula:

x = cos(angle) * radius
y = sin(angle) * radius

in your case

tDir.x = cos(angle) * 50;
tDir.y = sin(angle) * 50;

...although it's worth keeping in mind PVector already provides this functionality via fromAngle().

Here's your code tweaked to use this idea:

PVector mPos; //mouse position
PVector tPos; //position of turret
PVector tDir; //the firing direction of the turret
 int gravMult = 3;
//angle in radians
float angle = 0;

void setup() {
  size(1200, 600);
  init();
}

void init() {
  //PVector initializations
  mPos = new PVector(); //zero til mouse x and y exist
  tPos = new PVector(width/8, height * .75);
  tDir = new PVector(); //
}
void draw() { 
  //clear last frame
  background(100,100,140);

  //check keys to see if there is new key input for turret
  if (keyPressed){
    if (key == 'w'){
      angle -= 0.1;
      tDir = PVector.mult(PVector.fromAngle(angle),50);
    }
    else if (key == 's'){
      angle += 0.1;
      tDir = PVector.mult(PVector.fromAngle(angle),50);
    }
  }else if(mousePressed){
    mPos.set(mouseX, mouseY);
    tDir = PVector.sub(mPos, tPos);
    tDir.normalize();
    tDir.mult(50);
  }
  //draw
  fill(255);  
  ellipse(tPos.x, tPos.y, 40, 40);
  strokeWeight(5);
  line(tPos.x, tPos.y, tPos.x + tDir.x, tPos.y + tDir.y);
  fill(255, 0, 0);
  ellipse(tPos.x + tDir.x, tPos.y + tDir.y, 10, 10);
}

You might also find this answer helpful.

George Profenza
  • 50,687
  • 19
  • 144
  • 218