# Using Vectors to Make a Bullet shoot to where I click with my mouse

**URL:** <https://discourse.processing.org/t/using-vectors-to-make-a-bullet-shoot-to-where-i-click-with-my-mouse/30915>\
**Category:** Coding Questions\
**Created:** [June 25, 2021, 11:00pm UTC](https://discourse.processing.org/t/using-vectors-to-make-a-bullet-shoot-to-where-i-click-with-my-mouse/30915 "2021-06-25T23:00:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunter9649](https://avatars.discourse-cdn.com/v4/letter/h/41988e/32.png) [@Hunter9649](https://discourse.processing.org/u/Hunter9649)\
**Post date:** [June 25, 2021, 11:00pm UTC](https://discourse.processing.org/t/using-vectors-to-make-a-bullet-shoot-to-where-i-click-with-my-mouse/30915/1 "2021-06-25T23:00:53Z")

</div>

So I have been trying to make a game like a shooter similar to [diep.io](http://diep.io) but its like a zombie thing. I could not figure out vectors because I’m too small brain. Anyone wanna help me. Here is the code I have so far.  
\<class Gunner {  
int x = 250;  
int y = 250;

void gun(float rotation) {  
pushMatrix();  
translate(x, y);  
rotate(rotation);  
fill(0, 0, 0);  
rect(0, 0, 15, 10);  
fill(0, 255, 0);  
ellipse(0, 0, 20, 20);  
popMatrix();  
}

int getX() {  
return x;  
}

int getY() {  
return y;  
}

void setX(int xPos){  
x = xPos;  
}

void setY(int yPos) {  
y = yPos;  
}  
}

boolean[] keys;

void setup() {  
size(500,500);  
keys = new boolean[4];  
keys[0] = false;  
keys[1] = false;  
keys[2] = false;  
keys[3] = false;  
}

int x;  
int y;  
float xDistance;  
float yDistance;  
float rotationAngle;  
Gunner gunner = new Gunner();

void draw() {  
background(255, 255, 255);  
gunner.gun(rotationAngle);  
if(keys[0]) {  
y = gunner.getY();  
if(y \> 0) {  
y–;  
gunner.setY(y);  
gunner.gun(rotationAngle);  
}  
}  
if(keys[1]) {  
x = gunner.getX();  
if(x \< 500) {  
x++;  
gunner.setX(x);  
gunner.gun(rotationAngle);  
}  
}  
if(keys[2]) {  
y = gunner.getY();  
if(y \< 500) {  
y++;  
gunner.setY(y);  
gunner.gun(rotationAngle);  
}  
}  
if(keys[3]) {  
x = gunner.getX();  
if(x \> 0) {  
x–;  
gunner.setX(x);  
gunner.gun(rotationAngle);  
}  
}  
}

void mouseMoved() {  
xDistance = mouseX - gunner.getX();  
yDistance = mouseY - gunner.getY();  
rotationAngle = atan2(yDistance, xDistance);  
gunner.gun(rotationAngle);  
}

void keyPressed() {  
if (key == ‘w’ || key == ‘W’) {  
keys[0] = true;  
}  
if (key == ‘d’ || key == ‘D’) {  
keys[1] = true;  
}  
if (key == ‘s’ || key == ‘S’) {  
keys[2] = true;  
}  
if (key == ‘a’ || key == ‘A’) {  
keys[3] = true;  
}  
}

void keyReleased() {  
if (key == ‘w’ || key == ‘W’) {  
keys[0] = false;  
}  
if (key == ‘d’ || key == ‘D’) {  
keys[1] = false;  
}  
if (key == ‘s’ || key == ‘S’) {  
keys[2] = false;  
}  
if (key == ‘a’ || key == ‘A’) {  
keys[3] = false;  
}  
}

void mouseClicked() {

}\>

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [June 26, 2021, 12:08am UTC](https://discourse.processing.org/t/using-vectors-to-make-a-bullet-shoot-to-where-i-click-with-my-mouse/30915/2 "2021-06-26T00:08:05Z")

</div>

https://www.youtube.com/embed/videoseries?list=PLRqwX-V7Uu6aFlwukCmDf0-1-uSR7mklK&wmode=transparent&rel=0&autohide=1&showinfo=1&enablejsapi=1

Hello @Hunter9649

This playlist may be of interest. **A great overview on vectors** , very clearly explained!

Also, if you could please format your code, it will be much easier for us to read. 😌  
🤓

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 26, 2021, 7:51am UTC](https://discourse.processing.org/t/using-vectors-to-make-a-bullet-shoot-to-where-i-click-with-my-mouse/30915/3 "2021-06-26T07:51:17Z")

</div>

[studio.ProcessingTogether.com/sp/pad/export/ro.9D4K1FIWJ7Kxc](http://studio.ProcessingTogether.com/sp/pad/export/ro.9D4K1FIWJ7Kxc)  
[studio.ProcessingTogether.com/sp/pad/export/ro.91kLmk61vAOZp](http://studio.ProcessingTogether.com/sp/pad/export/ro.91kLmk61vAOZp)  
`studio.ProcessingTogether.com/sp/pad/export/ro.91nVQMnL$v06L`
