Skip to content

Commit

Permalink
Bug fixes, optimizations
Browse files Browse the repository at this point in the history
- Made game loop constants such as the target FPS, target TPS, and max
updates before render editable from the r2dgame
- Fixed a bug where no entities were interpolating their position on
render, effectively capping the FPS at 30 (I’m an idiot)
- Fixed a bug where in certain situations editing the entity list while
ticking an entity in the list skips an entity when the iterator moves
on to the next entity to tick
- Removed obsolete functions in Map.java
- Added a fast collision check function in Map.java, collidesWithMap()
  • Loading branch information
Flafla2 committed Jan 19, 2014
1 parent 597c81f commit ef213fd
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 51 deletions.
28 changes: 22 additions & 6 deletions Remote2D/src/com/remote/remote2d/engine/Remote2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ public class Remote2D {
/**
* The speed at which the tick() function runs, in hertz. In other words, the target "Ticks per second"
*/
private static final double GAME_HERTZ = 30.0;
private static double GAME_HERTZ = 30.0;
/**
* The calculated value in nanoseconds on how much time <i>should</i> be in between tick functions, based on GAME_HERTZ.
*/
private static final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
private static double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
/**
* An arbitrary value dictating the max amount of ticks() we should do if we are playing catchup. The lower this is, the better
* render quality on slower machines, but physics/game logic will appear to slow down. Set this to -1 for 100% accuracy.
*/
private static final int MAX_UPDATES_BEFORE_RENDER = 5;
public static int MAX_UPDATES_BEFORE_RENDER = 5;
/**
* When the last time we ticked was. This is used to determine how many times we need to tick().
*/
Expand All @@ -65,8 +65,8 @@ public class Remote2D {
*/
private static double lastRenderTime = System.nanoTime();

private static final double TARGET_FPS = 60;
private static final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
private static double TARGET_FPS = 60;
private static double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;

/*----------GAME VARIABLES--------------*/
/**
Expand Down Expand Up @@ -142,7 +142,7 @@ private static void gameLoop()
} else if(!(e instanceof Remote2DException))
throw new Remote2DException(e);
}
Display.update();

lastRenderTime = now;

int thisSecond = (int) (lastUpdateTime / 1000000000);
Expand All @@ -152,6 +152,10 @@ private static void gameLoop()
fpsCounter = 0;
lastSecondTime = thisSecond;
}

Display.update();
//if(TARGET_FPS != -1)
// Display.sync((int)TARGET_FPS);

while ( now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES)
{
Expand All @@ -164,6 +168,18 @@ private static void gameLoop()
}
}

public static void setTargetFPS(int fps)
{
TARGET_FPS = fps;
TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
}

public static void setTargetTPS(int tps)
{
GAME_HERTZ = tps;
TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
}

private static void initGame()
{
Log.setLogger(new ConsoleLogger());
Expand Down
22 changes: 12 additions & 10 deletions Remote2D/src/com/remote/remote2d/engine/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ public Vector2 getDim()
* A general collider that encompasses this Entity's rendered area. NOT to be used
* for collision detection.
*/
public Collider getGeneralCollider()
public Collider getGeneralCollider(float interpolation)
{
return pos.getColliderWithDim(getDim());
return Interpolator.linearInterpolate(oldPos, pos, interpolation).getColliderWithDim(getDim());
}

/**
Expand All @@ -258,7 +258,7 @@ public float getGlobalRotation()
*/
public Vector2 getPosGlobal(float interpolation)
{
return Renderer.matrixMultiply(new Vector2(0,0), getTransformMatrix());
return Renderer.matrixMultiply(new Vector2(0,0), getTransformMatrix(1));
}

/**
Expand All @@ -269,7 +269,7 @@ public Vector2 getPosGlobal(float interpolation)
*/
public Vector2 getPosGlobal()
{
return Renderer.matrixMultiply(new Vector2(0,0), getTransformMatrix());
return Renderer.matrixMultiply(new Vector2(0,0), getTransformMatrix(1));
}

/**
Expand Down Expand Up @@ -316,23 +316,25 @@ public ArrayList<Collider> getPossibleColliders(Collider coll, Vector2 movement)

return retColliders;
}

/**
* The final matrix made up of the matrices of the combined transformations
* of this Entity and its parents.
*
* NOTE: Because parents have not been implemented yet this is equivalent to {@link #getLocalTransformMatrix()}
* @param interpolation
*/
public Matrix4f getTransformMatrix()
public Matrix4f getTransformMatrix(float interpolation)
{
Matrix4f mat = getLocalTransformMatrix();
Matrix4f mat = getLocalTransformMatrix(interpolation);

return mat;
}

public Matrix4f getLocalTransformMatrix() {
public Matrix4f getLocalTransformMatrix(float interpolation) {
Matrix4f mat = new Matrix4f();
Matrix4f.translate(new Vector3f(pos.x,pos.y,0), mat, mat);
Vector2 vec = Interpolator.linearInterpolate(oldPos, pos, interpolation);
Matrix4f.translate(new Vector3f(vec.x,vec.y,0), mat, mat);
Matrix4f.rotate((float)((rotation*Math.PI)/180f), new Vector3f(0,0,1), mat, mat);
return mat;
}
Expand Down Expand Up @@ -387,7 +389,7 @@ public void render(boolean editor, float interpolation)
components.get(x).renderBefore(editor, interpolation);

Renderer.pushMatrix();
Renderer.mult(getTransformMatrix());
Renderer.mult(getTransformMatrix(interpolation));

if(editor)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void addEntityToList(Entity e,int i)
entityList.add(i,e);
if(iterator != -1)
{
if(i < iterator)
if(i <= iterator)
iterator++;
}
}
Expand Down Expand Up @@ -107,12 +107,12 @@ public void removeEntityFromList(int i)
*/
public void render(boolean editor, float interpolation)
{
ColliderBox ra = map.camera.getMapRenderArea();
ColliderBox ra = map.camera.getMapRenderArea(interpolation);
for(int i=0;i<entityList.size();i++)
{
try
{
if(Collider.collides(entityList.get(i).getGeneralCollider(), ra))
if(Collider.collides(entityList.get(i).getGeneralCollider(interpolation), ra))
entityList.get(i).render(editor,interpolation);
} catch(Exception e)
{
Expand Down Expand Up @@ -257,7 +257,7 @@ public Entity instantiatePrefab(String path, int index)

Entity e = new Entity(map);
e.setPrefabPath(path);
entityList.add(index,e);
addEntityToList(e,index);
return e;
}

Expand Down
7 changes: 6 additions & 1 deletion Remote2D/src/com/remote/remote2d/engine/world/Camera.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public Vector2 getTruePos(float interpolation)

public ColliderBox getMapRenderArea()
{
return pos.subtract(getDimensions().divide(new Vector2(2))).getColliderWithDim(getDimensions());
return getMapRenderArea(1);
}

public ColliderBox getMapRenderArea(float interpolation)
{
return getTruePos(interpolation).subtract(getDimensions().divide(new Vector2(2))).getColliderWithDim(getDimensions());
}

public Vector2 getDimensions()
Expand Down
40 changes: 10 additions & 30 deletions Remote2D/src/com/remote/remote2d/engine/world/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,40 +166,20 @@ public Vector2 getCorrection(Collider coll,Vector2 velocity)
}

/**
* Uses quicksort to sort a batch of collisions by distance traveled.
* @param collection
* @return the sorted batch
* Returns if the given collider collides with any other collider in the map
* @param coll Collider to test
*/
public static ArrayList<Collision> sortColliders(ArrayList<Collision> collection)
public boolean collidesWithMap(Collider coll)
{
if(collection.size() <= 1)
return collection;
//Log.debug("sort colliders:"+collection.size());
int pivot = collection.size()/2;
boolean odd = collection.size()%2 != 0;
ArrayList<Collision> less = new ArrayList<Collision>();
ArrayList<Collision> more = new ArrayList<Collision>();
for(int x=0;x<collection.size();x++)
for(int x=0;x<entities.size();x++)
{
int distanceThis = collection.get(x).max-collection.get(x).min;
int distancePivot;
if(odd)
distancePivot = (collection.get(pivot).max+collection.get(pivot+1).max)/2-(collection.get(pivot).min+collection.get(pivot+1).min)/2;
else
distancePivot = collection.get(pivot).max-collection.get(pivot).min;
//Log.debug(x+" "+distancePivot+" "+distanceThis);
if(distanceThis > distancePivot)//if the shortest correction is longer than the pivot(so shortest time to collision), we set it towards the front
less.add(collection.get(x));
else
more.add(collection.get(x));
Entity e = entities.get(x);
ArrayList<Collider> colliders = e.getColliders();
for(Collider c : colliders)
if(Collider.collides(coll, c.getTransformedCollider(e.pos)))
return true;
}

//Log.debug("less:"+less.size()+" more:"+more.size());

ArrayList<Collision> returnArray = sortColliders(less);
returnArray.addAll(sortColliders(more));

return returnArray;
return false;
}

public Entity getTopEntityAtPoint(Vector2 vec)
Expand Down

0 comments on commit ef213fd

Please sign in to comment.