Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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  
  is this the right way to rotate and shooting bullets ??  (Read 631 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-04-21 18:42:11 »

Hello,
I've made a little changes to the game am working on, and now it's like this ;

-move with arrow keys
-gun rotate toward mouse
-shoot bullet

now the problem is the bullets doesn't go to the correct target, plus, they all rotate together, even after the bullet is shot it still change the x and y direction

my board class :

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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
package clickOfGod;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.util.ArrayList;

import javax.swing.JPanel;

public class Board extends JPanel implements Runnable {
   // declaration
   private Ship ship;
   private Bullet bullet;
   private Gun gun;
   private Control mouse;

   private Thread loop;

   private ArrayList bullets;

   public static int mx, my;

   public static double angle;

   // constructor
   public Board() {
      init();
      setBackground(Color.lightGray);
      setDoubleBuffered(true);
      setFocusable(true);
      addMouseListener(mouse);
      addMouseMotionListener(mouse);
      this.addKeyListener(mouse);

   }

   // initialisation
   private void init() {
      ship = new Ship();
      bullet = new Bullet(0, 0, 0, 0);
      gun = new Gun(0, 0);
      bullets = gun.getBullets();

      angle = 0;

      mouse = new Control();
      mx = 0;
      my = 0;

      loop = new Thread(this);
      loop.start();

   }

   // painting
   public void paint(Graphics g) {
      super.paint(g);

      Graphics2D g2d = (Graphics2D) g;
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

      // drawing the ship
      g2d.draw(ship.getBounds());

      // rotating and drawing the gun
      g2d.rotate(angle, gun.getX() + 5, gun.getY() + 60);
      g2d.drawRect(gun.getX(), gun.getY(), gun.getW(), gun.getH());


      // creating the bullets arrayList
            ArrayList bs = gun.getBullets();
            for (int i = 0; i < bs.size(); i++) {
               Bullet tb = (Bullet) bs.get(i);
               g2d.draw(tb.getBounds());

            }
     

      g.dispose();
   }

   // game logic
   public void play() {

      ship.move(); // moving the ship
      gun.fire(50);// firing

      // placing the gun in the middle of the ship
      gun.setX(ship.getX() + ship.getW() / 3);
      gun.setY(ship.getY() - ship.getH() / 2);
      // calculating the angle rotation of the gun based on mouse coorindates
      // (my,mx)
      angle = Math.atan2(gun.getY() - my, gun.getX() - mx) - Math.PI / 2;

      //moving or removing the bullets
      ArrayList bs = gun.getBullets();
      for (int i = 0; i < bs.size(); i++) {
         Bullet tb = (Bullet) bs.get(i);
         
         if (tb.getTravel()) {
            tb.move(mx,my);
         } else {
            bs.remove(i);
         }
      }

   }

   // game loop
   public void run() {

      while (true) {
         repaint();
         play();

         try {
            loop.sleep(5);
         } catch (InterruptedException e) {

            e.printStackTrace();
         }
      }

   }

}



my bullet class :

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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
package clickOfGod;

import java.awt.Rectangle;

public class Bullet {

   private int x, y, width, height;
   private boolean travel;

   public Bullet(int x, int y, int w, int h) {
      this.x = x;
      this.y = y;
      this.width = w;
      this.height = h;

      travel = true;
   }

   public void setX(int x) {
      this.x = x;
      ;
   }

   public void setY(int y) {
      this.y = y;
      ;
   }

   public int getX(){
      return x;
   }
   
   public int getY(){
      return y;
   }
   
   
   public Rectangle getBounds() {
      return new Rectangle(x, y, width, height);
   }

   public boolean getTravel() {
      return travel;
   }

   public void setTravel(boolean travel) {
      this.travel = travel;
   }

   public void move(float tx, float ty) {

   if (x != tx || y != ty) {
         float dx = tx - x;
         float dy = ty - y;

         float len = (float) Math.sqrt(dx * dx + dy * dy);
         if (len != 0) {
            dx /= len;
            dy /= len;
         }

         float speed = 2f;
         dx *= speed;
         dy *= speed;

         x += dx;
         y += dy;

         travel = true;

      } else {
         travel = false;
      }

     
     
     
     
     
     
     
     
     
   }

}



my gun class
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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
package clickOfGod;

import java.util.ArrayList;

public class Gun {

   private int x, y, w, h, life;

   private ArrayList bullets;

   public Gun(int x, int h) {

      define();

   }

   public void define() {
      this.x = x;
      this.y = y;
      this.h = 60;
      this.w = 10;
      bullets = new ArrayList();
      life = 0;
   }

   public int getX() {
      return x;
   }

   public int getY() {
      return y;
   }

   public void setX(int x) {
      this.x = x;
   }

   public void setY(int y) {
      this.y = y;
   }

   public int getW() {
      return w;

   }

   public int getH() {
      return h;
   }

   public ArrayList getBullets() {
      return bullets;
   }

   public void fire(int load) {

      life += 1;
      if (life == load) {
         bullets.add(new Bullet(x, y, 10, 10));
         life = 0;

      }

   }

}


i think those are the only classes you need to understand my problem

thank you 

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Online HeroesGraveDev

JGO Kernel


Medals: 296
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-04-21 21:46:08 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
ArrayList bs = gun.getBullets();
      for (int i = 0; i < bs.size(); i++) {
         Bullet tb = (Bullet) bs.get(i);
         
         if (tb.getTravel()) {
           tb.move(mx,my);
         } else {
            bs.remove(i);
         }
      }


The problem is this.

You are setting the target each frame for bullets that have already been fired.
The velocity should be calculated once and saved.

Offline alaslipknot
« Reply #2 - Posted 2013-04-21 21:54:53 »

any specific solution please ?

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 138
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-04-21 21:56:01 »

any specific solution please ?
I'd suggest asking that after thinking for longer than 510 seconds.
Offline alaslipknot
« Reply #4 - Posted 2013-04-21 22:05:37 »

xD
sooorryy  Tongue

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
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.

toopeicgaming1999 (51 views)
2014-11-26 15:22:04

toopeicgaming1999 (44 views)
2014-11-26 15:20:36

toopeicgaming1999 (8 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (26 views)
2014-11-25 11:26:43

Gibbo3771 (23 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (75 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!