# Tank Controls Help

**URL:** <https://discourse.processing.org/t/tank-controls-help/13043>\
**Category:** Coding Questions\
**Created:** [July 29, 2019, 4:18pm UTC](https://discourse.processing.org/t/tank-controls-help/13043 "2019-07-29T16:18:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samira](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@Samira](https://discourse.processing.org/u/Samira)\
**Post date:** [July 29, 2019, 4:18pm UTC](https://discourse.processing.org/t/tank-controls-help/13043/1 "2019-07-29T16:18:44Z")

</div>

I’m creating a tank for a game I am making. I want the tank to move in the direction it is facing. How can I do that?

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 29, 2019, 4:46pm UTC](https://discourse.processing.org/t/tank-controls-help/13043/2 "2019-07-29T16:46:44Z")

</div>

Hey There!

Some further code/ explanation would be awesome !

---

<div class="post-metadata">

**Author:** ![Samira](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@Samira](https://discourse.processing.org/u/Samira)\
**Post date:** [July 29, 2019, 4:49pm UTC](https://discourse.processing.org/t/tank-controls-help/13043/3 "2019-07-29T16:49:49Z")

</div>

Here’s my code…

```auto
function setup() {
	createCanvas(500, 500, WEBGL);
}

let xPos = 40;
let yPos = 0;
let bDir = 0;
let gDir = 0;

let up = 38;
let left = 37;
let right = 39;
let down = 40;
let a = 65;
let d = 68;

let z = 45;

function draw() {
	background(255);
	//z += 0.01;
	rotateX(45);
	rotateZ(z);

	tank(xPos, yPos, 0, bDir, gDir);

	if (keyIsDown(up)) {
		xPos += 2;
	}
	
	if (keyIsDown(down)) {
		xPos -= 2;
	}
	
	if (keyIsDown(right)) {
		bDir += 0.02;
	}
	
	if (keyIsDown(left)) {
		bDir -= 0.02;
	}
	
	if (keyIsDown(a)) {
		gDir += 0.02;
	}
	
	if (keyIsDown(d)) {
		gDir -= 0.02;
	}
	
}

```

this defines the tank function…

```auto
function tank (x, y, z, body, gun) {
	translate(x, y, z);
	rotateZ(body);
	
	box(30, 20, 10);
	
	translate(0, 0, -7);
	
	box(20, 20, 5);
	triPrism(-15, -10, 3, 5, 20, 5);
	triPrism(15, -10, 3, -5, 20, 5);
	
	
	
	translate(0, 0, 14);
	rotate(gun);
	
	
	box(10, 10, 5);
	
	rotateZ(80);
	translate(0, 10);
	
	cylinder(2, 20, 5, 5);
	
	translate(0, -10);
	rotateZ(280);
	
	rotate(-gun);
	translate(0, 0, -7);
	
	rotateZ(body);
	translate(-x, -y, -z);
};

```

---

<div class="post-metadata">

**Author:** ![Samira](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@Samira](https://discourse.processing.org/u/Samira)\
**Post date:** [July 29, 2019, 6:13pm UTC](https://discourse.processing.org/t/tank-controls-help/13043/4 "2019-07-29T18:13:48Z")

</div>

Nevermind I was able to figure out a formula.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 29, 2019, 9:17pm UTC](https://discourse.processing.org/t/tank-controls-help/13043/5 "2019-07-29T21:17:47Z")

</div>

Can you share your code please?

Sounds interesting!!!

Chrisir
