Skip to content

Commit

Permalink
Refactored and implemented object fall physics
Browse files Browse the repository at this point in the history
* Added DisplayBuffer class
* Removed viewBuffer from the Camera class drawing directly to
DisplayBuffer now
* Added PhysicsSettings class (contains physics settings of scenes)
* Added object fall physics
* Deleted ProfilerData class (Profiler gets data from engine)
  • Loading branch information
John committed Nov 5, 2018
1 parent 688c8e4 commit 00afb42
Show file tree
Hide file tree
Showing 24 changed files with 758 additions and 654 deletions.
7 changes: 4 additions & 3 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.6
6 changes: 6 additions & 0 deletions ProjectHistory
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ v0.1 - 24/08/2018
* Moved Renderer from SceneWindow to Engine
* Moved Animator from SceneWindow to Engine
* Moved SceneWindow back to src/main/java (moved to src/test/java accidently in last commit)
05/11/2018
* Added DisplayBuffer class
* Removed viewBuffer from the Camera class drawing directly to DisplayBuffer now
* Added PhysicsSettings class (contains physics settings of scenes)
* Added object fall physics
* Deleted ProfilerData class (Profiler gets data from engine)



Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ John´s Project Game Engine (JPGE) is tiny software based Game Engine writen in
# Versions
v0.1 - beta

