Tank Controls Help

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?

2 Likes

Hey There!

Some further code/ explanation would be awesome !

Here’s my code…

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…

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

Nevermind I was able to figure out a formula.

Can you share your code please?

Sounds interesting!!!

Chrisir

1 Like