Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  shoot interval  (Read 2005 times)
0 Members and 1 Guest are viewing this topic.
Offline Zushii

Senior Newbie





« Posted 2011-03-30 19:06:39 »

Hello Smiley

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)?
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-03-30 19:46:21 »

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

Offline Zushii

Senior Newbie





« Reply #2 - Posted 2011-03-30 19:53:31 »

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.
 
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline aazimon
« Reply #3 - Posted 2011-03-30 20:14:52 »

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.
Offline Zushii

Senior Newbie





« Reply #4 - Posted 2011-03-30 20:38:54 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
if (firing && weap.getAmmo() > 0) {
         timer += elapsedTime;
         
         if (timer >= weap.getFPS()) {
            shoot();
            weap.decreaseAmmo();
            weap.PlaySound();
            timer = 0;
         }
         
         
      }


Offline aazimon
« Reply #5 - Posted 2011-03-30 23:00:05 »

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?
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-03-31 01:51:05 »

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.

Offline philfrei
« Reply #7 - Posted 2011-03-31 04:07:17 »

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?

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Zushii

Senior Newbie





« Reply #8 - Posted 2011-03-31 16:55:57 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
 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;
    }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
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:
Online kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2011-03-31 17:06:02 »

1  
       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?

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


Just guessing.

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zushii

Senior Newbie





« Reply #10 - Posted 2011-03-31 17:44:38 »

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

Online kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #11 - Posted 2011-03-31 17:46:00 »

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

Offline Zushii

Senior Newbie





« Reply #12 - Posted 2011-03-31 18:02:59 »

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


The missile is added this way.

1  
2  
3  
4  
5  
6  
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:

1  
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.
Online kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #13 - Posted 2011-03-31 18:56:11 »

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! Smiley

Kev

Offline Zushii

Senior Newbie





« Reply #14 - Posted 2011-03-31 19:03:47 »

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.
Online kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #15 - Posted 2011-03-31 19:07:47 »

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

Kev

Offline Zushii

Senior Newbie





« Reply #16 - Posted 2011-03-31 19:17:22 »

EDIT:

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

Senior Newbie





« Reply #17 - Posted 2011-03-31 20:13:59 »

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.)
Online kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #18 - Posted 2011-03-31 20:18:00 »

Whats code for drawing your player and missle?

Kev

Offline Zushii

Senior Newbie





« Reply #19 - Posted 2011-03-31 20:41:59 »

1  
2  
3  
4  
5  
6  
7  
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();


1  
2  
3  
4  
5  
6  
7  
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();
        }
Offline Zushii

Senior Newbie





« Reply #20 - Posted 2011-04-01 18:07:38 »

Can nobody help? Haven't fixed the problem yet. Sad
Offline philfrei
« Reply #21 - Posted 2011-04-01 21:32:53 »

Poor boo! Cry

Things I would check or verify:

1) that the graphic's center matches the point you actually want to shoot from (e.g., if the graphic is asymmetrical);

2) that the translational rotation is doing exactly what you think it is (you could be rotating around a different point than you are assuming);

3) that the 'context' of the values used for drawing the shooter matches the 'context' used for the raygun path draw (the [x,y] invoked before an affine transform doesn't necessarily match the position on the screen after the transform).

There are lots of ways to screw up, here. Verify all the steps.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #22 - Posted 2011-04-01 21:36:48 »

Ah you stole those points right from my keyboard Tongue

Offline Zushii

Senior Newbie





« Reply #23 - Posted 2011-04-01 22:28:56 »

You actually were right with the second one.

Ive got so much code , that I totally didn't see that I was using the x,y pos of the player and not the center, to calculate the angle.

Thanks Cheesy

So so tired.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (24 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!