Java-Gaming.org Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (563)
Games in Android Showcase (151)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 2 3 [4] 5 6 ... 10
 31 
 on: 2015-03-26 17:10:01 
Started by Ask_Reply - Last post by KevinWorkman
Step 1: Switch to using a JPanel inside a JFrame. This will handle double buffering for you.
Yes your advice has helped a lot....Flickering has reduced but is still there...please help!!!

You've got three more steps of advice left. :p

After you've followed them, please post an updated MCVE and we'll go from there.

 32 
 on: 2015-03-26 17:09:29 
Started by Ask_Reply - Last post by Ask_Reply
Step 1: Switch to using a JPanel inside a JFrame. This will handle double buffering for you.
Yes your advice has helped a lot....Flickering has reduced but is still there...please help!!!

 33 
 on: 2015-03-26 17:08:27 
Started by MrPork - Last post by MrPork
This gets the direction of the gun and rotates it:

1  
GunDirection = (int) Math.toDegrees(Math.atan2(TargetShip.getOriginY()  - OriginPositionY, TargetShip.getOriginX() - OriginPositionX));


This makes the bullet:
1  
bulletList.add(new Bullet( (int) GunDirection , (int) OriginPositionX, (int) OriginPositionY, Faction, BulletTexture));


This sets the bullets position right when it spawns:

1  
2  
3  
[code]
this.setBulletX((int) (bulletX + Math.cos(Math.toRadians(bulletDirection))));
      this.setBulletY((int) (bulletY + Math.sin(Math.toRadians(bulletDirection))));
[/code]

And this moves the bullet in the direction its supposed to go:

1  
2  
bulletX += Math.cos(Math.toRadians(bulletDirection))*bulletSpeed;
      bulletY += Math.sin(Math.toRadians(bulletDirection))*bulletSpeed;


These are all the code snippets of where any math is used for this, I don't understand what else you need, unless you want the actual class file of the Mother ship and bullet, then here   Sad :

