vectors, time, new rendering methods, all o dat yo

rotation
Marcus Gosselin 2016-02-23 23:10:45 -05:00
parent 07b1f60f27
commit 07c4572ce3
11 changed files with 239 additions and 23 deletions

View File

@ -0,0 +1,32 @@
package diveengine2d;
import java.awt.Graphics2D;
import java.awt.Color;
public class DebugSettings {
public static int debugLevel = 0;
public static Color backgroundColor = new Color(250, 250, 250);
public static Color foregroundColor = new Color(25, 118, 210);
public static void render(Graphics2D g) {
if(debugLevel == 0) return;
g.setColor(backgroundColor);
int y = 20;
int x = 20;
g.fillRect(x, y, 140, debugLevel == 1 ? 72 : 100);
g.setColor(foregroundColor);
x += 10;
y += 20;
int yOff = 20;
g.drawString("" + Time.FPS, x, y);
y += yOff;
g.drawString("" + Time.timedFramesCurrent, x, y);
y += yOff;
g.drawString("" + SceneManager.entityCount(), x, y);
y += yOff;
}
}

View File

@ -0,0 +1,32 @@
package diveengine2d;
public class DiveMath {
public static boolean inTriangle(Vector2 a, Vector2 b, Vector2 c, Vector2 p) {
return sameSide(p, a, b, c) && sameSide(p, b, c, a) && sameSide(p, c, a, b);
}
public static boolean sameSide(Vector2 p1, Vector2 p2, Vector2 a, Vector2 b) {
Vector2 bma = b.subtract(a);
Vector2 p1ma = p1.subtract(a);
Vector2 p2ma = p2.subtract(a);
float cp1 = bma.x*p1ma.y-bma.y*p1ma.x;
float cp2 = bma.x*p2ma.y-bma.y*p2ma.x;
return cp1/cp2 >= 0;
}
/*
function SameSide(p1,p2, a,b)
cp1 = CrossProduct(b-a, p1-a)
cp2 = CrossProduct(b-a, p2-a)
if DotProduct(cp1, cp2) >= 0 then return true
else return false
function PointInTriangle(p, a,b,c)
if SameSide(p,a, b,c) and SameSide(p,b, a,c)
and SameSide(p,c, a,b) then return true
else return false
*/
}

View File

