Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Linear Motion on: 2008-11-23 18:05:28
Thanks.  I just thought it was some error in my code, due to the fact that an increase in fps somehow made the results more accurate, and because of the fact that it was constantly off by approximately the same ammount.

This hasn't caused me any trouble, but I just wanted to make sure that the method itself wasn't flawed.  The longer the program runs, the more the number goes off, so for implementation it might be better to just update the displacement to the correct value every second.
2  Game Development / Newbie & Debugging Questions / Linear Motion on: 2008-11-23 15:46:26
In order to program something interesting with 2-d motion, I figured that I should start with the absolute basics... linear motion.  I typed up this program today to try it out, and it doesn't completely work.  All the calculations it performs at run time turn out slightly differently from what it should be.  I noticed that this error margin decreases when I up the fps, but it only does that, decreases it.  Does anyone know what I should change to make the game displacement equal to the one it gets from the equation: x=x0 + v0*t + (1/2)a*t^2?
my source code:
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  
  import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import javax.swing.event.*;
   import java.util.*;
   import java.applet.*;


    public class LinearMotion extends Applet implements MouseMotionListener
   {
      Graphics g;
      Image offscreen;
      Dimension dim;
      int mouseX, mouseY;
      int mouseDX, mouseDY;
     
      double initialVelocity=45;// m/s
      double velocity=initialVelocity;
      double acceleration=-6;// m/s^2
      double startingPosition=20.0;
      double position =startingPosition;// m
      int meterLength=5;// m/10*pixel
     
      boolean backwards=true;//can velocity become negative? false if friction, etc
      boolean finished=false;//if backwards = true, and velocity becomes negative
     
      int fps=100;
      int frames = 0;//incremented every screen
      ArrayList<Double> oldPositions = new ArrayList<Double>();
     
      Thread t = new Thread();
       public void init()  
      {
         dim = getSize();
         addMouseMotionListener(this);
         setBackground(Color.black);
         offscreen = createImage(dim.width,dim.height);
         g = offscreen.getGraphics();
         t.start();
      }
   
       public void paint(Graphics z)  
      {
         try{ t.sleep(1000/fps); }
             catch(Exception e){};
         g.clearRect(0,0,dim.width,dim.width);
         
         /*Physics Calculations*/
         position+=(velocity/(double)fps);
         velocity+=(acceleration/(double)fps);
         System.out.println(acceleration/fps);
         System.out.println(position);
         System.out.println(velocity);
         if(backwards==false && velocity<0){
            velocity=0;
            finished=true;
         }
         
         frames++;
         /*Mark previous displacement positions*/
         if(frames%fps==0 && !finished)
            oldPositions.add(new Double(position));
                 
         
         /*Draw Grid*/
         g.setColor(Color.white);
         g.drawLine(0,70,dim.width,70);
         for(int x=0;x<dim. width/10;x++){
            g.setColor(Color.white);
            g.drawLine(x*10,65,x*10,70);
            if(x%5==0){
               g.drawString(x*meterLength+"",x*10,80);
               g.setColor(Color.red);
               g.drawLine(x*10,65,x*10,70-1);
            }
         }
                  /*Draw moving dot and variables*/
         g.setColor(Color.cyan);
         int xLoc = (int)(position*10.0/meterLength);
         g.drawRect(xLoc,50,1,1);
         g.drawLine(xLoc,75,xLoc,65);
         g.drawLine(xLoc+1,75,xLoc+1,65);
                 
         g.setColor(Color.yellow);
         g.drawString("Velocity: "+round(velocity,2)+" m/s",40,100);
         g.drawString("Acceleration: "+round(acceleration,2)+" m/s^2",40,120);
         g.drawString("Current Position: "+round(position,2)+" m",40,140);
         
         
     
         /*Draw previous positions on the grid*/
         g.setColor(Color.yellow);
         g.drawString("Demo Position",10,170);
         g.drawString("Actual Position",130,170);
         g.drawString("Difference",250,170);
         for(int x=0;x<oldPositions.size();x++){
            Double d = oldPositions.get(x);
           
           
         //draws a box on the grid where the point moves
            int drawY=50-1;
            g.setColor(Color.yellow);            
            g.drawRect((int)(d*10.0/meterLength)-1,drawY,2,2);//rect
            g.setColor(Color.white);
            g.drawString(x+"",(int)(d*10.0/meterLength-3),drawY-10);//#
           
            double demo = round(d,2);
            g.drawString("["+x+"]: "+demo,20,200+x*15);//Demo position
           
            int time =x+1;
            double actual = (startingPosition+initialVelocity*time+0.5*acceleration*time*time);
            g.drawString(""+actual,150,200+x*15);//Actual Position
           
            g.drawString(""+(round(actual-demo,5)),270,200+x*15);//Difference
         }
         
         z.drawImage(offscreen,0,0,this);        
         repaint();
      }
   
       public void update(Graphics g)
      {
         paint(g);
      }
   
       public void mouseMoved(MouseEvent evt)  
      {
         mouseX = evt.getX();
         mouseY = evt.getY();
         repaint();
      }
   
       public void mouseDragged(MouseEvent e)  
      {
         mouseDX=e.getX();
         mouseDY=e.getY();
         repaint();
      }
       double round(double d, int decimalPlace){
         int rounder=1;
         for(int x=0;x<decimalPlace;x++)
            rounder*=10;
         return (double)((int)(d*rounder))/(double)rounder;
      }
   }
 