MotherShip
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  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
package com.me.eclipse.entities;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class Ship_MotherShip implements Ship {

   public int Faction = 0, Hull = 500, upKeep;
   public Vector2 Position = new Vector2();
   private TextureRegion Ship_Base, Ship_Top, Ship_Gun;
   private Texture ShipSelect, BulletTexture;
   private int BaseDirection, TopDirection, GunDirection = 90;
   private int Width, Height, OriginX, OriginY, OriginPositionX, OriginPositionY, shootDelay = 0;
   private int GoalX, GoalY, Speed = 3;
   private boolean isHovered = false, isSelected = false;
   private Ship TargetShip;
   private Rectangle rect;
   private ArrayList<Bullet> bulletList;

   public Ship_MotherShip(int x, int y, TextureRegion Ship_Base,
         TextureRegion Ship_Top, TextureRegion Ship_Gun, Texture ShipSelect, ArrayList<Bullet> bulletList) {
      this.Ship_Base = Ship_Base;
      this.Ship_Top = Ship_Top;
      this.Ship_Gun = Ship_Gun;
      this.ShipSelect = ShipSelect;
      this.Position.x = x;
      this.Position.y = y;
      this.GoalX = x;
      this.GoalY = y;
      this.Hull = 500;
      this.upKeep = 0;
      this.Faction = 0;
      this.Width = Ship_Base.getRegionWidth();
      this.Height = Ship_Base.getRegionHeight();
      this.OriginX  = Width/2;
      this.OriginY = Height/2;
      this.OriginPositionX = (int) Position.x + OriginX;
      this.OriginPositionY = (int) Position.y + OriginY;
      this.bulletList = bulletList;
     
      rect = new Rectangle();
      rect.setWidth(Ship_Base.getRegionWidth());
      rect.setHeight(Ship_Base.getRegionHeight());
     
      BulletTexture = new Texture(Gdx.files.internal("data/Ships/Bullet.png"));
   }

   @Override
   public void render(SpriteBatch batch) {
      System.out.println("Rednering");
      batch.draw(Ship_Base, Position.x, Position.y, OriginX, OriginY, Width,
            Height, 1, 1, BaseDirection, false);
      batch.draw(Ship_Top, Position.x, Position.y, OriginX, OriginY, Width,
            Height, 1, 1, TopDirection, false);

      batch.draw(Ship_Gun, Position.x, Position.y, OriginX, OriginY, Width,
            Height, 1, 1, GunDirection, false);

      if (isSelected) {
         batch.draw(ShipSelect, Position.x, Position.y);
      }

   }

   @Override
   public void renderGlobal(Texture GreenGlobal, Texture RedGlobal,
         Texture WhiteGlobal, SpriteBatch batch) {
      if (Faction == 0) {
         batch.draw(WhiteGlobal, Position.x - 380, Position.y - 280);
      }
      if (Faction == 1) {
         batch.draw(GreenGlobal, Position.x - 380, Position.y - 280);
      }
      if (Faction == 2) {
         batch.draw(RedGlobal, Position.x - 380, Position.y - 280);
      }

   }

   @Override
   public void update(Vector3 MousePos, Sound select) {
      this.OriginPositionX = (int) Position.x + OriginX;
      this.OriginPositionY = (int) Position.y + OriginY;
      rect.setPosition(Position.x, Position.y);
      BaseDirection += 1;
      shootDelay += 1;
      targetEnemy();
     
      if(shootDelay >= 150){
         Shoot();
         shootDelay -= shootDelay;
      }

      /* Check for mouse hover and selection */
      if (MousePos.x < Position.x + Width && MousePos.x > Position.x
            && MousePos.y < Position.y + Height && MousePos.y > Position.y) {
         isHovered = true;
      } else {
         isHovered = false;
      }

      if (isHovered && isSelected == false && Faction == 1
            && Gdx.input.isButtonPressed(Buttons.LEFT)) {
         select.play();
         setSelected(true);
      }
      if (isHovered == false && Gdx.input.isButtonPressed(Buttons.LEFT)) {
         setSelected(false);
         ;
      }

      /* Check for mouse move actions */
      if (isSelected == true && Gdx.input.isButtonPressed(Buttons.RIGHT)) {
         moveTo((int) MousePos.x, (int) MousePos.y);
      }

      /* Move to Goal */
      if (Position.x < GoalX) {
         Position.x += Speed;
      }
      if (Position.x > GoalX) {
         Position.x -= Speed;
      }
      if (Position.y < GoalY) {
         Position.y += Speed;
      }
      if (Position.y > GoalY) {
         Position.y -= Speed;
      }

   }

   @Override
   public void findEnemy(Ship ship) {

      if (TargetShip == null && ship.getFaction() != this.Faction) {
         this.TargetShip = ship;
         System.out.println("Target Acquired");
      }

   }

   @Override
   public void targetEnemy() {
      if (TargetShip != null) {
         GunDirection = (int) Math.toDegrees(Math.atan2(TargetShip.getOriginY()  - OriginPositionY, TargetShip.getOriginX() - OriginPositionX));
         System.out.println(GunDirection);
      }
   }

   @Override
   public void moveTo(int x, int y) {
      this.GoalX = x;
      this.GoalY = y;

   }

   @Override
   public int getFaction() {

      return Faction;
   }

   @Override
   public void setFaction(int faction) {
      this.Faction = faction;

   }

   @Override
   public int getHull() {

      return Hull;
   }

   @Override
   public void setHull(int hull) {
      this.Hull = hull;

   }

   @Override
   public int getUpkeep() {

      return upKeep;
   }

   @Override
   public void setUpkeep(int upkeep) {
      this.upKeep = upkeep;

   }

   @Override
   public Vector2 getPosition() {

      return Position;
   }

   @Override
   public void setPosition(Vector2 position) {
      this.Position = position;

   }

   public int getGoalX() {
      return GoalX;
   }

   public void setGoalX(int goalX) {
      GoalX = goalX;
   }

   public int getGoalY() {
      return GoalY;
   }

   public void setGoalY(int goalY) {
      GoalY = goalY;
   }

   public boolean isSelected() {
      return isSelected;
   }

   public void setSelected(boolean isSelected) {
      this.isSelected = isSelected;
   }
   

   @Override
   public int getSpeed() {

      return Speed;
   }

   @Override
   public Rectangle getRectangle() {
      return rect;
   }
   
   @Override
   public void Shoot(){
      bulletList.add(new Bullet( (int) GunDirection , (int) OriginPositionX, (int) OriginPositionY, Faction, BulletTexture));
   }

   @Override
   public int getOriginX() {
      return OriginPositionX;
   }

   @Override
   public int getOriginY() {
      return OriginPositionY;
   }

}


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  
68  
69  
70  
71  
72  
73  
74  
package com.me.eclipse.entities;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;

