shoot interval

Hello :slight_smile:

I’m having some problems with my gun intervall timer.

It was all fine until I added switchable weapons. Now the intervall is read from the Weapon object and now my swing timer won’t work anymore.

Do you have any tips? Maybe a method to calculate the time between shots via elapsed time (i have that variable)?

Could you give more information on what exactly you are doing?

Okay.

The player can shoot a gun, the gun - a Object - has a int which determines how fast the gun can shoot.

I used a Timer with a fixed rate. But now I have a few different guns, ie smg, shotgun, pistol with different rates of fire.

I can’t use the timer because, the timer will initilalise before the weapon object is added to the players list. So it will always be zero, resulting in uncontrollable rapidfire.

Not knowing how your code is set up. You should call and update to the current gun of the player for each tick of the timer. With the static frame rate, you know how much time between ticks. The gun object would need to keep track of the ticks, and when it reaches the fire rate, it resets the tracking of ticks in the gun object.

The Problem ist doing it that way is that when I stop shooting the timer stays where it was. and when I press fire again it will take some time until it shoots. Which is bad. It’s supposed to fire immediatly and then only every so-and-so seconds.

if (firing && weap.getAmmo() > 0) {
			timer += elapsedTime;
			
			if (timer >= weap.getFPS()) {
				shoot();
				weap.decreaseAmmo();
				weap.PlaySound();
				timer = 0;
			}
			
			
		}

Sounds like the gun doesn’t know it stop firing and doesn’t reset. When you stop pressing the button, can you reset the gun’s timer?

From what I understand, you manually change the delay of the Swing Timer to the delay specified by the gun? I suggest having the timer run in a continuous loop, calling an update(long deltaTime) method in the gun, where it keeps track of the firing.

Just an idea. I haven’t ever tried to implement this situation before but maybe the following is not too inefficient.

Have a datetime variable stored with your Object/Gun that holds the “next” time it is OK to fire. Then, with each shot, update that variable by that Object’s refire interval.

For example, if you want to only allow 5 shots a second, when you shoot, add 200ms to “Now” and store it. Then consult this time variable (compared to “Now”) before firing again.

Maybe?

Thank for the tips.

by reseting the timer when he stops shooting, I fixed the problem.

Now I have a different question. I want the player to aim via mouse and therefor I need to calculate the angle of the player to the mouse.

 public double getAngleOfTwoPoints(double x1, double y1,double x2, double y2) {
    	
    	double DeltaX = x2-x1;
    	double DeltaY = y2-y1;
    	    	
    	double angle = Math.toDegrees(Math.atan2(DeltaY, DeltaX)+ 89.65f);
    	
    	return angle;
    }
public void mouseMoved(MouseEvent e) {
		
		      mx = e.getX();
		      my = e.getY();
		      
		      angle = vk.getAngleOfTwoPoints(x, y, mx, my);
		      
		      e.consume();
	}
	
	 public void mouseDragged( MouseEvent e ) {  // called during motion with buttons down
	      mx = e.getX();
	      my = e.getY();
	      angle = vk.getAngleOfTwoPoints(x, y, mx, my);
	      firing = true;	      
	      e.consume();
	   }
	
	 public void mouseReleased( MouseEvent e ) {  // called after a button is released
	      firing = false;
	      e.consume();
	   }
	
	 public void mousePressed( MouseEvent e ) {  // called after a button is pressed down
		  mx = e.getX();
	      my = e.getY();
		  angle = vk.getAngleOfTwoPoints(x, y, mx, my);
	      firing = true;
	      e.consume();
	   }

thats how I calculate it. But for some reason there is a huge offset to my Mouse X,Y pos and the angle he shoots at.

Heres a pic to illustrate my problem:


       double angle = Math.toDegrees(Math.atan2(DeltaY, DeltaX)+ 89.65f);

Looks a bit dodgy. Is the 89.65 meant to be 90 degrees? More over shouldn’t it be outside the brackets?


       double angle = Math.toDegrees(Math.atan2(DeltaY, DeltaX)) + 90;

Just guessing.

Kev

Thanks that helped, but there is still a minor offset.

Can you show the code how you:

a) Draw the line
b) Move the bullets

and how that relates to “angle” and mx and my.

Kev

missiles.add(new Missile(x+width/2,y+height/2,angle));

The missile is added this way.


public void move() {
        
    	x += Math.sin(Math.toRadians(angle)) * MISSILE_SPEED;
	y -= Math.cos(Math.toRadians(angle)) * MISSILE_SPEED;
    	
    }

This is how they are moved.

and the line was just drawn via paint^^ but to give you the idea:

g.drawLine(mx, my, (int)player.getCenterX(), (int)player.getCenterY());

I calculate the angle from the players center to the mouse position. and then give the angle calculated to the missile object.

Ah ha! Then I suspect your player sprite isn’t quite central on the line. Try replacing the player sprite termporarily with a straight line down the middle of a sprite. It’ll probably match that - you’ll need to adjust for the shape of sprite… maybe! :slight_smile:

Kev

nope. Its lined up correctly.

weird. It seems that at certain angles its fine, but at some others it has a small offest of about 10-20pixels or so.

Tis strange, is the X/Y of the player always right? You might want to fill a circle at that location to check?

Kev

EDIT:

I actually noticed that the missile does not leave the player correctly sometimes. I will look into this.

The center of the player seems to be slightly not the same all the time. Though this irritates me since the player is 40x40 px so there is no weird center when rotating.

But it seems that when you rotate the player via mouse. the center is not the same center as the players center.

Heres pic for reference:

as you can see the missile is displaced from the beginning. The oval represents the center of the player. (the center of the oval would be the exact center of the player.)

Whats code for drawing your player and missle?

Kev

Graphics2D gRV = (Graphics2D) bufferStrategy.getDrawGraphics();
        if (player.isvisible()) {
        	gRV.rotate(Math.toRadians(player.getAngle()),player.getX()+player.getImage().getWidth(null)/2,player.getY()+player.getImage().getHeight(null)/2);  // Rotate the image by 1 radian.
        	gRV.drawImage(player.getImage(),(int)player.getX(),(int)player.getY(), canvas);
        	//gRV.drawOval((int)player.getCenterX()-5,(int)player.getCenterY()-5,10,10);
        }
    	gRV.dispose();
for (int i=0; i<mPlayer.size(); i++) {
        	Graphics2D gRV = (Graphics2D) bufferStrategy.getDrawGraphics();
        	Missile m = (Missile)mPlayer.get(i);
        	gRV.rotate(Math.toRadians(m.getAngle()),m.getCenterX(),m.getCenterY());
        	gRV.drawImage(m.getImage(),(int)m.getX(),(int)m.getY(),canvas);
        	gRV.dispose();
        }