Hi guys,
A few days ago i posted a question about a bubbleshooter game i wanted to make. I’ve now managed to make the bullet move towards my mouse position, but the angle keeps changing if i move the mouse. I want the angle to be fixed after I press the mouse button.
Hopefully you could help me!
<Ball b;
ArrayListbullets;
void setup(){
size(840,840);
b = new Ball();
bullets = new ArrayList();
bullets.add(new Bullet());
}
void draw(){
background(0);
b.display();
//Bullets
Bullet bullet = bullets.get(0);
bullet.display();
bullet.move();
}
class Ball{
float x= width/2;
float y=height;
Ball(){
}
void display(){
fill(255);
ellipse(x,y,50,50);
}
class Bullet{
float x =width/2;
float y=height;
boolean doOnce =false;
Bullet(){
}
void display(){
fill(255);
ellipse(x,y,12.5,12.5);
}
void move(){
float distancex = (mouseX-b.x);
float distancey = abs(mouseY-height);
float angle = atan((distancey/distancex));
if (mousePressed == true){
doOnce = true;
}
if (doOnce == true){
if (angle>0){
x+= 3cos(angle);
y-= 3sin(angle);
}
if (angle<0){
x-=3cos(angle);
y-=3-sin(angle);
}}}
}