Shooting Spawns Unwanted Extra Projectiles

Hi! I was user “GiantSpider” who made two other posts a few weeks ago, but my login didn’t work for the new forum so I’m just using Google now.

I want to try to resolve my issue without posting all my code if possible - better for both you and me. But here is the “shoot qi blast” method where I create the projectile object:

Blockquote
(not sure how to use this new forum’s code quoting thingie)
protected void shoot_qb(int x, int y, double dir, Mob mob, Mouse mouse) {
if (qb_charging == false) {
Projectile p = new QiBlast(x, y, dir, mob, mouse);
projectiles.add§;
}
}

For some reason if I click the mouse to shoot blasts quickly sometimes I instead shoot one, two, or even three extra blasts. The weird thing - which may be a hint to the solution - is that while one of the blasts always fires in the direction the character is facing, the other ones fire off in random directions. I don’t even know how that’s possible given how my code is put together. In the update function for the qi blast I have:

Blockquote
dx = getx - (Game.screen_width / 2);
dy = gety - (Game.screen_height / 2);
angle = Math.atan2(dy, dx);
nx = speed * Math.cos(angle);
ny = speed * Math.sin(angle);

and

Blockquote
x += nx;
y += ny;

getx and gety are the mouse pointer locations on the screen. The player is in the middle of the screen so you can surmise what the rest means. I’ll probably post more code but I think this should be enough for someone more experienced to know what is going wrong. I’ll keep at it but I’ve been stuck on this for awhile. You are only supposed to charge and shoot one blast at a time, and it shoots toward the mouse pointer from the center of the screen. You can have multiple blasts active if they have already been fired, but that’s not what’s happening here.

Why oh why do people always convert a vector to an angle and immediately back to a vector…
Angles/sin/cos/tan/atan are never EVER actually necessary/needed.
Change to this exactly equivalent code:

dx = getx - (Game.screen_width / 2);
dy = gety - (Game.screen_height / 2);
invlen = 1.0f / Math.sqrt(dx*dx + dy*dy);
nx = speed * dx*invlen;
ny = speed * dy*invlen;

Maybe you query the mouse button state at every game logic or frame tick and do not use “mouse events”.
And maybe all your projectiles update their position always based on the current mouse position and not based on their initial direction vector that they got when they were initially created.

2 Likes

It must be not using mouse events. Wow, for a bug that had me tied up for so long, the solution was pretty simple. But now I have no more excuse to binge play Neverwinter Nights :frowning:

Thank you for helping me though!

Okay, it may be a little more complicated than I had hoped but I’m revisiting my code with the update frame uptick vs. event thinking on my mind. One odd thing is that the error seems to grow - I get progressively more extra qi blast projectiles as I go along with flying around shooting in my empty testing area. I am not sure that the event versus update thing is the problem though, which is why I wanted to explain again about the extra qi blasts that fire off in random directions.

Can’t really tell all that much from the code you posted. A projectile gets created when the qb_charging boolean is set to false. When does it get set to false? When does it get set to true? Seems like it gets set to false when it shouldn’t be.

What behaviour are you expecting exactly? Shoot one projectile for each click? Keep shooting projectiles while you hold the mouse down? What happens currently?

When you click, it charges a single qi blast and when you release it fires it. That is what is supposed to happen but I frequently get extra, unwanted projectiles.

Oddly, the extra projectiles start out fully charged and they fire off in random directions. The qi blast that is supposed to be created charges and then fires off in the correct direction (toward the mouse). I figure these must be clues to figure this out.

I am using threading, but I only have a single thread called “display” that I create in the main Game class. Could threading be the problem somehow? I had that thought after I changed something that seemed to get rid of the extra projectiles but then after a few shots I lost the ability to fire projectiles at all. I don’t know what could cause that to happen unless there is more going on under the hood than I know - such as how threads work.