List of usage examples for com.badlogic.gdx.math Vector2 set
public Vector2 set(float x, float y)
From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java
License:Apache License
private static boolean GetHullEntrance(PolygonCreationAssistance pca, Vector2 entrance) { // Search for first solid pixel. for (int y = 0; y < pca.Height; y++) { for (int x = 0; x < pca.Width; x++) { if (pca.IsSolid(x, y)) { entrance.set(x, y); return true; }//from w w w .ja v a 2s. c o m } } // If there are no solid pixels. entrance.set(0, 0); return false; }
From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java
License:Apache License
private static boolean GetNextHullEntrance(PolygonCreationAssistance pca, Vector2 start, Vector2 entrance) { // Search for first solid pixel. int size = pca.Height * pca.Width; int x;/*w ww . ja v a 2s. c o m*/ boolean foundTransparent = false; for (int i = (int) start.x + (int) start.y * pca.Width; i < size; i++) { if (pca.IsSolid(i)) { if (foundTransparent) { x = i % pca.Width; entrance.set(x, (i - x) / pca.Width); return true; } } else { foundTransparent = true; } } // If there are no solid pixels. entrance.set(0, 0); return false; }
From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java
License:Apache License
private static boolean GetNextHullPoint(PolygonCreationAssistance pca, Vector2 last, Vector2 current, Vector2 next) { int x;/* w w w. j a v a 2 s .c o m*/ int y; int indexOfFirstPixelToCheck = GetIndexOfFirstPixelToCheck(last, current); int indexOfPixelToCheck; int pixelsToCheck = 8; // _closePixels.Length; for (int i = 0; i < pixelsToCheck; i++) { indexOfPixelToCheck = (indexOfFirstPixelToCheck + i) % pixelsToCheck; x = (int) current.x + ClosePixels[indexOfPixelToCheck][0]; y = (int) current.y + ClosePixels[indexOfPixelToCheck][1]; if (x >= 0 && x < pca.Width && y >= 0 && y <= pca.Height) { if (pca.IsSolid(x, y)) // todo { next.set(x, y); return true; } } } next.set(0, 0); return false; }
From source file:com.kotcrab.vis.editor.module.scene.entitymanipulator.tool.PolygonTool.java
License:Apache License
private void showAutoTracerDialog(EntityProxy entity, VisAssetDescriptor assetDescriptor) { stage.addActor(new PolygonAutoTraceDialog(sceneMC, assetDescriptor, vertices -> { ChangePolygonAction action = new ChangePolygonAction(entityManipulator, proxy); //convert to world cords Rectangle bounds = entity.getBoundingRectangle(); for (Vector2 vertex : vertices) { vertex.set(bounds.x + bounds.getWidth() * vertex.x, bounds.y + bounds.getHeight() * vertex.y); }/*from w w w . j av a 2s . c om*/ component.vertices.clear(); component.vertices.addAll(vertices); selectedVertex = null; updateComponentFaces(); action.takeSnapshot(); undoModule.add(action); entityManipulator.selectedEntitiesValuesChanged(); }).fadeIn()); }
From source file:com.ore.infinium.systems.MovementSystem.java
License:Open Source License
private void simulate(Entity entity, float delta) { //fixme maybe make a dropped component? if (controlMapper.get(entity) == null) { if (m_world.isServer()) { ItemComponent itemComponent = itemMapper.get(entity); if (itemComponent != null && itemComponent.state == ItemComponent.State.DroppedInWorld) { simulateDroppedItem(entity, delta); }//w w w .jav a 2 s . c om } return; } if (m_world.isServer()) { return; } //fixme handle noclip final SpriteComponent spriteComponent = spriteMapper.get(entity); final Vector2 origPosition = new Vector2(spriteComponent.sprite.getX(), spriteComponent.sprite.getY()); final VelocityComponent velocityComponent = velocityMapper.get(entity); final Vector2 oldVelocity = new Vector2(velocityComponent.velocity); Vector2 newVelocity = new Vector2(oldVelocity); final Vector2 desiredDirection = controlMapper.get(entity).desiredDirection; //acceleration due to gravity Vector2 acceleration = new Vector2(desiredDirection.x * PlayerComponent.movementSpeed, World.GRAVITY_ACCEL); JumpComponent jumpComponent = jumpMapper.get(entity); if (jumpComponent.canJump && jumpComponent.shouldJump) { if (jumpComponent.jumpTimer.milliseconds() >= jumpComponent.jumpInterval) { //good to jump, actually do it now. jumpComponent.jumpTimer.reset(); acceleration.y = -PlayerComponent.jumpVelocity; } } jumpComponent.canJump = false; jumpComponent.shouldJump = false; newVelocity = newVelocity.add(acceleration.x * delta, acceleration.y * delta); final float epsilon = 0.00001f; if (Math.abs(newVelocity.x) < epsilon && Math.abs(newVelocity.y) < epsilon) { //gets small enough velocity, cease movement/sleep object. newVelocity.set(0.0f, 0.0f); } newVelocity.x *= 0.8f; // newVelocity = glm::clamp(newVelocity, glm::vec2(-maxMovementSpeed, PLAYER_JUMP_VELOCITY), glm::vec2(maxMovementSpeed, 9999999999999));//(9.8f / PIXELS_PER_METER) * 4.0)); // newVelocity = glm::clamp(newVelocity, glm::vec2(-maxMovementSpeed, PLAYER_JUMP_VELOCITY), glm::vec2(maxMovementSpeed, (9.8f / PIXELS_PER_METER) * 4.0)); //clamp both axes between some max/min values.. Vector2 dt = new Vector2(delta, delta); // newVelocity.x = MathUtils.clamp(newVelocity.x, -PlayerComponent.maxMovementSpeed, PlayerComponent.maxMovementSpeed); // newVelocity.y = MathUtils.clamp(newVelocity.y, PlayerComponent.jumpVelocity, World.GRAVITY_ACCEL_CLAMP); ///////// velocity verlet integration // http://lolengine.net/blog/2011/12/14/understanding-motion-in-games //HACK if i do 0.5f * delta, it doesn't move at all?? // * delta //OLD CODE desiredPosition = ((origPosition.xy() + (oldVelocity + newVelocity)) * glm::vec2(0.5f) * dt); //TODO: add threshold to nullify velocity..so we don't infinitely move and thus burn through ticks/packets velocityComponent.velocity.set(newVelocity.x, newVelocity.y); // newVelocity is now invalid, note. Vector2 desiredPosition = origPosition.add(oldVelocity.add(newVelocity.scl(0.5f * delta))); final Vector2 finalPosition = performCollision(desiredPosition, entity); spriteComponent.sprite.setPosition(finalPosition.x, finalPosition.y); // Gdx.app.log("player pos", finalPosition.toString()); //FIXME: do half-ass friction, to feel better than this. and then when movement is close to 0, 0 it. }
From source file:com.ore.infinium.World.java
License:Open Source License
private void alignPositionToBlocks(Vector2 pos) { pos.set(BLOCK_SIZE * MathUtils.floor(pos.x / BLOCK_SIZE), BLOCK_SIZE * MathUtils.floor(pos.y / BLOCK_SIZE)); }
From source file:com.punchables.rainbowdad.utils.Collider.java
/** * Take two DynamicGameObjects//w w w . ja va 2 s. c o m * @param object * @param tile * @param tileSize * @return * true if a collision occurred, false if a collision didn't occur */ public static boolean handleTileCollision(DynamicGameObject object, ArrayList<MapTile> collidingTiles, int tileSize) { Vector2 forceVector = new Vector2(); int offset = 0; int errorMargin = 0; float maxCollisionVector = 0; boolean isMaxCollisionVectorX = false; MapTile collidingTile = new MapTile(); int dir_x = 0; int dir_y = 0; for (MapTile tile : collidingTiles) { float[] collisionValues = checkCollision(object.getHitbox(), tile, tileSize, true); //System.out.println(collisionValues[0]); if (collisionValues[0] > maxCollisionVector) { collidingTile = tile; maxCollisionVector = collisionValues[0]; isMaxCollisionVectorX = true; dir_x = (int) (collisionValues[0] / abs(collisionValues[0])); } if (collisionValues[1] > maxCollisionVector) { collidingTile = tile; maxCollisionVector = collisionValues[1]; isMaxCollisionVectorX = false; dir_y = (int) (collisionValues[1] / abs(collisionValues[1])); } //if collision has occured // if(collisionValues[2] == 1 // && (abs(collisionValues[0]) > errorMargin || abs(collisionValues[1]) > errorMargin)){ // // int dir_x = (int) (collisionValues[0] / abs(collisionValues[0])); // int dir_y = (int) (collisionValues[1] / abs(collisionValues[1])); // // //check for the greater offset // if(collisionValues[0] >= collisionValues[1]){ // forceVector.set(-collisionValues[0] - offset * dir_x, 0); // //forceVector.x = -collisionValues[0] + offset * dir_x; // // object.getVel().x = 0; // System.out.println("X COLLISION! " + System.currentTimeMillis()); // //object.getAccel().x = 0; // // } else if(collisionValues[0] < collisionValues[1]){ // forceVector.set(0, -collisionValues[1] - offset * dir_y); // //forceVector.y = -collisionValues[1] + offset * dir_y; // // object.getVel().y = 0; // System.out.println("Y COLLISION! " + System.currentTimeMillis()); // //object.getAccel().y = 0; // } // //System.out.println("x: " + collisionValues[0] + " y: " + collisionValues[1]); // //System.out.println(forceVector); // // } } if (isMaxCollisionVectorX && maxCollisionVector > 0) { forceVector.set(-maxCollisionVector - offset * dir_x, 0); //forceVector.x = -collisionValues[0] + offset * dir_x; //object.getVel().x = 0; System.out.println("X COLLISION! " + System.currentTimeMillis()); //object.getAccel().x = 0; } else if (!isMaxCollisionVectorX && maxCollisionVector > 0) { forceVector.set(0, -maxCollisionVector - offset * dir_y); //forceVector.y = -collisionValues[1] + offset * dir_y; //object.getVel().y = 0; System.out.println("Y COLLISION! " + System.currentTimeMillis()); //object.getAccel().y = 0; } else { return false; } //collidingTile.set(TileType.DEBUG); object.getPos().add(forceVector); return true; //return true; }
From source file:com.ridiculousRPG.ui.ActorFocusUtil.java
License:Apache License
private static boolean tryFocusPrevChild(Actor focused, boolean up, boolean left, Stage stage, Array<Actor> allActors) { Vector2 tmpPoint = ActorFocusUtil.tmpPoint; focused.stageToLocalCoordinates(tmpPoint.set(focused.getX(), focused.getY())); float fX1 = focused.getX() - tmpPoint.x; float fY1 = focused.getY() - tmpPoint.y; float fX2 = fX1 + focused.getWidth() * .5f; float fY2 = fY1 + focused.getHeight() * .5f; for (int i = allActors.size - 1; i > -1; i--) { Actor a = allActors.get(i);/*from ww w . j av a 2 s .co m*/ if (a == focused) continue; a.stageToLocalCoordinates(tmpPoint.set(a.getX(), a.getY())); float aX1 = a.getX() - tmpPoint.x; float aY1 = a.getY() - tmpPoint.y; float aX2 = aX1 + a.getWidth() * .5f; float aY2 = aY1 + a.getHeight() * .5f; if (isFocusable(a)) { if (up) { if (aY1 >= fY2 && aX2 > fX1 && aX1 < fX2) return focus(a, true, stage); } else if (left) { if (aX2 <= fX1 && aY1 < fY2 && aY2 > fY1) return focus(a, true, stage); } else if (aY1 >= fY2 || (aX2 <= fX1 && aY1 < fY2 && aY2 > fY1)) return focus(a, true, stage); } else if (a instanceof Group) { if (tryFocusPrevChild(focused, up, left, stage, ((Group) a).getChildren())) { return true; } } } return false; }
From source file:com.ridiculousRPG.ui.ActorFocusUtil.java
License:Apache License
private static boolean tryFocusNextChild(Actor focused, boolean down, boolean right, Stage stage, Array<Actor> allActors) { Vector2 tmpPoint = ActorFocusUtil.tmpPoint; focused.stageToLocalCoordinates(tmpPoint.set(focused.getX(), focused.getY())); float fX1 = focused.getX() - tmpPoint.x; float fY1 = focused.getY() - tmpPoint.y; float fX2 = fX1 + focused.getWidth() * .5f; float fY2 = fY1 + focused.getHeight() * .5f; for (int i = 0, len = allActors.size; i < len; i++) { Actor a = allActors.get(i);// www . ja v a2 s. c o m if (a == focused) continue; a.stageToLocalCoordinates(tmpPoint.set(a.getX(), a.getY())); float aX1 = a.getX() - tmpPoint.x; float aY1 = a.getY() - tmpPoint.y; float aX2 = aX1 + a.getWidth() * .5f; float aY2 = aY1 + a.getHeight() * .5f; if (isFocusable(a)) { if (down) { if (aY2 <= fY1 && aX2 > fX1 && aX1 < fX2) return focus(a, false, stage); } else if (right) { if (aX1 >= fX2 && aY1 < fY2 && aY2 > fY1) return focus(a, false, stage); } else if (aY2 <= fY1 || (aX1 >= fX2 && aY1 < fY2 && aY2 > fY1)) return focus(a, false, stage); } else if (a instanceof Group) { if (tryFocusNextChild(focused, down, right, stage, ((Group) a).getChildren())) { return true; } } } return false; }
From source file:com.ridiculousRPG.ui.ActorsOnStageService.java
License:Apache License
private void computeTouchPoint(Actor a, Vector2 point) { point.set(a.getWidth() * .5f, a.getHeight() * .5f); a.localToStageCoordinates(point);// www . ja v a 2 s.co m point.set(point.x * Gdx.graphics.getWidth() / getWidth(), (getHeight() - point.y) * Gdx.graphics.getHeight() / getHeight()); }