Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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  
  Noob first codelet, wanting to smooth out my animation  (Read 301 times)
0 Members and 1 Guest are viewing this topic.
Offline Credendo

Innocent Bystander





« Posted 2013-05-06 17:19:05 »

So I've been trying to learn to use awt to make some basic graphics driven stuff, and managed to make a jumping ball that can be controlled with the keyboard. It's functional enough, but the animation isn't very smooth looking. I can't seem to figure out how to use the frame delta that is generated by the variable timestep loop I implemented thanks to the incredibly useful post at http://www.java-gaming.org/topics/game-loops/24220/view.html

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  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
public class JumpingBall{

   /**
    * @param args
   */

   //variables

   //Methods
   public static void main(String[] args) {
      Screen screen = new Screen(Globals.DESIRED_FPS);
      screen.run(true);
     
   }
}

public class Ball {

   // variables
   private int Radius;
   private double xLocation;
   private double yLocation;
   private double deltaX;
   private double deltaY;
   
   //Getters/Setters for the Balls features
   public int getRadius() {
      return Radius;
   }
   public void setRadius(int radius) {
      this.Radius = radius;
   }
   public double getxLocation() {
      return xLocation;
   }
   public void setxLocation(double xLocation) {
      this.xLocation = xLocation;
   }
   public double getyLocation() {
      return yLocation;
   }
   public void setyLocation(double yLocation) {
      this.yLocation = yLocation;
   }
   public double getDeltaX() {
      return deltaX;
   }
   public void setDeltaX(double deltaX) {
      this.deltaX = deltaX;
   }
   public double getDeltaY() {
      return deltaY;
   }
   public void setDeltaY(double deltaY) {
      this.deltaY = deltaY;
   }
   public Ball(int radius, double xLocation, double yLocation) {
      this.Radius = radius;
      this.xLocation = xLocation;
      this.yLocation = yLocation;

   }
   
   //Methods
}

public class Globals {
   public final static int SCREEN_WIDTH = 800; // The screen width
   public final static int SCREEN_HEIGHT = 600; // The screen height
   
   public final static int DESIRED_FPS = 60; // Desired max FPS
}

package net.traverse.jumpingball;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;

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


public class Screen extends Canvas{

   private static final long serialVersionUID = 1L; //Version UID
   
   //Variables used by the game loop itself
   private int targetFPS; //The target FPS the game should run at, will be passed in the constructor
   private int FPS; // The current actual frames per second being rendered
   private long optimalTime; //the optimal time in nanoseconds
   private long lastLoopTime; //time in nanoseconds of the last loop pass
   
   Ball b; //
   
   BufferStrategy strategy;
   
   public Screen(int tFPS){
      this.targetFPS = tFPS;
      this.optimalTime = 1000000000 / tFPS;
      this.FPS = 0;
      this.lastLoopTime = System.nanoTime();
      this.b= new Ball(30, 400, 300);
      JFrame container = new JFrame("My first JFrame");
      JPanel panel = (JPanel) container.getContentPane();
      panel.setPreferredSize(new Dimension(Globals.SCREEN_WIDTH - 10, Globals.SCREEN_HEIGHT - 10));
      panel.setLayout(null);
     
      setBounds(0, 0, Globals.SCREEN_WIDTH, Globals.SCREEN_HEIGHT);
     
      panel.add(this);
     
      setIgnoreRepaint(true);
     
      container.pack();
      container.setLocationRelativeTo(null);
      container.setResizable(false);
      container.setVisible(true);

      container.addWindowListener(new WindowAdapter()
      {
         public void windowClosing(WindowEvent e)
         {
            System.exit(0);
         }
      });
     
      addKeyListener(new KeyboardHandler());
     
      requestFocus();
      setFocusable(true);
     
      createBufferStrategy(2);
      strategy = getBufferStrategy();

   }
   private class KeyboardHandler extends KeyAdapter
   {

      public void keyReleased(KeyEvent e)
      {
         int key = e.getKeyCode();
         
         if (key == KeyEvent.VK_LEFT)
         {
            b.setDeltaX(0);
         }

         if (key == KeyEvent.VK_RIGHT)
         {
            b.setDeltaX(0);
         }

      }

