Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (482)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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  
  Linear Motion  (Read 1346 times)
0 Members and 1 Guest are viewing this topic.
Offline marinepower101

Junior Newbie





« Posted 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;
      }
   }
 
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2008-11-23 17:38:06 »

Since you're using <a href="http://en.wikipedia.org/wiki/Euler's_method">euler integration</a> your results will always be an approximation. Plus you'll have the usual floating-point inaccuracies and cumulative rounding error.

What problem is this causing exactly? Usually the small differences aren't big enough to matter for games. As you've found you can increase your simulation steps to get better accuracy, or you can switch to another integration method.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline marinepower101

Junior Newbie





« Reply #2 - Posted 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.
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.

CopyableCougar4 (14 views)
2014-08-22 19:31:30

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

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

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

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

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

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

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

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

Norakomi (37 views)
2014-08-06 19:49:38
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!