Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: quew8 (posted 2014-07-13 13:57:52, viewed 81 times)

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  
package testproject;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;

/**
 *
 * @author Quew8
 */
public class TestProject {
    public static final int LOD = 32; //Number of vertices per circle
    public static final float BODY_RADIUS = 2f;
    public static final Colour LINE_COLOUR = new Colour(0, 1, 0, 1);
    public static final Colour BODY_COLOUR = new Colour(0, 0, 1, 1);
    public static final float g = 9.81f / 1000; //Constant for average gravitational acceleration at the surface of the Earth. 
    public static final float FRICTION_COEFFICIENT = 0.01f;
    
    private long lastFrame;
    private final SwingingBody body = new SwingingBody(new Vector(50, 50), new Vector(10, 50), 10);
    
    public void init() throws LWJGLException {
        Display.setDisplayMode(new DisplayMode(800, 600));
        Display.setTitle("Swinging Test App");
        Display.create();
        
        glClearColor(0, 0, 0, 0);
        
        glViewport(0, 0, Display.getWidth(), Display.getHeight());
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 100, 0, 100, -1, 1);
        
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        
        lastFrame = getTimeMillis();
    }
    
    public void loop() {
        while(!Display.isCloseRequested()) {
            update();
            render();
            Display.update();
        }
    }
    
    public void update() {
        int delta = getDelta();
        body.update(delta);
    }
    
    public void render() {
        glClear(GL_COLOR_BUFFER_BIT);
        body.draw();
    }
    
    /**
     * 
     */
    public class SwingingBody {
        private final Vector centre;
        private final Vector position;
        private float angle; //The angle between the rope and the horizontal.
        private float av = 0; //angular velocity
        private final float r; //The radius of the swing - the length of the rope.
        private final float mass;
        
        public SwingingBody(Vector centre, Vector position, float mass) {
            this.centre = centre;
            this.position = position;
            final Vector diff = centre.subtract(position);
            this.r = diff.magnitude();
            this.angle = atan(diff.getY() / diff.getX());
            this.mass = mass;
        }
        
        public void update(int delta) {
            float aa = ( g * cos(angle) / r ) - ( av * FRICTION_COEFFICIENT / mass );
            av += ( aa * delta );
            angle += ( av * delta );
            position.setX(centre.getX() + ( r * cos(angle) ) );
            position.setY(centre.getY() - ( r * sin(angle) ) );
        }
        
        public void draw() {
            glBegin(GL_TRIANGLE_FAN);
            glColor4f(BODY_COLOUR.getR(), BODY_COLOUR.getG(), BODY_COLOUR.getB(), BODY_COLOUR.getA());
            glVertex2f(position.getX(), position.getY());
            float theta = 0;
            for(int i = 0; i <= LOD; i++) {
                glVertex2f(position.getX() + ( BODY_RADIUS * sin(theta) ), position.getY() - ( BODY_RADIUS * cos(theta) ));
                theta += ( 2 * Math.PI / LOD );
            }
            glEnd();
            
            glBegin(GL_LINES);
            glColor4f(LINE_COLOUR.getR(), LINE_COLOUR.getG(), LINE_COLOUR.getB(), LINE_COLOUR.getA());
            glVertex2f(centre.getX(), centre.getY());
            glVertex2f(position.getX(), position.getY());
            glEnd();
        }
    }
    
    /**
     * 
     */
    public static class Vector {
        private float x, y;
        
        public Vector(float x, float y) {
            this.x = x;
            this.y = y;
        }
        
        public Vector subtract(Vector v) {
            return new Vector(this.getX() - v.getX(), this.getY() - v.getY());
        }
        
        public float magnitude() {
            return (float) Math.sqrt( ( getX() * getX() ) + ( getY() * getY() ) );
        }
        
        public void setX(float x) {
            this.x = x;
        }

        public void setY(float y) {
            this.y = y;
        }
        
        public float getX() {
            return x;
        }
        
        public float getY() {
            return y;
        }
    }
    
    /**
     * 
     */
    public static class Colour {
        private final float r, g, b, a;

        public Colour(float r, float g, float b, float a) {
            this.r = r;
            this.g = g;
            this.b = b;
            this.a = a;
        }

        public float getR() {
            return r;
        }

        public float getG() {
            return g;
        }

        public float getB() {
            return b;
        }

        public float getA() {
            return a;
        }
    }
    
    public int getDelta() {
        long time = getTimeMillis();
        int delta = (int) (time - lastFrame);
        lastFrame = time;
        return delta;
    }
    
    public static long getTimeMillis() {
       return (Sys.getTime() * 1000) / Sys.getTimerResolution();
    }
    
    public static float sin(float theta) {
        return (float) Math.sin(theta);
    }
    
    public static float cos(float theta) {
        return (float) Math.cos(theta);
    }
    
    public static float atan(float r) {
        return (float) Math.atan(r);
    }
    
    /**
     * @param args the command line arguments
     * @throws org.lwjgl.LWJGLException
     */
    public static void main(String[] args) throws LWJGLException {
        TestProject tp = new TestProject();
        tp.init();
        tp.loop();
    }
    
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

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

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (24 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (69 views)
2014-11-07 19:57: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

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!