Successfully implmented physics system

This commit is contained in:
Brychan Dempsey 2021-05-14 14:29:28 +12:00
parent 5aeb91d28f
commit 5121b193b9
4 changed files with 10 additions and 8 deletions

View File

@ -61,6 +61,7 @@ public final class App {
double dt = 0.0; double dt = 0.0;
double frameRate = 1.0 / 20; // 1 second divided by target number of frames double frameRate = 1.0 / 20; // 1 second divided by target number of frames
double idleTime = 0.0; // the amount of time spent sleeping
boolean quit = false; boolean quit = false;
long loopStart = System.currentTimeMillis(); long loopStart = System.currentTimeMillis();
@ -68,13 +69,14 @@ public final class App {
// Get the time between frames for accurate time-based calculations (i.e. physics systems etc) // Get the time between frames for accurate time-based calculations (i.e. physics systems etc)
long startTime = System.nanoTime(); long startTime = System.nanoTime();
frameRateSystem.update(dt); frameRateSystem.update(dt, idleTime);
physicsSystem.update(dt); physicsSystem.update(dt);
logVec2DSystem.update(dt); logVec2DSystem.update(dt);
dt = (System.nanoTime() - startTime) / 1e9; // convert nanoseconds to seconds dt = (System.nanoTime() - startTime) / 1e9; // convert nanoseconds to seconds
// Limit frame rate (approximately) to minimum of 16 ms // Limit frame rate (approximately) to minimum of 16 ms
if (dt < frameRate){ if (dt < frameRate){
idleTime = frameRate-dt;
Thread.sleep((int)((frameRate-dt)*1000)); Thread.sleep((int)((frameRate-dt)*1000));
dt = (System.nanoTime() - startTime) / 1e9; dt = (System.nanoTime() - startTime) / 1e9;
} }

View File

@ -3,7 +3,7 @@ package nz.ac.massey.programming_project_159333_s1_2021;
public class FrameRateSystem extends ECSSystem{ public class FrameRateSystem extends ECSSystem{
void init() {} void init() {}
void update(double dt){ void update(double dt, double idleTime){
System.out.print("dt: " + dt + " "); System.out.print(String.format("dt: %.3g (%.3g idle) ", dt, idleTime));
} }
} }

View File

@ -11,7 +11,7 @@ public class LogVec2DSystem extends ECSSystem{
void update(double dt){ void update(double dt){
for (Integer entity : entities) { for (Integer entity : entities) {
Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class.getName()); Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class.getName());
System.out.println("X: " + pos.x + ", Y:" + pos.y); System.out.println(String.format("X: %.6g, Y: %.6g", pos.x, pos.y));
} }
} }
} }

View File

@ -26,13 +26,13 @@ class SystemManager{
// instance. // instance.
// I.e., create an object that represents the system class; then store that class in the system // I.e., create an object that represents the system class; then store that class in the system
// table. // table.
public boolean registerSystem(String system, ECSSystem action){ public boolean registerSystem(String systemName, ECSSystem system){
if (systems.containsKey(system)){ if (systems.containsKey(systemName)){
System.err.println("System already registered"); System.err.println("System already registered");
return false; return false;
} }
systems.put(system, action); systems.put(systemName, system);
signatures.put(system, new BitSet()); signatures.put(systemName, new BitSet());
return true; return true;
} }