//*************************************************************************************************** // Leading with two tanks //*************************************************************************************************** import damkjer.ocd.*; //import processing.opengl.*; import noc.*; Camera camera1; Vector3D posTank1,posTank2,posBullet,facingVector, directionV; Vector3D patrolPosition1,patrolPosition2; Vector3D bulletDirection; // tank1 float speedTank1 = 1.0; float directionX = 1.0; // tank2 float angle, angleBullet; float bulletVelocity = 3.5; boolean recoil = false; boolean shoot = false; boolean bulletLife = false; boolean leading = true; void setup() { size( 300, 300, P3D); camera1 = new Camera(this, 200, -450, 700); posTank1 = new Vector3D(-100.0, 0.0, 100.0); posTank2 = new Vector3D(100.0, 0.0, 400.0); posBullet = new Vector3D( 100.0, -10.0, 400.0 ); //facingVector = new Vector3D(0.0, 0.0, 1.0); patrolPosition1 = new Vector3D(-100.0, 0.0, 200.0); patrolPosition2 = new Vector3D(300.0, 0.0, 200.0); bulletDirection = new Vector3D( 1.0,1.0,1.0); } void draw() { background(204); noStroke(); lights(); // calculations directionV = new Vector3D(posTank1.x-posTank2.x,0,posTank1.z-posTank2.z); directionV.normalize(); angle = atan2(directionV.x, directionV.z ); //----------------------------------------------------------- // tank1 (Yellow team) //----------------------------------------------------------- fill(255,255,0); pushMatrix(); if( patrolPosition1.x < posTank1.x ) { directionX *=-1; } if( patrolPosition2.x > posTank1.x ) { directionX *=-1; } posTank1.x = posTank1.x -( speedTank1 * directionX); translate(posTank1.x, posTank1.y, posTank1.z); box(40, 20, 50); popMatrix(); //----------------------------------------------------------- // tank2 (Green team) //----------------------------------------------------------- fill(0,255,0); pushMatrix(); translate(posTank2.x, posTank2.y, posTank2.z); rotateY(angle); box(40, 20, 50); popMatrix(); //----------------------------------------------------------- // shoot //----------------------------------------------------------- //posBullet fill(255,0,0); pushMatrix(); if( bulletLife == true ) { posBullet.x += bulletDirection.x* bulletVelocity; posBullet.z += bulletDirection.z* bulletVelocity; translate(posBullet.x, posBullet.y, posBullet.z); rotateY(angleBullet); // orientacio }else{ translate(posTank2.x, posBullet.y, posTank2.z); } //sphere(8); box(40, 20, 50); popMatrix(); if(recoil == true) { bulletLife = false; recoil = false; } if(shoot == true) { // calculation direction bullet if( leading ){ // movement with leading float distance = Vector3D.distance( posTank1, posTank2); float timeToArrive = distance / bulletVelocity; // target future move float moveTarget = speedTank1* timeToArrive; println( "t: "+timeToArrive +" | m:"+moveTarget+" | d:"+distance); Vector3D leading = new Vector3D(posTank1.x-(posTank2.x+(moveTarget*directionX)),0,posTank1.z-posTank2.z); leading.normalize(); bulletDirection = new Vector3D(leading.x, 0.0,leading.z); }else{ // movement without leading bulletDirection = directionV; bulletDirection = new Vector3D( bulletDirection.x,0.0,bulletDirection.z); } println("shoot!"); posBullet.x = posTank2.x; posBullet.z = posTank2.z; bulletLife = true; shoot = false; } //----------------------------------------------------------- // some outputs //----------------------------------------------------------- //println( posTank1.x +" | " +posTank1.z ); //println( directionV.x +" | " +directionV.z ); //println( degrees(angle) ); camera1.feed(); } void keyPressed() { if (keyCode == UP) { posTank2.z +=1; } if (keyCode == DOWN) { posTank2.z -=1; } if (keyCode == RIGHT) { posTank2.x +=1; } if (keyCode == LEFT) { posTank2.x -=1; } if (keyCode == SHIFT) { shoot = true; } if (keyCode == RETURN || keyCode == ENTER ) { recoil = true; } }