Skip to content

Commit

Permalink
#1 Update working with World class
Browse files Browse the repository at this point in the history
  • Loading branch information
introfog committed Feb 4, 2020
1 parent 71a1915 commit 6ce8ff5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
30 changes: 15 additions & 15 deletions src/main/java/com/introfog/pie/demo/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.introfog.pie.core.shape.Circle;
import com.introfog.pie.core.shape.IShape;
import com.introfog.pie.core.shape.Polygon;
import com.introfog.pie.core.World;
import com.introfog.pie.core.collisionDetection.BroadPhase;
import com.introfog.pie.core.math.MathPIE;
import com.introfog.pie.core.math.Vector2f;
Expand All @@ -20,6 +19,8 @@
import javax.swing.JPanel;
import javax.swing.Timer;

import static com.introfog.pie.demo.Main.world;

public class Display extends JPanel implements ActionListener {
private float deltaTime;
private long previousTime;
Expand All @@ -42,7 +43,7 @@ public Display() {
addMouseListener(new MouseEvents());

initializeBodies();
World.getInstance().iterations = 10;
world.setCollisionSolveIterations(10);
previousTime = System.nanoTime();

try {
Expand All @@ -62,14 +63,14 @@ public void paint(Graphics g) {
g.setColor(Color.BLACK);

g.drawString("FPS: " + (int) (1 / deltaTime), 2, 12);
g.drawString("Bodies: " + World.getInstance().getAmountBodies(), 2, 24);
g.drawString("Bodies: " + world.getBodies().size(), 2, 24);
g.drawString("Version: 0.2", 2, 36);

//testProductivity ();
//testBodiesPenetration ();
//rectangle.setOrientation (rectangle.body.orientation + 0.001f);

World.getInstance().update(deltaTime);
world.update(deltaTime);
draw(g);
}

Expand All @@ -80,12 +81,12 @@ public void actionPerformed(ActionEvent e) {

private void initializeBodies() {
circle = new Circle(40f, 220f, 350f, MathPIE.STATIC_BODY_DENSITY, 0.2f);
World.getInstance().addShape(circle);
world.addShape(circle);

Vector2f[] vertices = {new Vector2f(20f, -20f), new Vector2f(40f, 20f), new Vector2f(0f, 60f),
new Vector2f(-60f, 40f), new Vector2f(-40f, 0f), new Vector2f(0f, 0f)};
polygon = new Polygon(MathPIE.STATIC_BODY_DENSITY, 0.2f, 470f, 400f, vertices);
World.getInstance().addShape(polygon);
world.addShape(polygon);
}

private void testProductivity() {
Expand All @@ -100,22 +101,21 @@ private void testProductivity() {
currYNewBody += SIZE + 1f;
}

if (World.getInstance().getAmountBodies() % 2 == 0) {
if (world.getBodies().size() % 2 == 0) {
rectangle = Polygon
.generateRectangle(currXNewBody * (SIZE + 1f) + SIZE / 2f, currYNewBody + SIZE / 2f, SIZE, SIZE,
1f,
0.2f);
World.getInstance().addShape(rectangle);
world.addShape(rectangle);
float dt = deltaTime * 100000;
dt = Math.round(dt);
dt /= 100000;
out.print("Bodies: " + World.getInstance().getAmountBodies() + "\tdt: " + dt);
out.println("\tMay be collision bodies: " + World.getInstance().amountMayBeCollisionBodies
+ "\tIntersects oper.:" + BroadPhase.INTERSECTED_COUNTER);
out.print("Bodies: " + world.getBodies().size() + "\tdt: " + dt);
out.println("\tMay be collision bodies: \tIntersects oper.:" + BroadPhase.INTERSECTED_COUNTER);
out.flush();
} else {
circle = new Circle(SIZE / 2f, currXNewBody * (SIZE + 1f) + SIZE / 2f, currYNewBody + SIZE / 2f, 0.4f, 0.5f);
World.getInstance().addShape(circle);
world.addShape(circle);
}
currXNewBody++;
}
Expand All @@ -127,13 +127,13 @@ private void testBodiesPenetration() {
timer = TIMER * 10f;

rectangle = Polygon.generateRectangle(400f, currYNewBody, SIZE, SIZE, 0.4f, 0.5f);
World.getInstance().addShape(rectangle);
world.addShape(rectangle);
}
timer -= deltaTime;
}

private void draw(Graphics graphics) {
World.getInstance().bodies.forEach((body) -> {
world.getBodies().forEach((body) -> {
if (body.shape instanceof Polygon) {
Polygon polygon = (Polygon) body.shape;

Expand Down Expand Up @@ -177,7 +177,7 @@ private void draw(Graphics graphics) {
// Рисвание нормалей к точкам касания в коллизии
if (Main.ENABLE_DEBUG_DRAW) {
graphics.setColor(Color.GREEN);
World.getInstance().collisions.forEach((collision) -> {
world.getCollisions().forEach((collision) -> {
for (int i = 0; i < collision.contactCount; i++) {
graphics.drawLine((int) collision.contacts[i].x, (int) collision.contacts[i].y,
(int) (collision.contacts[i].x + collision.normal.x * 10),
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/introfog/pie/demo/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.introfog.pie.demo;

import com.introfog.pie.core.World;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

Expand All @@ -8,6 +9,8 @@ public class Main {
public static final int WINDOW_HEIGHT = 700;
public static final boolean ENABLE_DEBUG_DRAW = false;

public static World world = new World();

public static void main(String[] args) {
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/introfog/pie/demo/MouseEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.introfog.pie.core.shape.Circle;
import com.introfog.pie.core.shape.Polygon;
import com.introfog.pie.core.World;

import java.applet.Applet;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import static com.introfog.pie.demo.Main.world;

public class MouseEvents extends Applet implements MouseListener {
@Override
public void init() {
Expand Down Expand Up @@ -36,7 +37,7 @@ public void mousePressed(MouseEvent me) {
Circle circle;
float rand = (float) Math.random();
circle = new Circle(rand * 20f + 5f, mouseX, mouseY, 1f, 0.2f);
World.getInstance().addShape(circle);
world.addShape(circle);
} else if (me.getButton() == MouseEvent.BUTTON3) {
Polygon rectangle;
float rand = (float) Math.random();
Expand All @@ -45,7 +46,7 @@ public void mousePressed(MouseEvent me) {
float width = rand * 80f + 20f;
rectangle = Polygon.generateRectangle(mouseX, mouseY, width, height, 1f, 0.2f);
rectangle.setOrientation((float) Math.PI / 6f);
World.getInstance().addShape(rectangle);
world.addShape(rectangle);
}
}

Expand Down

0 comments on commit 6ce8ff5

Please sign in to comment.