Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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 494 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-04-21 20: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 

Am trying ...
hoping that it works
Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #1 - Posted 2013-04-21 23: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 23:54:53 »

any specific solution please ?

Am trying ...
hoping that it works
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-04-21 23: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-22 00:05:37 »

xD
sooorryy  Tongue

Am trying ...
hoping that it works
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.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!