      public void keyPressed(KeyEvent e)
      {

         int key = e.getKeyCode();
         
         if (key == KeyEvent.VK_LEFT)
         {
            b.setDeltaX(-10);
         }

         if (key == KeyEvent.VK_RIGHT)
         {
            b.setDeltaX(10);
         }

         if (key == KeyEvent.VK_UP)
         {
            if(b.getDeltaY()>=0){
               b.setDeltaY(b.getDeltaY()+10);
            }
            else{
               b.setDeltaY(b.getDeltaY()-10);
            }
         }

         if (key == KeyEvent.VK_DOWN)
         {
            if(b.getDeltaY()>=0){
               b.setDeltaY(b.getDeltaY()-10);
            }
            else{
               b.setDeltaY(b.getDeltaY()+10);
            }
         }

         
      }

     
      public void keyTyped(KeyEvent e)
      {
         
         if (e.getKeyChar() == 27)
         {
            System.exit(0);
         }
         
      }

   }
   
   public void run(boolean gameRunning){
      long lastFpsTime = 0;
      int fpsUpdate=0;
     
      while(gameRunning){
         //this section works out how long it's been since the last update
         // in order to calculate how far the entities should move on this pass
         long now = System.nanoTime();
         long updateLength = now - lastLoopTime;
         lastLoopTime = now;
         double delta = updateLength / ((double)optimalTime);
         lastFpsTime += updateLength;
          fpsUpdate++;
          if (lastFpsTime >= 1000000000)
            {
             this.FPS = fpsUpdate;
               lastFpsTime = 0;
               fpsUpdate = 0;
            }
          //This is the game logic update function
          gameRunning = doGameUpdates(delta);
         
          //This function draws the updated entities to the screen
         
          Graphics2D g2D = (Graphics2D) strategy.getDrawGraphics();
          g2D.setColor(Color.BLACK);
         g2D.fillRect(0, 0, Globals.SCREEN_WIDTH, Globals.SCREEN_HEIGHT);
          renderEntities(g2D);
          g2D.dispose();
         strategy.show();
         Toolkit.getDefaultToolkit().sync();
         try{
            Thread.sleep(((System.nanoTime()- lastLoopTime) + optimalTime)/1000000);
            }
         catch (InterruptedException e)
         {
            System.out.println("interrupted");
         }
         
      }
   }
   private boolean doGameUpdates(double delta){
         boolean gameRunning = true;
         
         
         /*
          * Fill in with entities to be updated
          */

         if (b.getyLocation() >= Globals.SCREEN_HEIGHT-(b.getRadius()*2)){
            b.setDeltaY((-1*b.getDeltaY()));
         }
         else if(b.getyLocation() <= 0){
            b.setDeltaY((-1*b.getDeltaY()));
         }
         else{
            b.setDeltaY((b.getDeltaY()+5));
         }
         System.out.println(delta);
         b.setyLocation(b.getyLocation()+ b.getDeltaY());
         b.setxLocation(b.getxLocation()+b.getDeltaX());
         
         return gameRunning;
   }

   private void renderEntities(Graphics2D g2D){
     
      g2D.setFont(new Font("Courier New", Font.BOLD, 24));
      g2D.setColor(Color.BLUE);
      g2D.drawString("FPS : " + FPS, 0, 24);
      g2D.fillOval((int)b.getxLocation(), (int)b.getyLocation(),b.getRadius()*2, b.getRadius()*2);
   }
   
}


On the console I show the delta as outputting at 1. something fairly consistently, but when I run this the bouncing ball doesn't seem smooth to me.
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.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (35 views)
2014-12-09 22:41:13

BurntPizza (70 views)
2014-12-08 04:46:31

JscottyBieshaar (32 views)
2014-12-05 12:39:02

SHC (44 views)
2014-12-03 16:27:13

CopyableCougar4 (40 views)
2014-11-29 21:32:03

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

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

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
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!