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);
}