Advertisement
ClickerMonkey

Moving Circle - Static Rectangle Collision Resolution

Sep 12th, 2013
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.88 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics2D;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.geom.Ellipse2D;
  6. import java.awt.geom.Line2D;
  7. import java.awt.geom.Rectangle2D;
  8.  
  9. import com.gameprogblog.engine.Game;
  10. import com.gameprogblog.engine.GameLoop;
  11. import com.gameprogblog.engine.GameLoopVariable;
  12. import com.gameprogblog.engine.GameScreen;
  13. import com.gameprogblog.engine.GameState;
  14. import com.gameprogblog.engine.Scene;
  15. import com.gameprogblog.engine.Vector;
  16. import com.gameprogblog.engine.core.Bound2;
  17. import com.gameprogblog.engine.input.GameInput;
  18.  
  19.  
  20. public class CircleRectangle implements Game
  21. {
  22.  
  23.    public static void main( String[] args )
  24.    {
  25.       Game game = new CircleRectangle();
  26.       GameLoop loop = new GameLoopVariable( 0.1f );
  27.       GameScreen screen = new GameScreen( 640, 480, true, loop, game );
  28.       screen.setBackground( Color.black );
  29.       GameScreen.showWindow( screen, "CircleRectangle" );
  30.    }
  31.  
  32.    public static final Font FONT = new Font( "Monospaced" , Font.PLAIN, 12 );
  33.    
  34.    private enum DraggingState {
  35.       START, END, RADIUS, NONE;
  36.    }
  37.    
  38.    private float pointRadius = 8.0f;
  39.    private boolean playing = false;
  40.    private Vector start;
  41.    private Vector end;
  42.    private Vector radiusPoint;
  43.    private float radius;
  44.    private Bound2 bounds;
  45.    private DraggingState dragging;
  46.  
  47.    @Override
  48.    public void start( Scene scene )
  49.    {
  50.       bounds = new Bound2( 150, 150, 490, 330 );
  51.       start = new Vector( 50, 400 );
  52.       end = new Vector( 320, 240 );
  53.       radius = 40.0f;
  54.       radiusPoint = new Vector( start.x, start.y - radius );
  55.       dragging = DraggingState.NONE;
  56.      
  57.       playing = true;
  58.    }
  59.  
  60.    @Override
  61.    public void input( GameInput input )
  62.    {
  63.       if (input.keyDown[KeyEvent.VK_ESCAPE])
  64.       {
  65.          playing = false;
  66.       }
  67.      
  68.       Vector mouse = new Vector(input.mouseX, input.mouseY);
  69.  
  70.       if (dragging != DraggingState.NONE && !input.keyDown[KeyEvent.VK_D]) {
  71.          dragging = DraggingState.NONE;
  72.       }
  73.      
  74.       if (dragging == DraggingState.NONE && input.keyDown[KeyEvent.VK_D]) {
  75.            if (mouse.distance( start ) <= pointRadius) {
  76.               dragging = DraggingState.START;
  77.            } else if (mouse.distance( end ) <= pointRadius) {
  78.               dragging = DraggingState.END;
  79.            } else if (mouse.distance( radiusPoint ) <= pointRadius) {
  80.               dragging = DraggingState.RADIUS;
  81.            }
  82.       }
  83.      
  84.       switch (dragging) {
  85.       case END:
  86.          end.set( mouse );
  87.          break;
  88.       case RADIUS:
  89.          radiusPoint.set( mouse );
  90.          radius = radiusPoint.distance( start );
  91.          break;
  92.       case START:
  93.          start.set( mouse );
  94.          radiusPoint.set( mouse );
  95.          radiusPoint.y -= radius;
  96.          break;
  97.       default:
  98.          break;
  99.       }
  100.    }
  101.  
  102.    @Override
  103.    public void update( GameState state, Scene scene )
  104.    {
  105.    }
  106.  
  107.    @Override
  108.    public void draw( GameState state, Graphics2D gr, Scene scene )
  109.    {
  110.       gr.setColor( Color.blue );
  111.       gr.draw( new Rectangle2D.Float( bounds.left, bounds.top, bounds.getWidth(), bounds.getHeight() ) );
  112.  
  113.       gr.setColor( Color.white );
  114.       gr.draw( new Line2D.Float( start.x, start.y, end.x, end.y ) );
  115.      
  116.       gr.setColor( Color.green );
  117.       gr.draw( new Ellipse2D.Float( start.x - pointRadius, start.y - pointRadius, pointRadius * 2, pointRadius * 2 ) );
  118.      
  119.       gr.setColor( Color.red );
  120.       gr.draw( new Ellipse2D.Float( end.x - pointRadius, end.y - pointRadius, pointRadius * 2, pointRadius * 2 ) );
  121.      
  122.       gr.setColor( Color.yellow );
  123.       gr.draw( new Ellipse2D.Float( radiusPoint.x - pointRadius, radiusPoint.y - pointRadius, pointRadius * 2, pointRadius * 2 ) );
  124.       gr.draw( new Ellipse2D.Float( start.x - radius, start.y - radius, radius * 2, radius * 2 ) );
  125.       gr.draw( new Ellipse2D.Float( end.x - radius, end.y - radius, radius * 2, radius * 2 ) );
  126.      
  127.       /*
  128.        * Calculations
  129.        */
  130.       float L = bounds.left;
  131.       float T = bounds.top;
  132.       float R = bounds.right;
  133.       float B = bounds.bottom;
  134.       float dx = end.x - start.x;
  135.       float dy = end.y - start.y;
  136.      
  137.       // cast point along the left axis intersecting the path start->end and measure the distance between that point and the left
  138.       float pl = dy * (L - start.x) / dx + start.y;
  139.       float plt = T - pl;
  140.       float plb = pl - B;
  141.      
  142.       float pr = dy * (R - start.x) / dx + start.y;
  143.       float prt = T - pr;
  144.       float prb = pr - B;
  145.      
  146.       float pt = dx * (T - start.y) / dy + start.x;
  147.       float ptl = L - pt;
  148.       float ptr = pt - R;
  149.  
  150.       float pb = dx * (B - start.y) / dy + start.x;
  151.       float pbl = L - pb;
  152.       float pbr = pb - R;
  153.      
  154.       gr.setColor( Color.lightGray );
  155.       gr.setFont( FONT );
  156.       gr.drawString( "dist from top on left side:     " + plt, 10, 20 );
  157.       gr.drawString( "dist from bottom on left side:  " + plb, 10, 32 );
  158.       gr.drawString( "dist from top on right side:    " + prt, 10, 44 );
  159.       gr.drawString( "dist from bottom on right side: " + prb, 10, 56 );
  160.       gr.drawString( "dist from left on top side:     " + ptl, 10, 68 );
  161.       gr.drawString( "dist from right on top side:    " + ptr, 10, 80 );
  162.       gr.drawString( "dist from left on bottom side:  " + pbl, 10, 92 );
  163.       gr.drawString( "dist from right on bottom side: " + pbr, 10, 104 );
  164.      
  165.       String message = "";
  166.       if ((plb > radius && prb > radius) ||
  167.           (plt > radius && prt > radius) ||
  168.           (ptl > radius && pbl > radius) ||
  169.           (ptr > radius && pbr > radius))
  170.       {
  171.          message = "not intersecting";
  172.       }
  173.      
  174.       gr.drawString( message, 10, 116 );
  175.    }
  176.  
  177.    @Override
  178.    public void destroy()
  179.    {
  180.  
  181.    }
  182.  
  183.    @Override
  184.    public boolean isPlaying()
  185.    {
  186.       return playing;
  187.    }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement