Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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  
  shooting is working but ...  (Read 834 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-04-27 01:24:11 »

Hello,
it's almost a week now, i've been trying to shoot bullets depending on the gun direction, and i think this is the best result i got so far,
am able to shoot and each bullet go to its own direction depending on the gun rotation, the only thing is that the bullet directions are wrong :p
it is supposed to go to the up-right corner but instead :


i really don't know why am facing that much problem with rotation  Undecided
please can you help

codes :

bullet :
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  
package clickOfGod;

import java.awt.Rectangle;

public class Bullet {

   private int x, y, width, height, s;
   double a;
   private boolean travel;

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

   }

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

   }

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

   public void setA(double a) {
      this.a = a;
   }

   public int getX() {
      return x;
   }

   public int getY() {
      return y;
   }

   public double getA() {
      return a;
   }

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


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


   public void move() {

      x += 2 * Math.cos(a);
      y += 2 * Math.sin(a);

   }

}


Gun :
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  
package clickOfGod;

import java.util.ArrayList;

public class Gun {

   private int x, y, w, h, life;
   private double a;
   static int bx, by;
   private ArrayList bullets;
   static boolean firing, inc, dec;
   static double now;

   public Gun(int x, int y, double a) {

      define();

   }

   public void define() {
      this.x = x;
      this.y = y;
      this.h = 60;
      this.w = 10;
      this.a = a;
      bullets = new ArrayList();
      life = 0;
      firing = false;
      now = 0;
      inc = false;
      dec = false;
   }

   public int getX() {
      return x;
   }

   public int getY() {
      return y;
   }

   public double getA() {
      return a;
   }

   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 setA(double a) {
   
      this.a=a;
   }

   public void fire(int load) {

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

      }

   }

}


Board :
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  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
package clickOfGod;

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

import javax.swing.JPanel;

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

   private ArrayList bullets;

   public static boolean shoot;

   public static int mx, my;
   public static double angle;
   public static float yv, xv = 0;

   // 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();
      gun = new Gun(0, 0, 0);
      bullet = new Bullet(0, 0, 0, 0, 0);
      bullets = gun.getBullets();

      angle = 0;

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

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

      shoot = false;
   }

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

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

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

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

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

         g2d.rotate(tb.getA(), tb.getX() + 5, tb.getY() + 60);
         g2d.draw(tb.getBounds());
         g2d.setTransform(old);
      }

      g.dispose();
   }

   // game logic
  public void play() {
      // moving the ship
     ship.move();
      // firing
     if (shoot) {
         gun.fire(10);

      }
      // 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();
         } else {
            bs.remove(i);
         }
      }

      // 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;
      gun.setA(angle);

   }

   // game loop
  public void run() {

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

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

            e.printStackTrace();
         }
      }

   }

}


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."
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #1 - Posted 2013-04-27 01:27:31 »

Dont use
1  
Math.atan2()

Smiley
Offline alaslipknot
« Reply #2 - Posted 2013-04-27 01:41:44 »

Dont use
1  
Math.atan2()


where  Huh
please am really tired of the angle thing  Tongue
i need a clear example to understand it once for all
can anybody do that 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 Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2013-04-27 01:44:48 »

Let me start off by saying that using ata2 is perfectly fine, it may not be the best solution, but there's certainly nothing wrong with it.


this is your problem:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
int x, y;

   public void move() {

      // implicitly, your values are cast back to ints here,
     // leading to a huge loss of accuracy
     x += 2 * Math.cos(a);
      y += 2 * Math.sin(a);

   }


making 'x' and 'y' floats would solve this problem.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline alaslipknot
« Reply #4 - Posted 2013-04-27 01:50:06 »

making 'x' and 'y' floats would solve this problem.

thank you but how can i draw the rectangle with floats x & y ?

PS :
my angles are always something like this  0.7150018600912116
is there a way to make it look normal ? (from 0 to 360 )

thanx

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline Phased
« Reply #5 - Posted 2013-04-27 01:52:40 »

cast the x and y variables to a int in the drawing

1  
g2d.drawRect((int)gun.getX(), (int)gun.getY(), gun.getW(), gun.getH());
Offline Axeman

Senior Member


Medals: 7



« Reply #6 - Posted 2013-04-27 02:02:50 »

cast the x and y variables to a int in the drawing

...and add 0.5 to round it off to the nearest int

1  
g2d.drawRect((int) (gun.getX() + 0.5f), (int) (gun.getY() + 0.5f), gun.getW(), gun.getH());
Offline alaslipknot
« Reply #7 - Posted 2013-04-27 02:07:23 »

[quote ]
cast the x and y variables to a int in the drawing
[/quote]

Quote
...and add 0.5 to round it off to the nearest int


i tried that, the result is more strange, bullets always go to the right and slow down when it goes to the top

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

Senior Member


Medals: 7



« Reply #8 - Posted 2013-04-27 02:13:26 »

Eh? Perhaps i misunderstood. Did you get a stranger result when changing x,y to floats? The cast to int was for the drawing only.
Offline alaslipknot
« Reply #9 - Posted 2013-04-27 02:25:45 »

Eh? Perhaps i misunderstood. Did you get a stranger result when changing x,y to floats? The cast to int was for the drawing only.

yes i change the x and y to float inside the bullet function, and i cast to int in the drawing part

PS :
can you guys please right an example of rotating and moving toward rotation angle ??
please i really need to understand how it works

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."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2013-04-27 02:26:22 »

my angles are always something like this  0.7150018600912116
is there a way to make it look normal ? (from 0 to 360 )
you really need to be aware of the difference between radians and degrees if you're doing anything related to angles.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Agro
« Reply #11 - Posted 2013-04-27 02:33:28 »

Radians are measured based off a unit circle with the radius of 1. There are 2*pi radians in a circle, which is equal to 360 degrees, so there are pi radians in a triangle. Use Math.toDegrees(radians) to convert from radians to degrees. Its a much better presentable format. Smiley

Offline alaslipknot
« Reply #12 - Posted 2013-04-27 13:15:58 »

guys ...
am really stacked here
PLEASE
anyone could explain the whole process of rotating a rectangle and make it move toward the rotation angle
pleaaaaaaaaaase

thank you very much 

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline SHC
« Reply #13 - Posted 2013-04-27 13:25:50 »

Isn't the vertical axis reversed?

1  
2  
3  
4  
5  
6  
7  
public void move() {

    x += 2 * Math.cos(a);
-   y += 2 * Math.sin(a);
+   y += 2 * Math.sin(-a);

}


I think this works now.

Offline alaslipknot
« Reply #14 - Posted 2013-04-28 02:21:49 »

thank you guys for your help,
i finally made it  Grin
the main problem was that i didn't know the "head" if my squares  Roll Eyes

"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.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (27 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!