From 8abf594c8a128b88afaa885aa44089450ce222fb Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 10 Jun 2014 16:59:21 -0430 Subject: [PATCH] Added collision detection. --- .../AutomaticMovementComponent.java | 35 ++++++ .../CollisionDetectionComponent.java} | 12 +- .../nxtar/entities/BombGameEntityCreator.java | 38 ++++-- .../products => input}/GamepadUserInput.java | 2 +- .../products => input}/KeyboardUserInput.java | 2 +- .../TouchUserInput.java} | 23 ++-- .../products => input}/UserInput.java | 9 +- .../nxtar/network/VideoStreamingThread.java | 8 -- .../network/monitors/VideoFrameMonitor.java | 4 - .../ciens/ccg/nxtar/states/InGameState.java | 68 ++++++++--- .../ccg/nxtar/states/MainMenuStateBase.java | 2 +- .../systems/CollisionDetectionSystem.java | 51 +++++++- .../systems/RobotArmPositioningSystem.java | 114 +++++++++++++++--- .../ciens/ccg/nxtar/utils/GameSettings.java | 5 +- src/ve/ucv/ciens/ccg/nxtar/utils/Utils.java | 24 ++++ 15 files changed, 310 insertions(+), 87 deletions(-) create mode 100644 src/ve/ucv/ciens/ccg/nxtar/components/AutomaticMovementComponent.java rename src/ve/ucv/ciens/ccg/nxtar/{factories/products/TouchUserInput.java => components/CollisionDetectionComponent.java} (73%) rename src/ve/ucv/ciens/ccg/nxtar/{factories/products => input}/GamepadUserInput.java (94%) rename src/ve/ucv/ciens/ccg/nxtar/{factories/products => input}/KeyboardUserInput.java (95%) rename src/ve/ucv/ciens/ccg/nxtar/{factories/UserInputFactory.java => input/TouchUserInput.java} (51%) rename src/ve/ucv/ciens/ccg/nxtar/{factories/products => input}/UserInput.java (74%) create mode 100644 src/ve/ucv/ciens/ccg/nxtar/utils/Utils.java diff --git a/src/ve/ucv/ciens/ccg/nxtar/components/AutomaticMovementComponent.java b/src/ve/ucv/ciens/ccg/nxtar/components/AutomaticMovementComponent.java new file mode 100644 index 0000000..99f654f --- /dev/null +++ b/src/ve/ucv/ciens/ccg/nxtar/components/AutomaticMovementComponent.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2014 Miguel Angel Astor Romero + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ve.ucv.ciens.ccg.nxtar.components; + +import com.artemis.Component; +import com.badlogic.gdx.math.Vector3; + +public class AutomaticMovementComponent extends Component { + public boolean moving; + public boolean forward; + public Vector3 startPoint; + public Vector3 endPoint; + public float distance; + + public AutomaticMovementComponent(){ + this.moving = false; + this.forward = true; + this.startPoint = new Vector3(); + this.endPoint = new Vector3(); + this.distance = 0.0f; + } +} diff --git a/src/ve/ucv/ciens/ccg/nxtar/factories/products/TouchUserInput.java b/src/ve/ucv/ciens/ccg/nxtar/components/CollisionDetectionComponent.java similarity index 73% rename from src/ve/ucv/ciens/ccg/nxtar/factories/products/TouchUserInput.java rename to src/ve/ucv/ciens/ccg/nxtar/components/CollisionDetectionComponent.java index 4a4ab2d..4fee5a1 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/factories/products/TouchUserInput.java +++ b/src/ve/ucv/ciens/ccg/nxtar/components/CollisionDetectionComponent.java @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package ve.ucv.ciens.ccg.nxtar.factories.products; +package ve.ucv.ciens.ccg.nxtar.components; -import com.badlogic.gdx.math.collision.Ray; +import com.artemis.Component; -public class TouchUserInput extends UserInput { - public Ray movementRay; +public class CollisionDetectionComponent extends Component { + public boolean colliding; - public TouchUserInput(){ - movementRay = null; + public CollisionDetectionComponent(){ + this.colliding = false; } } diff --git a/src/ve/ucv/ciens/ccg/nxtar/entities/BombGameEntityCreator.java b/src/ve/ucv/ciens/ccg/nxtar/entities/BombGameEntityCreator.java index f1ec915..8f64be1 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/entities/BombGameEntityCreator.java +++ b/src/ve/ucv/ciens/ccg/nxtar/entities/BombGameEntityCreator.java @@ -16,8 +16,10 @@ package ve.ucv.ciens.ccg.nxtar.entities; import ve.ucv.ciens.ccg.nxtar.components.AnimationComponent; +import ve.ucv.ciens.ccg.nxtar.components.AutomaticMovementComponent; import ve.ucv.ciens.ccg.nxtar.components.BombComponent; import ve.ucv.ciens.ccg.nxtar.components.BombComponent.bomb_type_t; +import ve.ucv.ciens.ccg.nxtar.components.CollisionDetectionComponent; import ve.ucv.ciens.ccg.nxtar.components.CollisionModelComponent; import ve.ucv.ciens.ccg.nxtar.components.EnvironmentComponent; import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent; @@ -26,8 +28,10 @@ import ve.ucv.ciens.ccg.nxtar.components.RenderModelComponent; import ve.ucv.ciens.ccg.nxtar.components.ShaderComponent; import ve.ucv.ciens.ccg.nxtar.components.VisibilityComponent; import ve.ucv.ciens.ccg.nxtar.graphics.shaders.DirectionalLightPerPixelShader; +import ve.ucv.ciens.ccg.nxtar.systems.CollisionDetectionSystem; import com.artemis.Entity; +import com.artemis.managers.GroupManager; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.graphics.Color; @@ -64,8 +68,9 @@ public class BombGameEntityCreator extends EntityCreatorBase{ } } - private Shader shader; - private int currentBombId; + private Shader shader; + private int currentBombId; + private GroupManager groupManager; // Render models. private Model robotArmModel = null; @@ -82,7 +87,7 @@ public class BombGameEntityCreator extends EntityCreatorBase{ private Model wiresBombModelWire1 = null; private Model wiresBombModelWire2 = null; private Model wiresBombModelWire3 = null; - private Model easterEggModel = null; + private Model monkeyModel = null; // Collision models. private Model robotArmCollisionModel = null; @@ -99,7 +104,6 @@ public class BombGameEntityCreator extends EntityCreatorBase{ private Model wiresBombCollisionModelWire1 = null; private Model wiresBombCollisionModelWire2 = null; private Model wiresBombCollisionModelWire3 = null; - private Model easterEggCollisionModel = null; public BombGameEntityCreator(){ currentBombId = 0; @@ -123,7 +127,7 @@ public class BombGameEntityCreator extends EntityCreatorBase{ manager.load("models/render_models/bomb_game/cable_1.g3db", Model.class); manager.load("models/render_models/bomb_game/cable_2.g3db", Model.class); manager.load("models/render_models/bomb_game/cable_3.g3db", Model.class); - // manager.load("models/render_models/bomb_game/", Model.class); + manager.load("models/render_models/bomb_game/monkey.g3db", Model.class); // Load the collision models. manager.load("models/collision_models/bomb_game/robot_arm_col.g3db", Model.class); @@ -143,12 +147,14 @@ public class BombGameEntityCreator extends EntityCreatorBase{ manager.load("models/collision_models/bomb_game/cable_1_col.g3db", Model.class); manager.load("models/collision_models/bomb_game/cable_2_col.g3db", Model.class); manager.load("models/collision_models/bomb_game/cable_3_col.g3db", Model.class); - // manager.load("models/collision_models/bomb_game/door.g3db", Model.class); } @Override public void createAllEntities(){ EntityParameters parameters; + Entity monkey; + + groupManager = world.getManager(GroupManager.class); // Create and set the lighting. parameters = new EntityParameters(); @@ -186,7 +192,15 @@ public class BombGameEntityCreator extends EntityCreatorBase{ parameters.markerCode = 91; addDoor(parameters); - // TODO: Add easter egg. + // Add the monkey. + monkey = world.createEntity(); + monkey.addComponent(new RenderModelComponent(monkeyModel)); + monkey.addComponent(new GeometryComponent()); + monkey.addComponent(new MarkerCodeComponent(1023)); + monkey.addComponent(new VisibilityComponent()); + monkey.addComponent(new ShaderComponent(shader)); + monkey.addComponent(new EnvironmentComponent(parameters.environment)); + monkey.addToWorld(); entitiesCreated = true; } @@ -217,11 +231,13 @@ public class BombGameEntityCreator extends EntityCreatorBase{ private void addRobotArm(EntityParameters parameters){ Entity robotArm = world.createEntity(); - robotArm.addComponent(new GeometryComponent(new Vector3(), new Matrix3(), new Vector3(1, 1, 1))); + robotArm.addComponent(new GeometryComponent(new Vector3(-1.0f, 0.0f, 0.0f), new Matrix3(), new Vector3(1, 1, 1))); robotArm.addComponent(new EnvironmentComponent(parameters.environment)); robotArm.addComponent(new ShaderComponent(parameters.shader)); robotArm.addComponent(new RenderModelComponent(robotArmModel)); robotArm.addComponent(new CollisionModelComponent(robotArmCollisionModel)); + robotArm.addComponent(new AutomaticMovementComponent()); + robotArm.addComponent(new CollisionDetectionComponent()); robotArm.addToWorld(); } @@ -264,6 +280,7 @@ public class BombGameEntityCreator extends EntityCreatorBase{ throw new IllegalArgumentException("Unrecognized bomb type: " + Integer.toString(type.getValue())); // Add the bomb and increase the id for the next one. + groupManager.add(bomb, CollisionDetectionSystem.COLLIDABLE_OBJECT); bomb.addToWorld(); currentBombId++; } @@ -320,6 +337,7 @@ public class BombGameEntityCreator extends EntityCreatorBase{ thing.addComponent(new BombComponent(bomb)); thing.addComponent(new VisibilityComponent()); thing.addComponent(new MarkerCodeComponent(parameters.markerCode)); + groupManager.add(thing, CollisionDetectionSystem.COLLIDABLE_OBJECT); if(DEBUG_RENDER_PARAPHERNALIA_COLLISION_MODELS) addDebugCollisionModelRenderingEntity(collisionModel, parameters, false); @@ -351,6 +369,7 @@ public class BombGameEntityCreator extends EntityCreatorBase{ door.addComponent(new VisibilityComponent()); doorInstance = door.getComponent(RenderModelComponent.class).instance; door.addComponent(new AnimationComponent(doorInstance, parameters.nextAnimation, parameters.loopAnimation)); + groupManager.add(door, CollisionDetectionSystem.COLLIDABLE_OBJECT); door.addToWorld(); if(DEBUG_RENDER_DOOR_COLLISION_MODELS){ @@ -396,7 +415,7 @@ public class BombGameEntityCreator extends EntityCreatorBase{ wiresBombModelWire1 = manager.get("models/render_models/bomb_game/cable_1.g3db", Model.class); wiresBombModelWire2 = manager.get("models/render_models/bomb_game/cable_2.g3db", Model.class); wiresBombModelWire3 = manager.get("models/render_models/bomb_game/cable_3.g3db", Model.class); - // easterEggModel = manager.get("models/render_models/bomb_game/", Model.class); + monkeyModel = manager.get("models/render_models/bomb_game/monkey.g3db", Model.class); // Get the collision models. robotArmCollisionModel = manager.get("models/collision_models/bomb_game/robot_arm_col.g3db", Model.class); @@ -416,6 +435,5 @@ public class BombGameEntityCreator extends EntityCreatorBase{ wiresBombCollisionModelWire1 = manager.get("models/collision_models/bomb_game/cable_1_col.g3db", Model.class); wiresBombCollisionModelWire2 = manager.get("models/collision_models/bomb_game/cable_2_col.g3db", Model.class); wiresBombCollisionModelWire3 = manager.get("models/collision_models/bomb_game/cable_3_col.g3db", Model.class); - // easterEggCollisionModel = manager.get("models/collision_models/bomb_game/door.g3db", Model.class); } } diff --git a/src/ve/ucv/ciens/ccg/nxtar/factories/products/GamepadUserInput.java b/src/ve/ucv/ciens/ccg/nxtar/input/GamepadUserInput.java similarity index 94% rename from src/ve/ucv/ciens/ccg/nxtar/factories/products/GamepadUserInput.java rename to src/ve/ucv/ciens/ccg/nxtar/input/GamepadUserInput.java index f39a496..59b79d2 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/factories/products/GamepadUserInput.java +++ b/src/ve/ucv/ciens/ccg/nxtar/input/GamepadUserInput.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package ve.ucv.ciens.ccg.nxtar.factories.products; +package ve.ucv.ciens.ccg.nxtar.input; public class GamepadUserInput extends UserInput { public float axisLeftX; diff --git a/src/ve/ucv/ciens/ccg/nxtar/factories/products/KeyboardUserInput.java b/src/ve/ucv/ciens/ccg/nxtar/input/KeyboardUserInput.java similarity index 95% rename from src/ve/ucv/ciens/ccg/nxtar/factories/products/KeyboardUserInput.java rename to src/ve/ucv/ciens/ccg/nxtar/input/KeyboardUserInput.java index 813c6fb..31949ea 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/factories/products/KeyboardUserInput.java +++ b/src/ve/ucv/ciens/ccg/nxtar/input/KeyboardUserInput.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package ve.ucv.ciens.ccg.nxtar.factories.products; +package ve.ucv.ciens.ccg.nxtar.input; public class KeyboardUserInput extends UserInput { public boolean keyLeft; diff --git a/src/ve/ucv/ciens/ccg/nxtar/factories/UserInputFactory.java b/src/ve/ucv/ciens/ccg/nxtar/input/TouchUserInput.java similarity index 51% rename from src/ve/ucv/ciens/ccg/nxtar/factories/UserInputFactory.java rename to src/ve/ucv/ciens/ccg/nxtar/input/TouchUserInput.java index 26eee80..16492f2 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/factories/UserInputFactory.java +++ b/src/ve/ucv/ciens/ccg/nxtar/input/TouchUserInput.java @@ -13,23 +13,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package ve.ucv.ciens.ccg.nxtar.factories; +package ve.ucv.ciens.ccg.nxtar.input; -import ve.ucv.ciens.ccg.nxtar.factories.products.GamepadUserInput; -import ve.ucv.ciens.ccg.nxtar.factories.products.KeyboardUserInput; -import ve.ucv.ciens.ccg.nxtar.factories.products.TouchUserInput; -import ve.ucv.ciens.ccg.nxtar.factories.products.UserInput; +import com.badlogic.gdx.math.Vector3; -public abstract class UserInputFactory{ - public static UserInput createTouchUserInput(){ - return new TouchUserInput(); +public class TouchUserInput extends UserInput { + public Vector3 userTouchEndPoint; + + public TouchUserInput(){ + this.userTouchEndPoint = null; } - public static UserInput createGamepadUserInput(){ - return new GamepadUserInput(); + public TouchUserInput(Vector3 userTouchEndPoint){ + this.userTouchEndPoint = new Vector3(userTouchEndPoint); } - public static UserInput createKeyboardUserInput(){ - return new KeyboardUserInput(); + public TouchUserInput(float x, float y, float z){ + this.userTouchEndPoint = new Vector3(x, y, z); } } diff --git a/src/ve/ucv/ciens/ccg/nxtar/factories/products/UserInput.java b/src/ve/ucv/ciens/ccg/nxtar/input/UserInput.java similarity index 74% rename from src/ve/ucv/ciens/ccg/nxtar/factories/products/UserInput.java rename to src/ve/ucv/ciens/ccg/nxtar/input/UserInput.java index 8213aa8..6e38daa 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/factories/products/UserInput.java +++ b/src/ve/ucv/ciens/ccg/nxtar/input/UserInput.java @@ -13,11 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package ve.ucv.ciens.ccg.nxtar.factories.products; +package ve.ucv.ciens.ccg.nxtar.input; -import ve.ucv.ciens.ccg.nxtar.factories.UserInputFactory; - -/** - * Tag class for the {@link UserInputFactory} products. - */ -public abstract class UserInput{} +public abstract class UserInput{ } diff --git a/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java b/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java index 3230826..4110053 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java +++ b/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; -import java.net.Socket; import ve.ucv.ciens.ccg.networkdata.VideoFrameDataMessage; import ve.ucv.ciens.ccg.nxtar.interfaces.ApplicationEventsListener; @@ -41,7 +40,6 @@ public class VideoStreamingThread extends Thread{ private boolean pause; private boolean coreNotified; private Object protocolPauseMonitor; - private Socket client; private VideoFrameMonitor frameMonitor; private long then; private long now; @@ -236,12 +234,6 @@ public class VideoStreamingThread extends Thread{ } } - try{ - client.close(); - }catch(IOException io){ - Gdx.app.error(TAG, CLASS_NAME + ".run() :: Error closing client socket.", io); - } - Gdx.app.debug(TAG, CLASS_NAME + ".run() :: Thread finished."); } diff --git a/src/ve/ucv/ciens/ccg/nxtar/network/monitors/VideoFrameMonitor.java b/src/ve/ucv/ciens/ccg/nxtar/network/monitors/VideoFrameMonitor.java index a0902ec..4d598ff 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/network/monitors/VideoFrameMonitor.java +++ b/src/ve/ucv/ciens/ccg/nxtar/network/monitors/VideoFrameMonitor.java @@ -61,14 +61,11 @@ public class VideoFrameMonitor{ public void setNewFrame(byte[] frame){ byte[] temp; - Gdx.app.debug(TAG, CLASS_NAME + ".setNewFrame() :: Loading new frame in frameA."); frameA = frame; temp = frameA; synchronized(frameMonitor){ - Gdx.app.debug(TAG, CLASS_NAME + ".setNewFrame() :: Swapping frameA and frameB."); frameA = frameB; frameB = temp; - Gdx.app.debug(TAG, CLASS_NAME + ".setNewFrame() :: Swapping done."); } } @@ -76,7 +73,6 @@ public class VideoFrameMonitor{ byte[] frame; synchronized(frameMonitor){ - //Gdx.app.debug(TAG, CLASS_NAME + ".getCurrentFrame() :: Fetching frameB."); frame = frameB; } return frame; diff --git a/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java b/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java index 8d00f7c..08644af 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java +++ b/src/ve/ucv/ciens/ccg/nxtar/states/InGameState.java @@ -19,13 +19,14 @@ import ve.ucv.ciens.ccg.networkdata.MotorEvent; import ve.ucv.ciens.ccg.networkdata.MotorEvent.motor_t; import ve.ucv.ciens.ccg.nxtar.NxtARCore; import ve.ucv.ciens.ccg.nxtar.NxtARCore.game_states_t; -import ve.ucv.ciens.ccg.nxtar.factories.UserInputFactory; -import ve.ucv.ciens.ccg.nxtar.factories.products.UserInput; import ve.ucv.ciens.ccg.nxtar.graphics.CustomPerspectiveCamera; +import ve.ucv.ciens.ccg.nxtar.input.GamepadUserInput; +import ve.ucv.ciens.ccg.nxtar.input.TouchUserInput; import ve.ucv.ciens.ccg.nxtar.interfaces.ImageProcessor.MarkerData; import ve.ucv.ciens.ccg.nxtar.network.monitors.MotorEventQueue; import ve.ucv.ciens.ccg.nxtar.network.monitors.VideoFrameMonitor; import ve.ucv.ciens.ccg.nxtar.systems.AnimationSystem; +import ve.ucv.ciens.ccg.nxtar.systems.CollisionDetectionSystem; import ve.ucv.ciens.ccg.nxtar.systems.GeometrySystem; import ve.ucv.ciens.ccg.nxtar.systems.MarkerPositioningSystem; import ve.ucv.ciens.ccg.nxtar.systems.MarkerRenderingSystem; @@ -60,6 +61,7 @@ public class InGameState extends BaseState{ private static final String BACKGROUND_SHADER_PATH = "shaders/bckg/bckg"; private static final float NEAR = 0.01f; private static final float FAR = 100.0f; + private static final Vector3 TEMP_VEC_3 = new Vector3(); // Background related fields. private float uScaling[]; @@ -79,7 +81,7 @@ public class InGameState extends BaseState{ private RobotArmPositioningSystem robotArmPositioningSystem; // Cameras. - private OrthographicCamera unitaryOrthoCamera; + private OrthographicCamera unitaryOrthographicCamera; private OrthographicCamera pixelPerfectOrthoCamera; private CustomPerspectiveCamera perspectiveCamera; @@ -118,7 +120,7 @@ public class InGameState extends BaseState{ // Set up the cameras. pixelPerfectOrthoCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); - unitaryOrthoCamera = new OrthographicCamera(1.0f, Gdx.graphics.getHeight() / Gdx.graphics.getWidth()); + unitaryOrthographicCamera = new OrthographicCamera(1.0f, Gdx.graphics.getHeight() / Gdx.graphics.getWidth()); if(!Ouya.runningOnOuya) setUpButtons(); @@ -183,15 +185,15 @@ public class InGameState extends BaseState{ gameWorld = GameSettings.getGameWorld(); robotArmPositioningSystem = new RobotArmPositioningSystem(); + markerRenderingSystem = new MarkerRenderingSystem(modelBatch); + objectRenderingSystem = new ObjectRenderingSystem(modelBatch); + gameWorld.setSystem(new MarkerPositioningSystem()); - gameWorld.setSystem(robotArmPositioningSystem, true); + gameWorld.setSystem(robotArmPositioningSystem, Ouya.runningOnOuya); gameWorld.setSystem(new GeometrySystem()); gameWorld.setSystem(new AnimationSystem()); - // TODO: Make and add object-marker collision detection system. + gameWorld.setSystem(new CollisionDetectionSystem()); //gameWorld.setSystem(GameSettings.gameLogicSystem); - - markerRenderingSystem = new MarkerRenderingSystem(modelBatch); - objectRenderingSystem = new ObjectRenderingSystem(modelBatch); gameWorld.setSystem(markerRenderingSystem, true); gameWorld.setSystem(objectRenderingSystem, true); @@ -325,7 +327,7 @@ public class InGameState extends BaseState{ // Set the correct camera for the device. if(!Ouya.runningOnOuya){ - core.batch.setProjectionMatrix(unitaryOrthoCamera.combined); + core.batch.setProjectionMatrix(unitaryOrthographicCamera.combined); }else{ core.batch.setProjectionMatrix(pixelPerfectOrthoCamera.combined); } @@ -446,11 +448,11 @@ public class InGameState extends BaseState{ @Override public boolean touchDown(int screenX, int screenY, int pointer, int button){ MotorEvent event; - UserInput input; + TouchUserInput input; if(!Ouya.runningOnOuya){ win2world.set(screenX, screenY, 0.0f); - unitaryOrthoCamera.unproject(win2world); + unitaryOrthographicCamera.unproject(win2world); touchPointWorldCoords.set(win2world.x * Gdx.graphics.getWidth(), win2world.y * Gdx.graphics.getHeight()); Gdx.app.log(TAG, CLASS_NAME + String.format(".touchDown(%d, %d, %d, %d)", screenX, screenY, pointer, button)); @@ -536,12 +538,21 @@ public class InGameState extends BaseState{ } }else{ - input = UserInputFactory.createTouchUserInput(); + touchPointWorldCoords.set(win2world.x, win2world.y); + if(frameBufferSprite != null && frameBufferSprite.getBoundingRectangle().contains(touchPointWorldCoords)){ + Gdx.app.log(TAG, CLASS_NAME + "touchDown(): Touch point inside framebuffer."); - // TODO: Calculate movement ray. + TEMP_VEC_3.set(screenX, screenY, 1.0f); + perspectiveCamera.unproject(TEMP_VEC_3, frameBufferSprite.getX(), frameBufferSprite.getY(), frameBufferSprite.getWidth() * Gdx.graphics.getWidth(), frameBufferSprite.getHeight() * Gdx.graphics.getHeight()); + TEMP_VEC_3.rotate(Vector3.Z, 90).nor(); + TEMP_VEC_3.y = -TEMP_VEC_3.y; + //Gdx.app.log("TAG", CLASS_NAME + "touchDown(): Unprojected" + Utils.vector2String(TEMP_VEC_3)); - robotArmPositioningSystem.setUserInput(input); - robotArmPositioningSystem.process(); + input = new TouchUserInput(TEMP_VEC_3); + robotArmPositioningSystem.setUserInput(input); + }else{ + Gdx.app.log(TAG, CLASS_NAME + "touchDown(): Touch point outside framebuffer."); + } } return true; @@ -556,7 +567,7 @@ public class InGameState extends BaseState{ if(!Ouya.runningOnOuya){ win2world.set(screenX, screenY, 0.0f); - unitaryOrthoCamera.unproject(win2world); + unitaryOrthographicCamera.unproject(win2world); touchPointWorldCoords.set(win2world.x * Gdx.graphics.getWidth(), win2world.y * Gdx.graphics.getHeight()); Gdx.app.log(TAG, CLASS_NAME + String.format(".touchUp(%d, %d, %d, %d)", screenX, screenY, pointer, button)); @@ -665,7 +676,7 @@ public class InGameState extends BaseState{ if(!Ouya.runningOnOuya){ win2world.set(screenX, screenY, 0.0f); - unitaryOrthoCamera.unproject(win2world); + unitaryOrthographicCamera.unproject(win2world); touchPointWorldCoords.set(win2world.x * Gdx.graphics.getWidth(), win2world.y * Gdx.graphics.getHeight()); if(pointer == motorButtonsPointers[0] && !motorA.getBoundingRectangle().contains(touchPointWorldCoords)){ @@ -941,7 +952,26 @@ public class InGameState extends BaseState{ @Override public boolean axisMoved(Controller controller, int axisCode, float value){ - // TODO: Pass input to the input handler system. + GamepadUserInput userInput; + + if(Math.abs(value) > Ouya.STICK_DEADZONE){ + userInput = new GamepadUserInput(); + if(axisCode == Ouya.AXIS_LEFT_X){ + userInput.axisLeftX = value; + }else if(axisCode == Ouya.AXIS_LEFT_Y){ + userInput.axisLeftY = value; + }else if(axisCode == Ouya.AXIS_RIGHT_X){ + userInput.axisRightX = value; + }else if(axisCode == Ouya.AXIS_RIGHT_Y){ + userInput.axisRightY = value; + } + + robotArmPositioningSystem.setUserInput(userInput); + robotArmPositioningSystem.process(); + + return true; + } + return false; } } diff --git a/src/ve/ucv/ciens/ccg/nxtar/states/MainMenuStateBase.java b/src/ve/ucv/ciens/ccg/nxtar/states/MainMenuStateBase.java index 56c1614..09098c4 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/states/MainMenuStateBase.java +++ b/src/ve/ucv/ciens/ccg/nxtar/states/MainMenuStateBase.java @@ -312,7 +312,7 @@ public abstract class MainMenuStateBase extends BaseState{ @Override public boolean keyDown(int keycode){ if(keycode == Input.Keys.BACK){ - // Ignore. + Gdx.app.exit(); return true; } return false; diff --git a/src/ve/ucv/ciens/ccg/nxtar/systems/CollisionDetectionSystem.java b/src/ve/ucv/ciens/ccg/nxtar/systems/CollisionDetectionSystem.java index ef9e21d..7b01e3d 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/systems/CollisionDetectionSystem.java +++ b/src/ve/ucv/ciens/ccg/nxtar/systems/CollisionDetectionSystem.java @@ -15,23 +15,68 @@ */ package ve.ucv.ciens.ccg.nxtar.systems; +import ve.ucv.ciens.ccg.nxtar.components.CollisionDetectionComponent; import ve.ucv.ciens.ccg.nxtar.components.CollisionModelComponent; +import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent; +import ve.ucv.ciens.ccg.nxtar.components.VisibilityComponent; import com.artemis.Aspect; +import com.artemis.ComponentMapper; import com.artemis.Entity; +import com.artemis.annotations.Mapper; +import com.artemis.managers.GroupManager; import com.artemis.systems.EntityProcessingSystem; +import com.artemis.utils.ImmutableBag; +import com.badlogic.gdx.math.collision.BoundingBox; public class CollisionDetectionSystem extends EntityProcessingSystem { + public static final String COLLIDABLE_OBJECT = "COLLIDABLE"; + + @Mapper ComponentMapper collisionModelMapper; + @Mapper ComponentMapper collisionDetectionMapper; + @Mapper ComponentMapper visibilityMapper; + + private GroupManager groupManager; @SuppressWarnings("unchecked") public CollisionDetectionSystem(){ - super(Aspect.getAspectForAll(CollisionModelComponent.class)); + super(Aspect.getAspectForAll(CollisionModelComponent.class, CollisionDetectionComponent.class).exclude(MarkerCodeComponent.class)); } @Override protected void process(Entity e) { - // TODO Auto-generated method stub + + VisibilityComponent visibility; + CollisionModelComponent collision; + CollisionModelComponent target; + CollisionDetectionComponent onCollision; + BoundingBox colBB = new BoundingBox(); + BoundingBox targetBB = new BoundingBox(); + ImmutableBag collidables; + + groupManager = this.world.getManager(GroupManager.class); + collidables = groupManager.getEntities(COLLIDABLE_OBJECT); + + collision = collisionModelMapper.get(e); + onCollision = collisionDetectionMapper.get(e); + + for(int i = 0; i < collidables.size(); ++i){ + target = collisionModelMapper.getSafe(collidables.get(i)); + visibility = visibilityMapper.getSafe(collidables.get(i)); + + if(target == null || visibility == null) continue; + + if(visibility.visible){ + collision.instance.calculateBoundingBox(colBB); + target.instance.calculateBoundingBox(targetBB); + + if(colBB.contains(targetBB)){ + onCollision.colliding = true; + }else{ + onCollision.colliding = false; + } + } + } } - } diff --git a/src/ve/ucv/ciens/ccg/nxtar/systems/RobotArmPositioningSystem.java b/src/ve/ucv/ciens/ccg/nxtar/systems/RobotArmPositioningSystem.java index 0278434..d08fd34 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/systems/RobotArmPositioningSystem.java +++ b/src/ve/ucv/ciens/ccg/nxtar/systems/RobotArmPositioningSystem.java @@ -15,26 +15,38 @@ */ package ve.ucv.ciens.ccg.nxtar.systems; +import ve.ucv.ciens.ccg.nxtar.components.AutomaticMovementComponent; +import ve.ucv.ciens.ccg.nxtar.components.CollisionDetectionComponent; import ve.ucv.ciens.ccg.nxtar.components.GeometryComponent; -import ve.ucv.ciens.ccg.nxtar.factories.products.GamepadUserInput; -import ve.ucv.ciens.ccg.nxtar.factories.products.KeyboardUserInput; -import ve.ucv.ciens.ccg.nxtar.factories.products.TouchUserInput; -import ve.ucv.ciens.ccg.nxtar.factories.products.UserInput; +import ve.ucv.ciens.ccg.nxtar.components.MarkerCodeComponent; +import ve.ucv.ciens.ccg.nxtar.input.GamepadUserInput; +import ve.ucv.ciens.ccg.nxtar.input.KeyboardUserInput; +import ve.ucv.ciens.ccg.nxtar.input.TouchUserInput; +import ve.ucv.ciens.ccg.nxtar.input.UserInput; +import ve.ucv.ciens.ccg.nxtar.utils.Utils; import com.artemis.Aspect; import com.artemis.ComponentMapper; import com.artemis.Entity; import com.artemis.annotations.Mapper; import com.artemis.systems.EntityProcessingSystem; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.math.Vector3; public class RobotArmPositioningSystem extends EntityProcessingSystem { - @Mapper ComponentMapper geometryMapper; + private static final String TAG = "ROBOT_ARM_POSITIONING_SYSTEM"; + private static final String CLASS_NAME = RobotArmPositioningSystem.class.getSimpleName(); + private static final float STEP_SIZE = 0.05f; + + @Mapper ComponentMapper geometryMapper; + @Mapper ComponentMapper autoMapper; + @Mapper ComponentMapper collisionMapper; private UserInput input; @SuppressWarnings("unchecked") public RobotArmPositioningSystem(){ - super(Aspect.getAspectForAll(GeometryComponent.class)); + super(Aspect.getAspectForAll(GeometryComponent.class, AutomaticMovementComponent.class, CollisionDetectionComponent.class).exclude(MarkerCodeComponent.class)); } public void setUserInput(UserInput input){ @@ -42,20 +54,94 @@ public class RobotArmPositioningSystem extends EntityProcessingSystem { } @Override - protected void process(Entity e) { - GeometryComponent geometry = geometryMapper.get(e); + protected void process(Entity e) throws ClassCastException{ + Vector3 endPoint; + GamepadUserInput tempGP; + KeyboardUserInput tempKey; + GeometryComponent geometry = geometryMapper.get(e); + AutomaticMovementComponent auto = autoMapper.get(e); + CollisionDetectionComponent collision = collisionMapper.get(e); - if(input == null) return; + if(input == null){ + if(auto.moving) autoMove(geometry, auto, collision); + else return; - if(input instanceof TouchUserInput){ + }else{ + if(input instanceof TouchUserInput){ + if(!auto.moving){ + endPoint = ((TouchUserInput) input).userTouchEndPoint; + endPoint.set(endPoint.x, endPoint.y, -4.5f); + auto.startPoint.set(geometry.position); + auto.endPoint.set(endPoint); + auto.moving = true; + auto.forward = true; - }else if(input instanceof GamepadUserInput){ + Gdx.app.log(TAG, CLASS_NAME + ".process(): Started moving from " + Utils.vector2String(auto.startPoint) + " to " + Utils.vector2String(auto.endPoint)); + }else autoMove(geometry, auto, collision); - }else if(input instanceof KeyboardUserInput){ + }else if(input instanceof GamepadUserInput){ + tempGP = (GamepadUserInput) input; + geometry.position.x += !collision.colliding ? tempGP.axisLeftY * STEP_SIZE : 0.0f; + geometry.position.y += !collision.colliding ? tempGP.axisLeftX * STEP_SIZE : 0.0f; + geometry.position.z += !collision.colliding ? tempGP.axisRightY * STEP_SIZE : 0.0f; + clampPosition(geometry); - }else - throw new ClassCastException("Input is not a valid UserInput instance."); + }else if(input instanceof KeyboardUserInput){ + tempKey = (KeyboardUserInput) input; + geometry.position.x -= tempKey.keyUp && !collision.colliding ? STEP_SIZE : 0.0f; + geometry.position.x += tempKey.keyDown && !collision.colliding ? STEP_SIZE : 0.0f; + geometry.position.y -= tempKey.keyLeft && !collision.colliding ? STEP_SIZE : 0.0f; + geometry.position.y += tempKey.keyRight && !collision.colliding ? STEP_SIZE : 0.0f; + geometry.position.z -= tempKey.keyZ && !collision.colliding ? STEP_SIZE : 0.0f; + geometry.position.z += tempKey.keyA && !collision.colliding ? STEP_SIZE : 0.0f; + clampPosition(geometry); + + }else + throw new ClassCastException("Input is not a valid UserInput instance."); + } input = null; } + + private void autoMove(GeometryComponent geometry, AutomaticMovementComponent auto, CollisionDetectionComponent collision){ + float step; + + if(auto.moving){ + if(auto.forward) + step = STEP_SIZE; + else + step = -STEP_SIZE; + + Gdx.app.log(TAG, CLASS_NAME + ".autoMove(): Step = " + Float.toString(step)); + + auto.distance += step; + + Gdx.app.log(TAG, CLASS_NAME + ".autoMove(): Step = " + Float.toString(auto.distance)); + + geometry.position.x = (auto.startPoint.x * (1.0f - auto.distance)) + (auto.endPoint.x * auto.distance); + geometry.position.y = (auto.startPoint.y * (1.0f - auto.distance)) + (auto.endPoint.y * auto.distance); + geometry.position.z = (auto.startPoint.z * (1.0f - auto.distance)) + (auto.endPoint.z * auto.distance); + + Gdx.app.log(TAG, CLASS_NAME + ".autoMove(): Current position: " + Utils.vector2String(geometry.position)); + + if(auto.distance >= 1.0f || collision.colliding){ + auto.forward = false; + Gdx.app.log(TAG, CLASS_NAME + ".autoMove(): Going backwards now."); + }else if(auto.distance <= 0.0f){ + auto.forward = true; + auto.moving = false; + Gdx.app.log(TAG, CLASS_NAME + ".autoMove(): Going forward now."); + } + + }else return; + } + + private void clampPosition(GeometryComponent geometry){ + geometry.position.x = geometry.position.x >= -1.0f ? geometry.position.x : -1.0f; + geometry.position.x = geometry.position.x <= 1.0f ? geometry.position.x : 1.0f; + geometry.position.y = geometry.position.y >= -1.0f ? geometry.position.y : -1.0f; + geometry.position.y = geometry.position.y <= 1.0f ? geometry.position.y : 1.0f; + geometry.position.z = geometry.position.z >= 0.0f ? geometry.position.z : 0.0f; + geometry.position.z = geometry.position.z <= 6.0f ? geometry.position.z : 6.0f; + } } diff --git a/src/ve/ucv/ciens/ccg/nxtar/utils/GameSettings.java b/src/ve/ucv/ciens/ccg/nxtar/utils/GameSettings.java index 77dba40..bde53b3 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/utils/GameSettings.java +++ b/src/ve/ucv/ciens/ccg/nxtar/utils/GameSettings.java @@ -16,6 +16,7 @@ package ve.ucv.ciens.ccg.nxtar.utils; import com.artemis.World; +import com.artemis.managers.GroupManager; import ve.ucv.ciens.ccg.nxtar.NxtARCore; import ve.ucv.ciens.ccg.nxtar.entities.BombGameEntityCreator; @@ -31,8 +32,10 @@ public abstract class GameSettings{ if(core == null) throw new IllegalArgumentException("Core is null."); - if(getGameWorld() == null) + if(getGameWorld() == null){ gameWorld = new World(); + gameWorld.setManager(new GroupManager()); + } if(getEntityCreator() == null){ entityCreator = new BombGameEntityCreator(); diff --git a/src/ve/ucv/ciens/ccg/nxtar/utils/Utils.java b/src/ve/ucv/ciens/ccg/nxtar/utils/Utils.java new file mode 100644 index 0000000..15a7ed7 --- /dev/null +++ b/src/ve/ucv/ciens/ccg/nxtar/utils/Utils.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2014 Miguel Angel Astor Romero + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ve.ucv.ciens.ccg.nxtar.utils; + +import com.badlogic.gdx.math.Vector3; + +public abstract class Utils{ + public static String vector2String(Vector3 v){ + return "(" + Float.toString(v.x) + ", " + Float.toString(v.y) + ", " + Float.toString(v.z) + ")"; + } +}