@ -3,8 +3,8 @@ package diveengine2d;
import java.awt.Canvas; import java.awt.Canvas;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferStrategy; import java.awt.image.BufferStrategy;
import java.io.File; import java.io.File;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -19,7 +19,7 @@ public class Engine extends Canvas {
public static int WIDTH, HEIGHT; public static int WIDTH, HEIGHT;
public static String startScene = null; public static String startScene = null;
public static String name = null; public static String name = null;
public static BufferStrategy bs; public static BufferStrategy bs = null;
public Engine(String gameFolder) { public Engine(String gameFolder) {
@ -60,26 +60,62 @@ public class Engine extends Canvas {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.requestFocus(); this.requestFocus();
this.addKeyListener(new Input()); this.addKeyListener(new Input());
this.addMouseMotionListener(new Input());
this.addMouseListener(new Input());
createBufferStrategy(2); createBufferStrategy(2);
bs = getBufferStrategy(); bs = getBufferStrategy();
Time.nanos = System.nanoTime();
while(true) { while(true) {
long startTime = System.currentTimeMillis();
updateScene(); Time.startTime = System.currentTimeMillis();
Graphics2D g = (Graphics2D)bs.getDrawGraphics(); if (System.currentTimeMillis() > Time.nextSecond) {
render(g); Time.nextSecond += 1000;
bs.show(); Time.FPS = Time.framesInCurrentSecond;
int elapsed = (int)(System.currentTimeMillis() - startTime); Time.framesInCurrentSecond = 0;
try{ System.out.println("Timed Frames: " + Time.timedFramesCurrent);
Thread.sleep(17 - elapsed); System.out.println("Calculated Frames: " + Time.FPS);
}catch(Exception e) { Time.timedFramesCurrent = 0;
} }
Time.framesInCurrentSecond++;
render();
updateScene();
Time.tickTime = (System.nanoTime() - Time.nanos)/16640000d;
Time.deltaTime = Time.tickTime * Time.timeScale;
Time.nanos = System.nanoTime();
// System.out.println("dTime: " + Time.deltaTime);
Time.timedFramesCurrent += Time.deltaTime;
} }
} }
public void render() {
Graphics2D g = (Graphics2D)bs.getDrawGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
render(g);
DebugSettings.render(g);
Vector2 a = new Vector2(512, 100);
Vector2 b = new Vector2(256, 200);
Vector2 c = new Vector2(768, 400);
Vector2 mouse = new Vector2(Input.mouseX, Input.mouseY);
g.setColor(DiveMath.inTriangle(a, b, c, mouse) ? Color.GREEN : Color.RED);
g.fillOval((int)a.x, (int)a.y, 10, 10);
g.fillOval((int)b.x, (int)b.y, 10, 10);
g.fillOval((int)c.x, (int)c.y, 10, 10);
bs.show();
}
private void updateScene() { private void updateScene() {
SceneManager.updateAll(); SceneManager.updateAll();
} }
@ -112,7 +148,7 @@ public class Engine extends Canvas {
parts[1] = parts[1].trim(); parts[1] = parts[1].trim();
if (parts[0].equals("StartScene")) { if (parts[0].equals("StartScene")) {
this.startScene = parts[1]; Engine.startScene = parts[1];
} else if (parts[0].equals("Resolution")) { } else if (parts[0].equals("Resolution")) {
String[] resparts = parts[1].split("x"); String[] resparts = parts[1].split("x");
@ -143,7 +179,6 @@ public class Engine extends Canvas {
System.out.println("Loaded Config File..."); System.out.println("Loaded Config File...");
} }
private void render(Graphics2D g) { private void render(Graphics2D g) {
SceneManager.render(g); SceneManager.render(g);
g.setColor(Color.BLACK); g.setColor(Color.BLACK);

View File

@ -1,4 +1,5 @@
package diveengine2d; package diveengine2d;
import java.awt.Graphics2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -10,6 +11,7 @@ public class Entity {
public List<DiveScript> components = new ArrayList<DiveScript>(); public List<DiveScript> components = new ArrayList<DiveScript>();
@SuppressWarnings("unchecked") //HACK because it is checked, java is just duuuumb
public <T extends DiveScript> T getComponent(Class<T> componentType) { public <T extends DiveScript> T getComponent(Class<T> componentType) {
for(DiveScript script : components) { for(DiveScript script : components) {
if(script.getClass().isAssignableFrom(componentType)) { if(script.getClass().isAssignableFrom(componentType)) {
@ -36,4 +38,8 @@ public class Entity {
public void addComponent(DiveScript component) { public void addComponent(DiveScript component) {
components.add(component); components.add(component);
} }
public void render(Graphics2D g) {
if(DebugSettings.debugLevel > 0) g.drawString("" + x + ", " + y, x, y - 2);
}
} }

View File

@ -2,13 +2,17 @@ package diveengine2d;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Input implements KeyListener{ public class Input implements KeyListener, MouseListener, MouseMotionListener{
private static boolean[] keys = new boolean[512]; private static boolean[] keys = new boolean[512];
private static List<KeyListener> listeners = new ArrayList<KeyListener>(); private static List<KeyListener> listeners = new ArrayList<KeyListener>();
public static int mouseX, mouseY;
public static void addKeyListener(KeyListener listener) { public static void addKeyListener(KeyListener listener) {
listeners.add(listener); listeners.add(listener);
@ -36,4 +40,46 @@ public class Input implements KeyListener{
keys[e.getKeyCode()] = false; keys[e.getKeyCode()] = false;
for(KeyListener l : listeners) l.keyReleased(e); for(KeyListener l : listeners) l.keyReleased(e);
} }
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
} }

View File

@ -2,6 +2,7 @@ package diveengine2d;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Polygon;
public class RectRenderer extends DiveScript{ public class RectRenderer extends DiveScript{
@ -12,10 +13,18 @@ public class RectRenderer extends DiveScript{
name = "Rectangle Renderer"; name = "Rectangle Renderer";
} }
float i = 0;
public void render(Graphics2D g) { public void render(Graphics2D g) {
//g.rotate(Math.toRadians(i+=Time.deltaTime));
g.setColor(color); g.setColor(color);
g.fillRect((int)entity.x, (int)entity.y, width, height); Polygon p = new Polygon(new int[]{0, width, width, 0}, new int[]{0, 0, height, height}, 4);
//System.out.println("" + entity.x + " " + entity.y); for(int i = 0; i < p.npoints; i ++) {
g.drawString("" + Time.deltaTime, 100, 100);
}
p.translate((int)entity.x, (int)entity.y);
g.fillPolygon(p);
//g.fillRect((int)entity.x, (int)entity.y, width, height);
//g.rotate(Math.toRadians(-i));
} }
} }

View File

@ -1,17 +1,36 @@
package diveengine2d; package diveengine2d;
import java.awt.Color;
import java.awt.Graphics2D;
public class RigidBody extends DiveScript{ public class RigidBody extends DiveScript{
private static Color xAxisColor = new Color(244, 67, 54); //A500 red
private static Color yAxisColor = new Color(33, 150, 243); //A500 blue
private static Color debugColor = new Color(76, 175, 80); //A500 green
public double dx, dy, drot; public double dx, dy, drot;
public double friction; public double friction;
public boolean physics = false;
//public bool
public void update() { public void update() {
dx += (dx*friction - dx) * Time.deltaTime;
dy += (dy*friction - dy) * Time.deltaTime;
drot += (drot*friction - drot) * Time.deltaTime;
entity.x += dx * Time.deltaTime; entity.x += dx * Time.deltaTime;
entity.y += dy * Time.deltaTime; entity.y += dy * Time.deltaTime;
entity.rotation += drot * Time.deltaTime; entity.rotation += drot * Time.deltaTime;
double friction = 1 - ((1 - this.friction) * Time.deltaTime); }
dx *= friction;
dy *= friction; public void render(Graphics2D g) {
drot *= friction; if(DebugSettings.debugLevel > 0) {
g.setColor(xAxisColor);
g.drawLine((int)entity.x, (int)entity.y, (int)entity.x+(int)(dx*20), (int)entity.y);
g.setColor(yAxisColor);
g.drawLine((int)entity.x, (int)entity.y, (int)entity.x, (int)entity.y+(int)(dy*20));
g.setColor(debugColor);
g.drawLine((int)entity.x, (int)entity.y, (int)entity.x+(int)(dx*15), (int)entity.y+(int)(dy*15));
}
} }
} }

View File

@ -188,6 +188,7 @@ public class SceneManager {
for(DiveScript script : e.components) { for(DiveScript script : e.components) {
script.render(g); script.render(g);
} }
e.render(g);
} }
} }
@ -198,4 +199,8 @@ public class SceneManager {
} }
} }
} }
public static int entityCount() {
return entities.size();
}
} }

View File

@ -9,5 +9,6 @@ public class Time {
public static double timeScale = 1; public static double timeScale = 1;
public static double deltaTime = 0; public static double deltaTime = 0;
public static long nextSecond = System.currentTimeMillis() + 1000, startTime = 0; public static long nextSecond = System.currentTimeMillis() + 1000, startTime = 0;
public static double timedFramesCurrent;
} }

View File

@ -0,0 +1,21 @@
package diveengine2d;
public class Vector2 {
public float x, y;
public Vector2(float x, float y) {
this.x = x;
this.y = y;
}
public Vector2 subtract(Vector2 b) {
Vector2 _return = new Vector2(x, y);
_return.x -= b.x;
_return.y -= b.y;
return _return;
}
public Vector2 rectToPolar(Vector2 b) {
Vector2 _return = new Vector2(x, y);
return _return;
}
}

View File

@ -0,0 +1,10 @@
package diveengine2d;
public class Vector3 {
public float x, y, z;
public Vector3(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
}