@Override public boolean mouseMoved(InputEvent event, float x, float y) { Vector2 scrolling = new Vector2(); float zone = 10; float halfWidth = getStage().getViewport().getWorldWidth() * 0.5f; float halfHeight = getStage().getViewport().getWorldHeight() * 0.5f; Vector3 cameraPosition = getStage().getCamera().position; float west = cameraPosition.x - halfWidth + zone; float east = cameraPosition.x + halfWidth - zone; float north = cameraPosition.y + halfHeight - zone; float south = cameraPosition.y - halfHeight + zone; if (x <= west) { scrolling.x = -1; } if (x >= east) { scrolling.x = 1; } if (y >= north) { scrolling.y = 1; } if (y <= south) { scrolling.y = -1; } Service.eventQueue().enqueue(new Event(EventName.MOVING_CAMERA, scrolling)); return super.mouseMoved(event, x, y); }
private void tryMove() { bounds.x += vel.x; fetchCollidableRects(); for (int i = 0; i < r.length; i++) { Rectangle rect = r[i]; if (bounds.overlaps(rect)) { if (vel.x < 0) bounds.x = rect.x + rect.width + 0.01f; else bounds.x = rect.x - bounds.width - 0.01f; vel.x = 0; } } bounds.y += vel.y; fetchCollidableRects(); for (int i = 0; i < r.length; i++) { Rectangle rect = r[i]; if (bounds.overlaps(rect)) { if (vel.y < 0) { bounds.y = rect.y + rect.height + 0.01f; } else bounds.y = rect.y - bounds.height - 0.01f; vel.y = 0; } } pos.x = bounds.x - 0.2f; pos.y = bounds.y - 0.2f; }
public void update(float delta, Array<Block> collisions) { // NOTE: figure out why legs go into block, could be order of checking for collisions/updating // position // position += velocity above collisions before changing the velocity acceleration.y = gravity; if (velocity.y < 0) onGround = false; if (delta > .1f) { // make sure time passed isn't great enough to cause clipping issues when window // moved delta = .02f; } acceleration.scl(delta); velocity.add(acceleration); processCollisions(collisions); if (Math.abs(velocity.x) < .05) velocity.x = 0; else { velocity.x *= friction; } if (velocity.x > maxVel) velocity.x = maxVel; if (velocity.x < -maxVel) velocity.x = -maxVel; position.add(velocity.cpy().scl(delta)); bounds.x = position.x; bounds.y = position.y; stateTime += delta; setPosition(getPosition()); }
public void movePlayer(Touchpad touchpad) { float touchX = touchpad.getKnobPercentX(); float touchY = touchpad.getKnobPercentY(); if (touchX < -0.1 && touchX < -Math.abs(touchY)) { spriteVector.y = 0; spriteVector.x = -SPRITE_SPEED; spriteDirection = SpriteDirection.e; } else if (touchX > 0.1 && touchX > Math.abs(touchY)) { spriteVector.y = 0; spriteVector.x = SPRITE_SPEED; spriteDirection = SpriteDirection.w; } else if (touchY < -0.1 && touchY <= -Math.abs(touchX)) { spriteVector.x = 0; spriteVector.y = -SPRITE_SPEED; spriteDirection = SpriteDirection.s; } else if (touchY > 0.1 && touchY >= Math.abs(touchX)) { spriteVector.x = 0; spriteVector.y = SPRITE_SPEED; spriteDirection = SpriteDirection.n; } else if (touchX <= 0.1 && touchX >= -0.1 && touchY <= 0.1 && touchY >= -0.1) { spriteVector.x = 0; spriteVector.y = 0; } if (spriteVector.x == 0 && spriteVector.y == 0) spriteState = State.Idle; else spriteState = State.Walking; }
public void resize() { float w = Gdx.graphics.getWidth(); float h = Gdx.graphics.getHeight(); invSize.x = 1.0f / w; invSize.y = 1.0f / h; magicSize.x = w / 16; magicSize.y = h / 16; }
public void onRestart(int y) { rotation = 0; position.y = y; velocity.x = 0; velocity.y = 0; acceleration.x = 0; acceleration.y = 460; isAlive = true; }
private void recoilFromEnemy(Direction direction) { jumpState = JumpState.RECOILING; velocity.y = Constants.KNOCKBACK_VELOCITY.y; if (direction == Direction.LEFT) { velocity.x = -Constants.KNOCKBACK_VELOCITY.x; } else { velocity.x = Constants.KNOCKBACK_VELOCITY.x; } }
@Override public void interpolate(float f) { interpolatedPositionA.x = lastPhysicsPosA.x * f; interpolatedPositionA.x += joint.getAnchorA().x * (1 - f); interpolatedPositionA.y = lastPhysicsPosA.y * f; interpolatedPositionA.y += joint.getAnchorA().y * (1 - f); interpolatedPositionB.x = lastPhysicsPosB.x * f; interpolatedPositionB.x += joint.getAnchorB().x * (1 - f); interpolatedPositionB.y = lastPhysicsPosB.y * f; interpolatedPositionB.y += joint.getAnchorB().y * (1 - f); }
protected static Collision AABBvsCircle(AABB A, Circle B) { Vector2 AtoB = B.getPosition().cpy().sub(A.getCenter()); float xe = A.getDimensions().x / 2; float ye = A.getDimensions().y / 2; Vector2 closest = new Vector2(MathUtils.clamp(AtoB.x, -xe, xe), MathUtils.clamp(AtoB.y, -ye, ye)); boolean inside = false; if (AtoB.epsilonEquals(closest, (float) 1e-4)) { inside = true; if (Math.abs(AtoB.x) > Math.abs(AtoB.y)) { if (closest.x > 0.0f) { closest.x = xe; } else { closest.x = -xe; } } else { if (closest.y > 0.0f) { closest.y = ye; } else { closest.y = -ye; } } } Vector2 normal = new Vector2(AtoB).sub(closest); float d = normal.len2(); float r = B.radius; // No collision if (d > r * r && !inside) { return null; } d = (float) Math.sqrt(d); float penetration = r + d; if (inside) { normal.set(AtoB).mul(1.0f).nor(); } else { normal.set(AtoB).mul(-1.0f).nor(); } return new Collision(normal, penetration); }
private void action(int action) { mCubicStartAniSprite.setVisible(false); mCubicStartAniSprite.stopAnimation(0); switch (action) { case ACTION_TURN_LEVEL_1: case ACTION_TURN_LEVEL_2: case ACTION_TURN_LEVEL_3: ACTION = action; final Body body = (Body) this.getUserData(); final Vector2 vector2 = Vector2Pool.obtain(body.getPosition()); int frame[] = new int[] {0, 1}; body.setAngularDamping(1.2f); vector2.x = vector2.x / 3; vector2.y = vector2.y / 3; if (action == ACTION_TURN_LEVEL_2) { frame = new int[] {2, 3}; body.setAngularDamping(2.2f); vector2.x = vector2.x / 2; vector2.y = vector2.y / 2; } else if (action == ACTION_TURN_LEVEL_3) { frame = new int[] {4, 5}; body.setAngularDamping(3.2f); } Vol3Osyougatsu.OGG_A3_A_5_KOMATEI.play(); mCubicTurnAniSprite.animate(new long[] {250, 250}, frame, -1); mCubicTurnAniSprite.setVisible(true); body.setActive(true); Log.i(TAG, "body.getPosition " + vector2); vector2.x = random(vector2.x) + new Random().nextFloat(); vector2.y = random(vector2.y) + new Random().nextFloat(); body.setLinearVelocity(vector2); Log.i(TAG, "body.getPosition change " + vector2); this.unregisterUpdateHandler(timerHandler); this.registerUpdateHandler( timerHandler = new TimerHandler( 6f, new ITimerCallback() { @Override public void onTimePassed(TimerHandler timerHandler) { // Stop Turn stopTurn(); } })); break; } }
private void setTouchPoint(int screenX, int screenY) { touchPos.set(screenX, screenY, 0); context.cam.unproject(touchPos); Vector2 pos = context.addDump.pos; pos.x = touchPos.x - (CoreData.CUBE_WIDTH >> 1); if (pos.x < 0) { pos.x = 0; } else if (pos.x > GameScreen.SC_WIDTH - CoreData.CUBE_WIDTH) { pos.x = GameScreen.SC_WIDTH - CoreData.CUBE_WIDTH; } context.addDump.curSprite.setPosition(pos.x, AddDump.ADD_POS_Y); }
@Override protected void processEntity(Entity entity, float deltaTime) { PlatformMonsterComp pm = entity.getComponent(PlatformMonsterComp.class); MonsterMovementComp mm = entity.getComponent(MonsterMovementComp.class); BodyComp b = entity.getComponent(BodyComp.class); Vector2 pos = b.body.getPosition(); pos.x *= b.invWorldScale; pos.y *= b.invWorldScale; switch (mm.moveType) { case LEFT: if (pos.x <= pm.minX) { standMonster(pm, mm); } break; case STAND: if (pm.standCountdown > 0) { pm.standCountdown -= deltaTime; } if (pm.standCountdown <= 0) { pm.standCountdown = 0; if (pos.x <= pm.minX) { mm.moveType = MonsterMovementComp.MoveType.RIGHT; } else { mm.moveType = MonsterMovementComp.MoveType.LEFT; } } break; case RIGHT: if (pos.x >= pm.maxX) { standMonster(pm, mm); } break; } }
public static boolean inLineOfSight(Vector2 p1, Vector2 p2, Polygon polygon, boolean obstacle) { tmp.set(p1); tmp2.set(p2); float verts[] = polygon.getTransformedVertices(); for (int i = 0; i < verts.length; i += 2) { if (lineSegmentsCross( tmp.x, tmp.y, tmp2.x, tmp2.y, verts[i], verts[i + 1], verts[(i + 2) % verts.length], verts[(i + 3) % verts.length])) return false; } tmp.add(tmp2); tmp.x /= 2; tmp.y /= 2; boolean result = PolygonUtils.isPointInside(polygon, tmp.x, tmp.y, !obstacle); return obstacle ? !result : result; }
/** * Transforms the specified point in the actor's coordinates to be in the parent's coordinates. * Note this method will ONLY work for screen aligned, unrotated, unscaled actors! */ public Vector2 localToParentCoordinates(Vector2 localCoords) { if (getRotation() != 0 || getScaleX() != 1 || getScaleY() != 1) throw new GdxRuntimeException("Only unrotated and unscaled actors may use this method."); localCoords.x += getX(); localCoords.y += getY(); return localCoords; }
public void move(float delta) { setPosition(getX() + (impulse.x * delta), getY() + (impulse.y * delta)); if (getX() < leftBorder) { impulse.x = -impulse.x; setX(leftBorder); } else if (getX() > rightBorder) { impulse.x = -impulse.x; setX(rightBorder); } else if (getY() < bottomBorder) { impulse.y = -impulse.y; setY(bottomBorder); } else if (getY() > topBorder) { impulse.y = -impulse.y; setY(topBorder); } }
@Override public Vector2 accelerate(Vector2 acceleration, Animal animal) { // calculate the horizontal and vertical acceleration via Accelerometer acceleration.x = Gdx.input.getAccelerometerY(); acceleration.y = -Gdx.input.getAccelerometerX(); return acceleration; }
public void actBehavior(float deltaTime) { Random rand = new Random(); switch (state) { case RESTING: if (stateTime > restTime) { state = MOVING; accel.x = rand.nextFloat() * 2 - 1; // -1 to 1 accel.y = rand.nextFloat() * 2 - 1; accel.nor().scl(ACCEL); stateTime = 0.0f; } else { // still resting... } break; case MOVING: if (stateTime > moveTime) { state = RESTING; accel.set(0, 0); velocity.set(0, 0); stateTime = 0.0f; } else { // still moving... // Continue accel'ing in current direction accel.set(velocity.x, velocity.y).nor().scl(ACCEL); } break; default: break; } stateTime += deltaTime; }
@Override public void read(Json json, JsonValue jsonData) { Iterator<JsonValue> iter = jsonData.iterator(); JsonValue value; while (iter.hasNext()) { value = iter.next(); switch (value.name()) { case "name": name = value.asString(); break; case "dir": dir.x = value.get("x").asFloat(); dir.y = value.get("x").asFloat(); break; case "speed": speed = value.asFloat(); break; case "bounds": bounds.width = value.getFloat("width"); bounds.height = value.getFloat("height"); setPosition( new Vector2( value.getFloat("x") + bounds.width / 2, value.getFloat("y") + bounds.height / 2)); break; case "score": score.read(json, value); break; } } }
public void update(float delta) { lastShoot = null; boolean up = Gdx.input.isKeyPressed(Keys.W) || Gdx.input.isKeyPressed(Keys.UP); boolean down = Gdx.input.isKeyPressed(Keys.S) || Gdx.input.isKeyPressed(Keys.DOWN); boolean left = Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT); boolean right = Gdx.input.isKeyPressed(Keys.D) || Gdx.input.isKeyPressed(Keys.RIGHT); boolean shoot = Gdx.input.isTouched(); if (up && !down) { deltaPos.y += SPEED * delta; } else if (down && !up) { deltaPos.y -= SPEED * delta; } if (left && !right) { deltaPos.x -= SPEED * delta; } else if (right && !left) { deltaPos.x += SPEED * delta; } if (shoot && GameScreen.oldCursor == GameScreen.aim) { deltaPos.x = deltaPos.y = 0; shoot(); } dirX = Gdx.input.getX() - 400; dirY = 600 - Gdx.input.getY() - 300; angle = (float) (Math.toDegrees(Math.atan(dirY / dirX))) - 90; if (dirX >= 0) angle += 180; if (deltaPos.x != 0 || deltaPos.y != 0) currentAnimation = walking; else if (GameScreen.cursor == GameScreen.oldCursor && shoot) currentAnimation = shooting; else currentAnimation = idle; position.x += deltaPos.x; position.y += deltaPos.y; // if (Gdx.input.isKeyJustPressed(Keys.SPACE)) { // position.x += 70; // } // if (Gdx.input.isKeyJustPressed(Keys.P)) { // GameScreen.cam.zoom = (GameScreen.cam.zoom == 8 ? 1 : 8); // } animTime += delta; updateAABB(); }
private void drawTriangleStrips(ShapeRenderer shapeRenderer, Color color, Color color1) { for (int i = 0; i < vertexDataArray.size; i++) { StripVertex bb = vertexDataArray.items[i]; Array<Float> data = bb.insideVertexData; for (int j = 0; j < data.size - 3; ) { shapeRenderer.setColor(j == 0 ? color : color1); tmp.x = data.items[j]; tmp.y = data.items[j + 1]; tmp.rotateRad(angleRad); tmp.scl(scale); tmp.add(position); tmp1.x = data.items[j + 3]; tmp1.y = data.items[j + 4]; tmp1.rotateRad(angleRad); tmp1.scl(scale); tmp1.add(position); j += 3; shapeRenderer.line(tmp, tmp1); } data = bb.outsideVertexData; for (int j = 0; j < data.size - 3; ) { shapeRenderer.setColor(j == 0 ? Color.ORANGE : Color.RED); tmp.x = data.items[j]; tmp.y = data.items[j + 1]; tmp.rotateRad(angleRad); tmp.scl(scale); tmp.add(position); tmp1.x = data.items[j + 3]; tmp1.y = data.items[j + 4]; tmp1.rotateRad(angleRad); tmp1.scl(scale); tmp1.add(position); j += 3; shapeRenderer.line(tmp, tmp1); } } }
public void processCollisions(Array<Block> collidedBlocks) { for (Block block : collidedBlocks) { if (velocity.y <= 0 && distance(0, bounds.y, 0, block.bounds.y + block.bounds.height) < bounds.height / 2) { if ((distance(bounds.x + bounds.width, 0, block.bounds.x, 0) < .1 || distance(bounds.x, 0, block.bounds.x + block.bounds.width, 0) < .05) && !onGround && collidedBlocks.size > 1) { if (velocity.x > 0 && block.bounds.x > bounds.x) position.x = block.bounds.x - bounds.width; if (velocity.x < 0 && block.bounds.x < bounds.x) position.x = block.bounds.x + block.bounds.width; break; } velocity.y = 0; position.y = block.bounds.height + block.bounds.y; onGround = true; if (state.equals(State.JUMPING)) { setState(State.IDLE); } } else if (velocity.y > 0 && distance(0, bounds.y + bounds.height, 0, block.bounds.y) < bounds.height / 2) { if ((distance(bounds.x + bounds.width, 0, block.bounds.x, 0) < .1 || distance(bounds.x, 0, block.bounds.x + block.bounds.width, 0) < .05) && collidedBlocks.size > 1) { if (velocity.x > 0 && block.bounds.x > bounds.x) { position.x = block.bounds.x - bounds.width; System.out.println("going right"); } if (velocity.x < 0 && block.bounds.x < bounds.x) { System.out.println("going left"); position.x = block.bounds.x + block.bounds.width; } break; } position.y = block.bounds.y - bounds.height; velocity.y = 0; } else if (velocity.x > 0 && distance(bounds.x + bounds.width, 0, block.bounds.x, 0) < bounds.width / 2) { velocity.x = 0; position.x = block.bounds.x - bounds.width; } else if (velocity.x < 0 && distance(bounds.x, 0, block.bounds.x + block.bounds.width, 0) < bounds.width / 2) { velocity.x = 0; position.x = block.bounds.x + block.bounds.width; } } }
/** * Transforms the given vector by this transform * * @param v the vector */ public Vector2 mul(Vector2 v) { float x = vals[POS_X] + vals[COL1_X] * v.x + vals[COL2_X] * v.y; float y = vals[POS_Y] + vals[COL1_Y] * v.x + vals[COL2_Y] * v.y; v.x = x; v.y = y; return v; }
private void moveRight(float delta) { if (jumpState == Enums.JumpState.GROUNDED && walkState != Enums.WalkState.WALKING) { walkStartTime = TimeUtils.nanoTime(); } walkState = Enums.WalkState.WALKING; facing = Direction.RIGHT; position.x += delta * Constants.GIGAGAL_MOVE_SPEED; }
public void updateGameOver(float delta) { position.add(velocity.cpy().scl(delta)); if (position.x <= -getWidth()) { position.x = -getWidth() * 2; } bounds.setPosition(position); }
public void move(float x, float y) { position.x += x; position.y += y; Vector2 lPos = Utils.mapToScreen( new Vector2(position.x - 0.5F, position.y + 1), new Vector2(game.getMainCam().position.x, game.getMainCam().position.y)); label.setPosition(lPos.x, lPos.y); }
public void damage(float i, float x, float y) { long now = TimeUtils.nanosToMillis(TimeUtils.nanoTime()); if (now - lastDamaged > damageInterval) { lastDamaged = now; hp -= i; deltaPos.x += x; deltaPos.y += y; } }
public Car(float xKord, float yKord) { position = new Vector2(); size = new Vector2(Gdx.graphics.getWidth() / 5, Gdx.graphics.getWidth() / 5); speed = new Vector2(0, -700); collisionRect = new Rectangle(position.x, position.y, size.x, size.y); position.x = xKord; position.y = yKord; }
public void update(float delta) { position.add(velocity.cpy().scl(delta)); if (position.x <= -getWidth()) { position.x = Gdx.graphics.getWidth(); position.y = ((float) (Math.random() * (max))) - 10; } bounds.setPosition(position); }
private void setVelocity() { velocity = new Vector2(random.nextFloat() / 2, random.nextFloat() / 2); if (random.nextBoolean()) { velocity.x = -velocity.x; } if (random.nextBoolean()) { velocity.y = -velocity.y; } }
@Override public void reset() { lastPosition.x = 0; lastPosition.y = 0; position.x = 0; position.y = 0; dimensions.x = 0; dimensions.y = 0; type = GameObjectType.NONE; velocity.x = 0; velocity.y = 0; accelleration.x = 0; accelleration.y = 0; lastCollision = null; id = ""; scale.set(1f, 1f); color = Color.WHITE.cpy(); uuid = UUID.randomUUID().toString(); }