3  Game Development / Newbie & Debugging Questions / Re: Need some help with a project I'm working on on: 2008-11-20 19:20:16
Thanks for the tips guys, but dang is that a lot of reading  Cheesy.  I know what I'll be doing this weekend.  Rewriting the entire applet seems like the best option at this point, but it just seems like such a waste of code.  That entity idea is great though, so I'll implement it the next time I work on a game.  Would it look something like:
1  
2  
3  
4  
5  
6  
public class Entity{
public Entity(){}
public void draw(Graphics g){
//g.draw stuff here;
}
}

and
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class GameMap extends Entity{
int [][] map;
public GameMap(int x, int y){
map=new int[x][y];
}
public void draw(Graphics g){
for(int x=0;x<map.length;x++)
for(int y=0;y<map.length[0];y++)
g.drawImage(images.get(map[x][y]));
}
}


As for loading resources, would you guys just make some kind of resource class, that just loads every sound and image when its instantiated, and then pass that on to every other entity method, along with the Graphics?  The reason my original main applet class was so long was because of resource management.  One last thing, theres a class for every single unit, so that all their actions can be made unique from unit to unit.  Every unit also has a unique shooting sound, so I'm not sure what to do about that either.  Should I just do:
1  
2  
3  
4  
5  
6  
7  
8  
9  
if (unit instanceof CombatTank){
combatTankFire.stop();
combatTankFire.play();
}
else if (unit instanceof Medic){
medicHeal.stop();
medicHeal.play();
}
//... And so on, for all 20 units?
4  Game Development / Newbie & Debugging Questions / Need some help with a project I'm working on on: 2008-11-19 17:49:47
Hey guys, I've only recently started learning java (self teaching for around half a year) so that I could create this game I thought up.  It was basically thrown together, but is still the only java program I've made so far, so I would like to finish it up.  Currently, the code is approximately 5000 lines, but the more I look at it, the more it looks as if I could have written it better from the beginning.  (I looped through all my arrays at least 50 times every frame).

So my question is, "How would I go about optimizing my code"?  Right now, it is only one main applet class, with a class for every single unit, and a building class.  I would like to break up the huge paint method into different classes, but am unsure of how the best way to do this would be, as well as how to pass variables efficiently from one class to the next.

Also, I wanted to know if it is possible to make a png image a different color or rotate it, as there are multiple factions, and I wanted to make all their units and buildings their color.

Lastly, I was wondering how I could make effective unit movement.  As of now, all movement is calculated using distance, which requires me to loop through the whole map, except this does not work well.  There are some units which should not be able to cross water, or go across a pit, but using my method they just disappear and reappear somewhere else.

I have tried to solve all these problems independantly, but was unable to come up with a solution.  Here are some pics of the current build:


and the picture of the map editor:
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
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!