Hello
I am attempting to make a program that will shoot a bullet from a "gun" (just a line) and accurately simulate the trajectory (just gravity and the power from the gun). I did this using vectors. I also added a couple of features: a "power bar" that increases the power of the shot if you hold down the firing button, a keyboard interface, and some debug stuff. However, the gun does not fire properly.
Here is the code, commented heavily:
Quote:float angle, a, b, c, d, xco, yco; // Declaring variables
boolean throwvar, start, started;
PVector gravity, launch, sum;
String[] tex = new String[4];
void setup()
{
background(255, 255, 255); // Setting up backgroung, line width, and screen size
strokeWeight(5);
size(500, 500);
throwvar = false; // Initializing some important variables
angle = 0;
}
void draw()
{
background(255, 255, 255); // Clearing the screen
rect(50, 425, 400, 25); // Power bar (outline part)
fill(200, 255, 200); // Making non-outline portion of power bar green
rect(50, 425, 400*(d/10), 25); // Power bar (non-outline part)
noFill(); // Reversing color change
rotate(angle); // Rotating gun/aimer based on user-inputted angle
line(0, 0, 0, -50); // Making gun/aimer
start = true; // Telling the next "if" that the first frame has already occurred
if(start && !started) { // If statement to rotate aimer to 45 degrees on the first frame
angle += PI*0.75; // Indirect rotation
started = true; // Making this if statement invalid for subsequent executions of draw
}
if(throwvar && xco < 500 && yco > -500) { // If statement to launch pellet/bullet (also prevents pellet from going off screen)
gravity = new PVector(a, PI); // Initializing vectors
launch = new PVector(d, angle);
sum = PVector.add(gravity, launch);
xco = sum.x*cos(sum.y); // Converting the vectors (polar) to x/y coordinates
yco = sum.x*sin(sum.y);
tex[0] = str(xco); // Debug stuff
tex[1] = str(yco);
tex[2] = str(mouseX);
tex[3] = str(mouseY);
println(join(tex, ", "));
point(xco, yco); // Drawing bullet
a += 0.98; // Increasing velocity downwards (gravity is acceleration)
if(d >= 0) { // Making bullet speed decay over time
d -= 0.1;
}
}
if(xco >= 500 && yco <= -500) { // Resetting program when pellet hits the edge of screen
a = 0;
d = 0;
throwvar = false;
}
}
void keyPressed() // Stuff to make aimer change angle when you pres arrow keys as well as firing machanism
{
if(key == CODED && !throwvar) {
if(keyCode == UP && angle > PI/2) {
angle -= 0.01;
}
if(keyCode == DOWN && angle < PI) {
angle += 0.01;
}
}
if((key == ENTER || key == RETURN) && d < 10) {
d += 0.1;
}
}
void keyReleased() // Pretty self explanatory
{
if(key == ENTER || key == RETURN) {
throwvar = true;
}
else if(key == BACKSPACE || key == DELETE) {
throwvar = false;
}
}
Please copy and paste this into a processing sketch to see what I mean. To fire, press and hold enter until the desired power level is reached (think gunbound-style firing). Change the angle of the gun using the up and down arrow keys. IMPORTANT: try adjusting the firing angle to see just how glitchy this code is. You have to restart the program to fire again. Please look over my code, although I don't see any problems with it, you might. Thank you in advance.