HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Orginizing/Optimising my collision detection setup

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
collisionorginizingoptimisingsetupdetection

Problem

I'm creating a 2D tile based platformer with AABB (Axis aligned bounding boxes), and it works, I just want to organize and optimize my setup. Here are methods in my player class and entity class that are relevant to the collision. How could I make this better? I'm harshly looking at that public boolean[] method, but I cant figure out another way without using state.

AbstractEntity.java

```
public boolean[] getCornersAreSolid(double x, double y) {
int leftTile = (int)(x / Tile.SIZE);
int rightTile = (int)((x + this.collisionBox.getWidth()) / Tile.SIZE);
int topTile = (int)(y / Tile.SIZE);
int bottomTile = (int)((y + this.collisionBox.getHeight()) / Tile.SIZE);

boolean topLeft;
boolean topRight;
boolean bottomLeft;
boolean bottomRight;

if(leftTile = map.getWidth() || topTile = map.getHeight())
topLeft = false;
else
topLeft = map.getTileAt(leftTile, topTile).getAttribute(Attribute.SOLID);
if(rightTile = map.getWidth() || topTile = map.getHeight())
topRight = false;
else
topRight = map.getTileAt(rightTile, topTile).getAttribute(Attribute.SOLID);
if(leftTile = map.getWidth() || bottomTile = map.getHeight())
bottomLeft = false;
else
bottomLeft = map.getTileAt(leftTile, bottomTile).getAttribute(Attribute.SOLID);
if(rightTile = map.getWidth() || bottomTile = map.getHeight())
bottomRight = false;
else
bottomRight = map.getTileAt(rightTile, bottomTile).getAttribute(Attribute.SOLID);

return new boolean[]{topLeft, topRight, bottomLeft, bottomRight};
}
/*
* @return next position
*/
public Vec2D getNextPosition() {

int currCol = (int)getX() / Tile.SIZE;
int currRow = (int)getY() / Tile.SIZE;

double xdest = getX() + this.velocity.x;
double ydest = getY() + this.velocity.y;

double xtemp = getX();
double ytemp = getY();

boolean[] corners = getCornersAreSolid(getX(), ydest);
boolean topLeft = corners[0];

Solution

You play around with different ordering (depending on you data) it may be faster to check absolutes first, or last.

if(leftTile = map.getWidth() || topTile < 0 ||....


can become

if(leftTile = map.getWidth()...


Also, notice that you throw away half your answers after a fast check, plenty of room for improvement.

if(this.velocity.y < 0) { ...


Depending on how many objects versus tiles you have or if the collision map is static, consider rendering the collision map to a array to eliminate lookup overhead

You are creating a new Vec2d at the end of each test, you can reuse a object or just set the result where it belongs.

This is just suggestions, always benchmark. Always.

Code Snippets

if(leftTile < 0 || leftTile >= map.getWidth() || topTile < 0 ||....
if(leftTile < 0 || topTile < 0 || leftTile >= map.getWidth()...
if(this.velocity.y < 0) { ...

Context

StackExchange Code Review Q#49072, answer score: 6

Revisions (0)

No revisions yet.