From c969b87eca7f412019df9457e57f4f5d066d65b0 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Thu, 30 Jan 2020 14:03:47 +0400 Subject: [PATCH] Add ColliderComponent and add it to entities that can be collided with. Only entities with ColliderComponent will be checked for collisions. Also, don't check collisions with inactive/removed entities. --- core/src/com/me/asteroids/EntityFactory.java | 7 +++++++ .../com/me/asteroids/components/ColliderComponent.java | 9 +++++++++ core/src/com/me/asteroids/screens/GameScreen.java | 2 ++ core/src/com/me/asteroids/systems/CollisionSystem.java | 10 +++++++++- 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 core/src/com/me/asteroids/components/ColliderComponent.java diff --git a/core/src/com/me/asteroids/EntityFactory.java b/core/src/com/me/asteroids/EntityFactory.java index a0d2d4c..c93be42 100644 --- a/core/src/com/me/asteroids/EntityFactory.java +++ b/core/src/com/me/asteroids/EntityFactory.java @@ -6,6 +6,7 @@ import com.badlogic.gdx.math.Vector2; import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AsteroidComponent; import com.me.asteroids.components.BulletComponent; +import com.me.asteroids.components.ColliderComponent; import com.me.asteroids.components.DebrisComponent; import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.PlayerComponent; @@ -28,6 +29,8 @@ public class EntityFactory { private static final Vector2 tmpA = new Vector2(); private static final Vector2 tmpB = new Vector2(); + private static final ColliderComponent COLLIDER = new ColliderComponent(); + private static Entity createEntity(Engine engine) { Entity entity = engine.createEntity(); if (Constants.DEBUG) { @@ -64,6 +67,7 @@ public class EntityFactory { player.addComponent(velocity); player.addComponent(model); player.addComponent(accel); + player.addComponent(COLLIDER); player.addComponent(new PlayerComponent()); return player; } @@ -161,6 +165,7 @@ public class EntityFactory { bullet.addComponent(position); bullet.addComponent(velocity); bullet.addComponent(model); + bullet.addComponent(COLLIDER); bullet.addComponent(new BulletComponent()); return bullet; } @@ -185,6 +190,7 @@ public class EntityFactory { asteroid.addComponent(position); asteroid.addComponent(velocity); asteroid.addComponent(model); + asteroid.addComponent(COLLIDER); asteroid.addComponent(new AsteroidComponent()); return asteroid; } @@ -236,6 +242,7 @@ public class EntityFactory { split.addComponent(position); split.addComponent(velocity); split.addComponent(asteroidComponent); + split.addComponent(COLLIDER); entities[i] = split; } diff --git a/core/src/com/me/asteroids/components/ColliderComponent.java b/core/src/com/me/asteroids/components/ColliderComponent.java new file mode 100644 index 0000000..c61d3af --- /dev/null +++ b/core/src/com/me/asteroids/components/ColliderComponent.java @@ -0,0 +1,9 @@ +package com.me.asteroids.components; + +import com.me.common.ecs.Component; + +/** + * Add this to Entities that should be checked for collisions with other entities. + */ +public class ColliderComponent implements Component { +} diff --git a/core/src/com/me/asteroids/screens/GameScreen.java b/core/src/com/me/asteroids/screens/GameScreen.java index 81d5ff0..6d798a8 100644 --- a/core/src/com/me/asteroids/screens/GameScreen.java +++ b/core/src/com/me/asteroids/screens/GameScreen.java @@ -11,6 +11,7 @@ import com.me.asteroids.Graphics; import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AsteroidComponent; import com.me.asteroids.components.BulletComponent; +import com.me.asteroids.components.ColliderComponent; import com.me.asteroids.components.DebrisComponent; import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.PlayerComponent; @@ -58,6 +59,7 @@ public class GameScreen extends Screen implements Listener { engine.registerComponentClass(BulletComponent.class); engine.registerComponentClass(AsteroidComponent.class); engine.registerComponentClass(DebrisComponent.class); + engine.registerComponentClass(ColliderComponent.class); engine.registerComponentClass(PositionComponent.class); engine.registerComponentClass(VelocityComponent.class); engine.registerComponentClass(AccelerationComponent.class); diff --git a/core/src/com/me/asteroids/systems/CollisionSystem.java b/core/src/com/me/asteroids/systems/CollisionSystem.java index dc37c04..7e06a88 100644 --- a/core/src/com/me/asteroids/systems/CollisionSystem.java +++ b/core/src/com/me/asteroids/systems/CollisionSystem.java @@ -2,6 +2,7 @@ package com.me.asteroids.systems; import com.badlogic.gdx.math.Rectangle; import com.me.asteroids.EventFactory; +import com.me.asteroids.components.ColliderComponent; import com.me.asteroids.components.ModelComponent; import com.me.common.ecs.ComponentMapper; import com.me.common.ecs.Engine; @@ -13,7 +14,7 @@ public class CollisionSystem extends EntitySystem { private ComponentMapper modelMapper; public CollisionSystem(Engine engine) { - super(engine, ModelComponent.class); + super(engine, ColliderComponent.class, ModelComponent.class); modelMapper = engine.getComponentMapper(ModelComponent.class); } @@ -22,10 +23,17 @@ public class CollisionSystem extends EntitySystem { Entity[] entities = getEntities().items; for (int i = 0, n = getEntities().size; i < n-1; i++) { Entity entityA = entities[i]; + if (!entityA.isActive() || entityA.isRemoved()) { + continue; + } + Rectangle aabbA = modelMapper.get(entityA).model.getBoundingBox(); for (int j = i + 1; j < n; j++) { Entity entityB = entities[j]; + if (!entityB.isActive() || entityB.isRemoved()) { + continue; + } Rectangle aabbB = modelMapper.get(entityB).model.getBoundingBox(); if (aabbA.overlaps(aabbB)) {