public class Bullet {

   private int bulletDirection;
   private float bulletSpeed = 3f;
   private int bulletX, bulletY;
   private int Faction = 0;
   private boolean isAlive;
   private Texture bulletTexture;
   private Rectangle rect;
   
   public Bullet(int bulletDirection, int bulletX, int bulletY, int Faction, Texture bulletTexture){
      this.bulletDirection = bulletDirection;
      this.setBulletX((int) (bulletX + Math.cos(Math.toRadians(bulletDirection))));
      this.setBulletY((int) (bulletY + Math.sin(Math.toRadians(bulletDirection))));
      this.bulletTexture = bulletTexture;
      this.setFaction(Faction);
      rect = new Rectangle();
      rect.setSize(12,12);
   }
   
   public void render(SpriteBatch batch){
      batch.draw(bulletTexture, bulletX, bulletY);
     
   }
   
   public void update(){
      rect.x = bulletX;
      rect.y = bulletY;
      bulletX += Math.cos(Math.toRadians(bulletDirection))*bulletSpeed;
      bulletY += Math.sin(Math.toRadians(bulletDirection))*bulletSpeed;
   }

   public boolean isAlive() {
      return isAlive;
   }

   public void setAlive(boolean isAlive) {
      this.isAlive = isAlive;
   }

   public int getBulletX() {
      return bulletX;
   }

   public void setBulletX(int bulletX) {
      this.bulletX = bulletX;
   }

   public int getBulletY() {
      return bulletY;
   }

   public void setBulletY(int bulletY) {
      this.bulletY = bulletY;
   }
   
   public Rectangle getRectangle(){
      return rect;
   }

   public int getFaction() {
      return Faction;
   }

   public void setFaction(int faction) {
      Faction = faction;
   }
}





 34 
 on: 2015-03-26 16:58:46 
Started by MrPork - Last post by KevinWorkman
I highly doubt the conversion between radians and degrees is the culprit. More likely it's the wonky math you've got in there. But it's pretty hard to debug without looking at the same small code base, which is why I'm trying to work from the examples I posted.

 35 
 on: 2015-03-26 16:56:23 
Started by MrPork - Last post by MrPork

 36 
 on: 2015-03-26 16:54:25 
