I’m trying to shoot a moving ball. Most of it works (ball moves, hit is detected). Except the the movement of bullet. I can’t get it moving.
void setup(){
size(1000,800);
}
Projectile bullet = new Projectile();
Enemy rock = new Enemy();
boolean flag = true;
void draw()
{
background(100,100,100);
stroke(0,100,0);
circle(rock.pos.x, rock.pos.y, rock.radius*2);
stroke(100,0,0);
circle(bullet.pos.x, bullet.pos.y, bullet.radius*2);
if (mousePressed && (mouseButton == LEFT))
{
//bullet.set_vel(mouseX, mouseY);
while (flag){
stroke(100,0,0);
circle(bullet.pos.x, bullet.pos.y, bullet.radius*2);
bullet.update();
println("Bullet position " + str(bullet.pos.x) + " " + str(bullet.pos.y));
hit(rock,bullet);
flag = false;
}
}
rock.update();
hit(rock,bullet);
}
void hit(Enemy rock, Projectile bullet){
float dx, dy, distance;
dx = rock.pos.x - bullet.pos.x;
dy = rock.pos.y - bullet.pos.y;
distance = sqrt((sq(dx) + sq(dy)));
if (distance < (rock.radius + bullet.radius)){
rock.respawn();
bullet.respawn();
println("Rock hit bullet");
}
}
void keyPressed()
{
if (key == ESC)
{
flag = false;
exit();
}
}
=======================
class Enemy{
PVector pos;
PVector vel;
float radius;
float speed;
Enemy(){
//pos = new PVector(width * 0.75, height/4);
pos = new PVector(300, 600);
vel = new PVector(round(random(-2,2)), round(random(-2,2)));
radius = 60;
speed = 5;
}
void update(){
pos.x = pos.x + vel.x;
pos.y = pos.x + vel.y;
//print("Inside bullet update");
out_of_bounds();
}
boolean out_of_bounds(){
if ((pos.x+radius) > width || (pos.y + radius) > height || (pos.x - radius )< 0 || (pos.y - radius ) <0)
{
respawn();
}
return false;
}
void respawn(){
pos.x = random(0,width - radius - 1);
pos.y = random(0, height - radius -1);
vel.x = random(-2,2);
vel.y = random(-2,2);
print("Rock respawn");
}
}
===========================
class Projectile{
PVector pos;
PVector vel;
float speed;
float radius;
PVector target; //for debug
Projectile()
{
pos = new PVector(500,400);
vel = new PVector(round(random(-5,5)), round(random(-5,5))); //(round(random(-5,5))), round(random(-5,5))
speed = 10;
radius = 8;
target = new PVector();
disp_comment("Creating bullet "+ str(vel.x)+ " " +str(vel.y));
}
void update()
{
pos.x = pos.x + vel.x;
pos.y = pos.x + vel.y;
out_of_bounds();
disp_comment("Bullet updated " + str(pos.x) + " " + str(pos.y) + " ");
}
void set_vel(int tx, int ty){
// testing purpose
//stores target position
target.x = tx;
target.y = ty;
//set position back to the centre
pos.x = width/2;
pos.y = height/2;
disp_comment("Set Vel " + str(pos.x) + " " + str(pos.y) + " ");
}
void out_of_bounds(){
if ((pos.x+radius) > width || (pos.y + radius) > height || (pos.x - radius )< 0 || (pos.y - radius ) <0)
{
respawn();
disp_comment("Bullet Out of bounds ");
}
}
void respawn(){
pos.x = width/2;
pos.y = height/2;
vel.x = 0;
vel.y = 0;
disp_comment("Respawning bullet ");
}
void disp_comment(String s )
{
println(s);
}
}
========================
I fail to understand why the bullet is not redrawn. Any help is appreciated.