From d8b82c4feea986c36ac240ec25272ee975252dff Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 25 Jan 2020 13:18:38 +0400 Subject: [PATCH] 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. --- .../com/me/asteroids/components/ModelComponent.java | 3 ++- .../src/com/me/asteroids/systems/MovementSystem.java | 1 + .../com/me/asteroids/systems/ScreenWrapSystem.java | 12 +++++------- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/com/me/asteroids/components/ModelComponent.java b/core/src/com/me/asteroids/components/ModelComponent.java index 57e4c69..f7408dd 100644 --- a/core/src/com/me/asteroids/components/ModelComponent.java +++ b/core/src/com/me/asteroids/components/ModelComponent.java @@ -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; } diff --git a/core/src/com/me/asteroids/systems/MovementSystem.java b/core/src/com/me/asteroids/systems/MovementSystem.java index 75b5fb9..7c344bf 100644 --- a/core/src/com/me/asteroids/systems/MovementSystem.java +++ b/core/src/com/me/asteroids/systems/MovementSystem.java @@ -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(); } } diff --git a/core/src/com/me/asteroids/systems/ScreenWrapSystem.java b/core/src/com/me/asteroids/systems/ScreenWrapSystem.java index 345438a..4c519e5 100644 --- a/core/src/com/me/asteroids/systems/ScreenWrapSystem.java +++ b/core/src/com/me/asteroids/systems/ScreenWrapSystem.java @@ -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); } }