[![JPGE v0.1 - beta](https://i9.ytimg.com/vi/LnPAomKeT5o/mq2.jpg?sqp=CJSGwt4F&rs=AOn4CLBClKXfoGPC6TJ-T37VvLz5oSZ-JA)](https://www.youtube.com/watch?v=LnPAomKeT5o&t=1s)
[![JPGE v0.1 - beta](https://i9.ytimg.com/vi/LnPAomKeT5o/mq2.jpg?sqp=CKi89t4F&rs=AOn4CLASozT4nfVeVfR2FnIoxBwHSspHww)](https://www.youtube.com/watch?v=LnPAomKeT5o&t=1s)
13 changes: 10 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<java.version>1.6</java.version>

<!-- lifecycle plugin version settings -->
<version.compiler.plugin>3.5</version.compiler.plugin>
Expand All @@ -28,8 +28,8 @@
<version.checkstyle.plugin>2.17</version.checkstyle.plugin>

<!-- Java version settings -->
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.6</maven.compiler.source>



Expand Down Expand Up @@ -114,6 +114,13 @@
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
191 changes: 158 additions & 33 deletions src/main/java/com/johnsproject/jpge/Engine.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.johnsproject.jpge;

import com.johnsproject.jpge.dto.DisplayBuffer;
import com.johnsproject.jpge.dto.Scene;
import com.johnsproject.jpge.graphics.Animator;
import com.johnsproject.jpge.graphics.Renderer;
Expand Down Expand Up @@ -27,13 +28,25 @@ public static Engine getInstance() {
private Thread inputThread;
private Thread physicsThread;

private EngineSettings settings = new EngineSettings();
private int graphicsUpdateRate = 30;
private int inputUpdateRate = 30;
private int physicsUpdateRate = 30;

private long startTime = System.currentTimeMillis();
private boolean playing = true;

private int lastGraphicsTime = 0;
private int lastInputTime = 0;
private int lastPhysicsTime = 0;
private int lastRendereredFaces = 0;

private Scene scene = new Scene();
private Renderer renderer = new Renderer();
private Animator animator = new Animator();
private KeyInputManager keyManager = new KeyInputManager();
private MouseInputManager mouseManager = new MouseInputManager();
private PhysicsAnimator physicsAnimator = new PhysicsAnimator();
private DisplayBuffer displayBuffer = new DisplayBuffer(320, 240);
private SceneWindow sceneWindow = null;

public Engine() {
Expand All @@ -46,8 +59,8 @@ public Engine() {
* Starts the engine.
*/
public void play() {
if (!settings.isPlaying()) {
settings.setPlaying(true);
if (!isPlaying()) {
setPlaying(true);
startGraphicsThread();
startPhysicsThread();
}
Expand All @@ -57,18 +70,31 @@ public void play() {
* Pauses the engine.
*/
public void pause() {
if (settings.isPlaying()) {
settings.setPlaying(false);
if (isPlaying()) {
setPlaying(false);
}
}

private void setPlaying(boolean value) {
playing = value;
}

/**
* Returns the {@link EngineSettings settings} of this engine.
* Returns the time since the engine started in miliseconds.
*
* @return {@link EngineSettings settings} of this engine.
* @return time since the engine started in miliseconds.
*/
public EngineSettings getSettings() {
return settings;
public long getPlayTime() {
return System.currentTimeMillis() - startTime;
}

/**
* Returns if the engine is running.
*
* @return if the engine is running.
*/
public boolean isPlaying() {
return playing;
}

/**
Expand Down Expand Up @@ -134,6 +160,15 @@ public PhysicsAnimator getPhysicsAnimator() {
return physicsAnimator;
}

/**
* Returns the {@link DisplayBuffer} used by the engine.
*
* @return {@link DisplayBuffer} used by the engine.
*/
public DisplayBuffer getDisplayBuffer() {
return displayBuffer;
}

/**
* Returns the {@link SceneWindow} used by the engine.
*
Expand All @@ -152,13 +187,108 @@ public void setSceneWindow(SceneWindow sceneWindow) {
this.sceneWindow = sceneWindow;
}

/**
* Returns how often graphics should be updated in a second.
* Default is 30.
*
* @return how often graphics should be updated in a second.
*/
public int getGraphicsUpdateRate() {
return graphicsUpdateRate;
}

/**
* Sets how often graphics should be updated in a second.
* Default is 30.
*
* @param graphicsUpdateRate value to set.
*/
public void setGraphicsUpdateRate(int graphicsUpdateRate) {
this.graphicsUpdateRate = graphicsUpdateRate;
}

/**
* Returns how often input should be updated in a second.
* Default is 30.
*
* @return how often input should be updated in a second.
*/
public int getInputUpdateRate() {
return inputUpdateRate;
}

/**
* Sets how often input should be updated in a second.
* Default is 30.
*
* @param inputUpdateRate value to set.
*/
public void setInputUpdateRate(int inputUpdateRate) {
this.inputUpdateRate = inputUpdateRate;
}

/**
* Returns how often physics should be updated in a second.
* Default is 30.
*
* @return how often physics should be updated in a second.
*/
public int getPhysicsUpdateRate() {
return physicsUpdateRate;
}

/**
* Sets how often physics should be updated in a second.
* Default is 30.
*
* @param physicsUpdateRate value to set.
*/
public void setPhysicsUpdateRate(int physicsUpdateRate) {
this.physicsUpdateRate = physicsUpdateRate;
}

/**
* Returns how long the graphics thread took in the last update.
*
* @return how long the graphics thread took in the last update.
*/
public int getLastGraphicsTime() {
return lastGraphicsTime;
}

/**
* Returns how long the input thread took in the last update.
*
* @return how long the input thread took in the last update.
*/
public int getLastInputTime() {
return lastInputTime;
}

/**
* Returns how long the physics thread took in the last update.
*
* @return how long the physics thread took in the last update.
*/
public int getLastPhysicsTime() {
return lastPhysicsTime;
}

/**
* Returns the count of faces rendered in the last graphics update.
*
* @return count of faces rendered in the last graphics update.
*/
public int getLastRenderedFaces() {
return lastRendereredFaces;
}

private void startGraphicsThread() {
graphicsThread = new Thread(new Runnable() {
@Override
public void run() {
int lastElapsed = 0;
while (settings.isPlaying()) {
lastElapsed = updateGraphics(lastElapsed);
while (isPlaying()) {
lastGraphicsTime = updateGraphics(lastGraphicsTime);
}
}
});
Expand All @@ -169,9 +299,8 @@ private void startInputThread() {
inputThread = new Thread(new Runnable() {
@Override
public void run() {
int lastElapsed = 0;
while (true) {
lastElapsed = updateInput(lastElapsed);
lastInputTime = updateInput(lastInputTime);
}
}
});
Expand All @@ -182,53 +311,49 @@ private void startPhysicsThread() {
physicsThread = new Thread(new Runnable() {
@Override
public void run() {
int lastElapsed = 0;
while (settings.isPlaying()) {
lastElapsed = updatePhysics(lastElapsed);
while (isPlaying()) {
lastPhysicsTime = updatePhysics(lastPhysicsTime);
}
}
});
physicsThread.start();
}

private int updateGraphics(int lastElapsed) {
long before = System.nanoTime();
long before = System.currentTimeMillis();
animator.animate(scene);
lastRendereredFaces = renderer.render(scene, displayBuffer);
if (sceneWindow != null) {
animator.animate(scene);
renderer.render(scene, sceneWindow.getDepthBuffer());
sceneWindow.repaint();
sceneWindow.draw();
}
int elapsed = (int)(System.nanoTime() - before);
Profiler.getInstance().getData().setGraphicsTime(elapsed);
int elapsed = (int)(System.currentTimeMillis() - before);
try {
Thread.sleep(1000/settings.getGraphicsUpdateRate());
Thread.sleep(1000/getGraphicsUpdateRate());
} catch (InterruptedException e) {
e.printStackTrace();
}
return elapsed;
}

private int updateInput(int lastElapsed) {
long before = System.nanoTime();
long before = System.currentTimeMillis();
keyManager.update();
mouseManager.update();
int elapsed = (int)(System.nanoTime() - before);
Profiler.getInstance().getData().setInputTime(elapsed);
int elapsed = (int)(System.currentTimeMillis() - before);
try {
Thread.sleep(1000/settings.getInputUpdateRate());
Thread.sleep(1000/getInputUpdateRate());
} catch (InterruptedException e) {
e.printStackTrace();
}
return elapsed;
}

private int updatePhysics(int lastElapsed) {
long before = System.nanoTime();

int elapsed = (int)(System.nanoTime() - before);
Profiler.getInstance().getData().setPhysicsTime(elapsed);
long before = System.currentTimeMillis();
physicsAnimator.animate(scene);
int elapsed = (int)(System.currentTimeMillis() - before);
try {
Thread.sleep(1000/settings.getPhysicsUpdateRate());
Thread.sleep(1000/getPhysicsUpdateRate());
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down
Loading

0 comments on commit 00afb42

Please sign in to comment.