Fix screen wrapping math

Rather than place entities directly on the screen edge boundary, add the
distance they were beyond the screen edge.

Store aabb in ModelComponent for use by multiple systems.
This commit is contained in:
Matt Low 2020-01-25 13:18:38 +04:00
parent 22fc504619
commit d8b82c4fee
3 changed files with 8 additions and 8 deletions

View File

@ -1,10 +1,11 @@
package com.me.asteroids.components;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Rectangle;
import com.me.common.ecs.Component;
public class ModelComponent extends Component {
public Polygon model;
public Rectangle aabb;
}

View File

@ -41,6 +41,7 @@ public class MovementSystem extends EntitySystem {
Polygon model = modelComponent.model;
model.setPosition(position.x, position.y);
model.setRotation(positionComponent.rotation - 90);
modelComponent.aabb = model.getBoundingRectangle();
}
}

View File

@ -1,6 +1,5 @@
package com.me.asteroids.systems;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.Constants;
@ -21,17 +20,16 @@ public class ScreenWrapSystem extends EntitySystem {
ModelComponent modelComponent = entity.getComponent(ModelComponent.class);
Vector2 position = positionComponent.position;
Polygon model = modelComponent.model;
Rectangle aabb = model.getBoundingRectangle();
Rectangle aabb = modelComponent.aabb;
// Check top/bottom edges
float minY = aabb.y;
float maxY = minY + aabb.height;
if (minY > Constants.HEIGHT) {
position.y = position.y - maxY;
position.y = (position.y - maxY) + (minY - Constants.HEIGHT);
return;
} else if (maxY < 0) {
position.y = Constants.HEIGHT + (position.y - minY);
position.y = Constants.HEIGHT + (position.y - minY) + maxY;
return;
}
@ -39,9 +37,9 @@ public class ScreenWrapSystem extends EntitySystem {
float minX = aabb.x;
float maxX = minX + aabb.width;
if (maxX < 0) {
position.x = Constants.WIDTH + (position.x - minX);
position.x = Constants.WIDTH + (position.x - minX) + maxX;
} else if (minX > Constants.WIDTH) {
position.x = position.x - maxX;
position.x = (position.x - maxX) + (minX - Constants.WIDTH);
}
}