Skip to content
Sign upLog in
← Back to Community

Make bullets fire in a specific direction.

Profile icon
HackingGo306

You can kinda think of this as part 2 of This Question.

So first of all, I would like to point out that the question is kind of unspecific. And I am too lazy to make a repl explaining it. I would put pictures instead.

This is what I have right now:

Screen Shot 2021-02-12 at 4.16.49 PM

This is what I would like to have:

Screen Shot 2021-02-12 at 4.16.10 PM

This is what I can think of so far:

Screen Shot 2021-02-12 at 4.17.09 PM

Question:

Can anyone tell me how to make the bullets fire off away from the cannon instead of to your mouse.

Voters
Profile icon
HackingGo306
Comments
hotnewtop
Profile icon
Coder100

just change the theta
for example:

let theta = <angle>; let barrel1 = theta + Math.PI / 2; let barrel2 = theta + Math.PI; let barrel3 = theta - Math.PI / 2;

why not play diep.io instead of making one

Profile icon
HackingGo306

@Coder100

I figured it out on my own. Thanks for trying to help though.

Profile icon
Coder100

how did you do it?

@HackingGo306

Profile icon
HackingGo306

@Coder100

In some lines of the code, a length is added to the cannons and then multiplied by the angle. I changed the length to the distance between the mouse and the tank. This gave me what I wanted (picture 3)

Profile icon
19wintersp

What you need to do is calculate the point which is the same distance in the direction modified from the direction towards the mouse cursor. In your code, you are already calculating the start and end points of the cannons, and so you simply need to extend this by modifying the formula.

Profile icon
HackingGo306

@19wintersp

Thanks for the support, but I managed to figure it out on my own.

Profile icon
gregory54

Why can't you just fire bullets the right way yourself by practicing? You can read more about this on sri.com.my if you want. It really isn't that difficult if you ask me.

Profile icon
HackingGo306

@gregory54
Thanks for replying, but this question has already been answered and is quite old. Right now I am able to fully understand how to get this to work, along with more complicated ways to implement this technique. Back then I was not as experienced at programming, so I can see why you don't think this is a difficult thing to do.

Side Note: I only briefly read the site you sent to me (as I no longer need support on this question), yet all I was able to find is about fires. Do you have a more specific link?