Started by MrPork - Last post by KevinWorkman
Here's another little example that might be closer to what you're looking for. It shows some "ships" travelling at random headings, and shows the "nose" of each ship as a red circle. You should be able to combine this example with the previous example to achieve the effect you're going for:

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  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Main extends JPanel{

   List<Ship> ships = new ArrayList<Ship>();

   public static void main(String... args) {
      new Main();
   }

   public Main(){
      JFrame frame = new JFrame("Bullet Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.add(this);

      Timer t = new Timer(33, new ActionListener(){

         @Override
         public void actionPerformed(ActionEvent e) {
            for(int i = ships.size()-1; i >= 0; i--){
               ships.get(i).step();
            }
            repaint();
         }

      });

      t.start();

      addMouseListener(new MouseAdapter(){
         public void mousePressed(MouseEvent e){
            ships.add(new Ship(e.getX(), e.getY()));
         }
      });



      setBackground(Color.BLACK);

      frame.setSize(500, 500);
      frame.setVisible(true);

   }



   public void paintComponent(Graphics g){
      super.paintComponent(g);
      for(int i = ships.size()-1; i >= 0; i--){
         ships.get(i).paint(g);
      }
   }

   class Ship{

      double x;
      double y;
      double heading;
      double speed = 5;
      double radius = 10;

      public Ship(double x, double y){
         this.x = x;
         this.y = y;
         heading = Math.random()*360;

      }

      public void paint(Graphics g){
         g.setColor(Color.WHITE);
         g.fillOval((int)(x-radius), (int)(y-radius), (int)radius*2, (int)radius*2);


         double noseX = x+Math.cos(Math.toRadians(heading))*radius;
         double noseY = y+Math.sin(Math.toRadians(heading ))*radius;

         g.setColor(Color.RED);
         g.fillOval((int)(noseX-radius/2), (int)(noseY-radius/2), (int)radius, (int)radius);

      }

      public void step(){
         
         heading += -10 + Math.random()*20;


         double deltaX = Math.cos(Math.toRadians(heading))*speed;
         double deltaY = Math.sin(Math.toRadians(heading ))*speed;

         x += deltaX;
         y += deltaY;

         if(x < 0){
            x = getWidth();
         }
         if(x > getWidth()){
            x = 0;
         }

         if(y < 0){
            y = getWidth();
         }
         if(y > getWidth()){
            y = 0;
         }

      }
   }
}

 37 
 on: 2015-03-26 16:45:38 
Started by MrPork - Last post by MrPork
I stopped subtract and adding 90 since I made the image point down. It's still doing the same.

 38 
 on: 2015-03-26 16:43:21 
Started by MrPork - Last post by KevinWorkman
For the positioning: in theory you already know the position of the tip of the gun, right? Just put the bullet there.

If you don't know the position, then you can use the same logic you use the figure out the deltaX and deltaY, just use the length of the gun instead of the speed.

For the angle, stop adding and subtracting 90! :p

 39 
 on: 2015-03-26 16:31:01 
Started by MrPork - Last post by MrPork
Well for the first question, I want to position the bullet in the middle of the Ship.

For the second: If, my gun is pointing directly up, the bullet will go quite a bit to the left instead, and not even pass through the barrel.

And for the third: It does exactly what I want to do, except of course, position the bullet in the correct area of the ship, and shoot in the direction that its pointing.

 40 
 on: 2015-03-26 16:26:05 
Started by MrPork - Last post by KevinWorkman
I guess my problem is just actually positioning the bullet, not the direction it goes. H'm. I'm not exactly sure how I would put it into that code, and doing so would just show you the exact 4 lines I showed you before except in a more complicated manner.  Huh

What do you mean by positioning the bullet? Where do you want to position it?

Nope, its the angle. The way the gun is pointing does not equal the way the bullet is going at all.

I'm not sure what you mean by this.

That code is almost exactly like my code though, I have no idea what to put in there.

You don't necessarily have to add anything. Let me put it this way: what does my code not do that you want it to?

Pages: 1 2 3 [4] 5 6 ... 10
 
wxwsk8er (49 views)
2015-03-20 15:39:46

Fairy Tailz (42 views)
2015-03-15 21:52:20

Olo (26 views)
2015-03-13 17:51:59

Olo (27 views)
2015-03-13 17:50:51

Olo (33 views)
2015-03-13 17:50:16

Olo (40 views)
2015-03-13 17:47:07

ClaasJG (29 views)
2015-03-10 11:36:42

ClaasJG (36 views)
2015-03-10 11:33:01

Pippogeek (45 views)
2015-03-05 14:36:23

Pippogeek (37 views)
2015-03-05 13:56:12
How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

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