We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I'm quite new to programming in Processing and I'm making a simple shooting game. Basically there's a tank and the user is able to rotate the gun/cannon and shoot, but I don't know how to make the missile appear in the right location and at the right angle after rotating the cannon. I will add another tank when I get the shooting mechanics right. This is the code that I've got so far:
PVector tankLocation;
float theta = 0.01;
void setup() {
size(1200, 800);
tankLocation = new PVector(80, 575);
}
void draw() {
noStroke();
background(135, 206, 235);
fill(34, 139, 34);
ellipse(520, 800, 1600, 450);
fill(85, 107, 47);
rotate(-0.12);
rect(tankLocation.x, tankLocation.y, 80, 40); // Tank
translate(135, 584);
rotate(theta);
rect(0, 0, 70, 10); // Gun
if (keyPressed == true) {
if (keyCode == UP) {
theta = theta - 0.05;
if (theta<-0.49) { // Prevents rotating the gun too much
theta = -0.49;
}
}
if (keyCode == DOWN) {
theta = theta + 0.05;
if (theta>=0.01) {
theta = 0.01;
}
}
if (key == 's') {
}
}
}
How can I alter it so I could shoot missiles at an angle by pressing the key 's'? Help would be greatly appreciated. :)
Answers
The first step is to be able to shoot a bullet. You need to think about what information you need to manipulate a bullet. You need to work in the actual concept how to generate the bullet. Write a list of things you need to create and draw the bullet for the first time, and then what you need to update in each draw frame to make the bullet move.
Check the following example to have an idea of what comes next: https://processing.org/examples/bounce.html
Kf
I've managed to alter the program so the bullets appear and move after the 's' key is pressed. I might have overcomplicated the code though, I had to read a few manuals to get it working and I stumbled upon some things that were new for me. But there are slight problems, the bullet position is irrelevant to the position of the gun and I still don't really know how to make the bullets appear in the same position as the gun. This is the current code:
You have done a great job. However the next part requires more work. Have you checked shiffman's videos before? Try this: https://www.youtube.com/results?search_query=shiffman+pvector
It will be great for you to become familiar with PVector and basic concepts of vector operations. Then you need about transformations. Check this: https://processing.org/tutorials/transform2d/
You can also have a read at https://processing.org/tutorials/objects/ as it will keep your bullet as an object, an entity, in your program and you will be able to manipulate it. Working with objects is not needed, and you can skipped for now.
Kf
Thanks for the help so far, I've made it so it shoots at the right angle (I will alter the code later so the movement won't be linear but I think I already have some ideas on how to do that so that's not an issue). But then I tried adding in another tank (which would be controlled by another player) and for some reason when I try to rotate the left tank's gun, the other gun also rotates around a weird center.EDIT: Panic over (for now). Solved it by using push and pop matrix.