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.
This commit is contained in:
Matt Low 2020-01-30 14:03:47 +04:00
parent 3b1c075102
commit c969b87eca
4 changed files with 27 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.AsteroidComponent; import com.me.asteroids.components.AsteroidComponent;
import com.me.asteroids.components.BulletComponent; import com.me.asteroids.components.BulletComponent;
import com.me.asteroids.components.ColliderComponent;
import com.me.asteroids.components.DebrisComponent; import com.me.asteroids.components.DebrisComponent;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PlayerComponent;
@ -28,6 +29,8 @@ public class EntityFactory {
private static final Vector2 tmpA = new Vector2(); private static final Vector2 tmpA = new Vector2();
private static final Vector2 tmpB = new Vector2(); private static final Vector2 tmpB = new Vector2();
private static final ColliderComponent COLLIDER = new ColliderComponent();
private static Entity createEntity(Engine engine) { private static Entity createEntity(Engine engine) {
Entity entity = engine.createEntity(); Entity entity = engine.createEntity();
if (Constants.DEBUG) { if (Constants.DEBUG) {
@ -64,6 +67,7 @@ public class EntityFactory {
player.addComponent(velocity); player.addComponent(velocity);
player.addComponent(model); player.addComponent(model);
player.addComponent(accel); player.addComponent(accel);
player.addComponent(COLLIDER);
player.addComponent(new PlayerComponent()); player.addComponent(new PlayerComponent());
return player; return player;
} }
@ -161,6 +165,7 @@ public class EntityFactory {
bullet.addComponent(position); bullet.addComponent(position);
bullet.addComponent(velocity); bullet.addComponent(velocity);
bullet.addComponent(model); bullet.addComponent(model);
bullet.addComponent(COLLIDER);
bullet.addComponent(new BulletComponent()); bullet.addComponent(new BulletComponent());
return bullet; return bullet;
} }
@ -185,6 +190,7 @@ public class EntityFactory {
asteroid.addComponent(position); asteroid.addComponent(position);
asteroid.addComponent(velocity); asteroid.addComponent(velocity);
asteroid.addComponent(model); asteroid.addComponent(model);
asteroid.addComponent(COLLIDER);
asteroid.addComponent(new AsteroidComponent()); asteroid.addComponent(new AsteroidComponent());
return asteroid; return asteroid;
} }
@ -236,6 +242,7 @@ public class EntityFactory {
split.addComponent(position); split.addComponent(position);
split.addComponent(velocity); split.addComponent(velocity);
split.addComponent(asteroidComponent); split.addComponent(asteroidComponent);
split.addComponent(COLLIDER);
entities[i] = split; entities[i] = split;
} }

View File

@ -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 {
}

View File

@ -11,6 +11,7 @@ import com.me.asteroids.Graphics;
import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.AsteroidComponent; import com.me.asteroids.components.AsteroidComponent;
import com.me.asteroids.components.BulletComponent; import com.me.asteroids.components.BulletComponent;
import com.me.asteroids.components.ColliderComponent;
import com.me.asteroids.components.DebrisComponent; import com.me.asteroids.components.DebrisComponent;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PlayerComponent;
@ -58,6 +59,7 @@ public class GameScreen extends Screen implements Listener {
engine.registerComponentClass(BulletComponent.class); engine.registerComponentClass(BulletComponent.class);
engine.registerComponentClass(AsteroidComponent.class); engine.registerComponentClass(AsteroidComponent.class);
engine.registerComponentClass(DebrisComponent.class); engine.registerComponentClass(DebrisComponent.class);
engine.registerComponentClass(ColliderComponent.class);
engine.registerComponentClass(PositionComponent.class); engine.registerComponentClass(PositionComponent.class);
engine.registerComponentClass(VelocityComponent.class); engine.registerComponentClass(VelocityComponent.class);
engine.registerComponentClass(AccelerationComponent.class); engine.registerComponentClass(AccelerationComponent.class);

View File

@ -2,6 +2,7 @@ package com.me.asteroids.systems;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.me.asteroids.EventFactory; import com.me.asteroids.EventFactory;
import com.me.asteroids.components.ColliderComponent;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.common.ecs.ComponentMapper; import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
@ -13,7 +14,7 @@ public class CollisionSystem extends EntitySystem {
private ComponentMapper<ModelComponent> modelMapper; private ComponentMapper<ModelComponent> modelMapper;
public CollisionSystem(Engine engine) { public CollisionSystem(Engine engine) {
super(engine, ModelComponent.class); super(engine, ColliderComponent.class, ModelComponent.class);
modelMapper = engine.getComponentMapper(ModelComponent.class); modelMapper = engine.getComponentMapper(ModelComponent.class);
} }
@ -22,10 +23,17 @@ public class CollisionSystem extends EntitySystem {
Entity[] entities = getEntities().items; Entity[] entities = getEntities().items;
for (int i = 0, n = getEntities().size; i < n-1; i++) { for (int i = 0, n = getEntities().size; i < n-1; i++) {
Entity entityA = entities[i]; Entity entityA = entities[i];
if (!entityA.isActive() || entityA.isRemoved()) {
continue;
}
Rectangle aabbA = modelMapper.get(entityA).model.getBoundingBox(); Rectangle aabbA = modelMapper.get(entityA).model.getBoundingBox();
for (int j = i + 1; j < n; j++) { for (int j = i + 1; j < n; j++) {
Entity entityB = entities[j]; Entity entityB = entities[j];
if (!entityB.isActive() || entityB.isRemoved()) {
continue;
}
Rectangle aabbB = modelMapper.get(entityB).model.getBoundingBox(); Rectangle aabbB = modelMapper.get(entityB).model.getBoundingBox();
if (aabbA.overlaps(aabbB)) { if (aabbA.overlaps(aabbB)) {