List of usage examples for com.badlogic.gdx.math Vector2 sub
@Override
public Vector2 sub(Vector2 v)
From source file:be.ac.ucl.lfsab1509.bouboule.game.body.Bonus.java
License:Open Source License
/** * Constructor for a Bonus object //from w w w . java 2 s.c om * @param px/py : initial position * @param angle : initial rotation * @param texRegionPath : Path to the image file * @param jsonFile : Path to the jsonFile if needed ( "" else) * @param jsonName : jsonName of the object ( must match the json file attribute ) * * public Bonus( final float px, final float py, * final float angle, final String texRegionPath, * final String jsonFile, final String jsonName, final short bonusType) */ public Bonus(final float angle, final String texRegionPath, final String jsonFile, final String jsonName, final Entity.BonusType bonusType) { super(); int size = GlobalSettings.ARENAWAYPOINTALLOW.size(); MapNode node = GlobalSettings.ARENAWAYPOINTALLOW.get(random.nextInt(size)); Vector2 pos = new Vector2(node.xToPixel() - 32, node.yToPixel() - 32); Vector2 radius = new Vector2(node.weightToPixel() * random.nextFloat(), 0); radius.rotate(random.nextInt(359)); pos.add(radius); this.texture = new TextureRegion(new Texture(texRegionPath)); this.sprite = new Sprite(texture); MakeBody(0, 0, 0, BodyType.StaticBody, 0, 0, true, pos, angle, jsonFile, jsonName, GraphicManager.convertToGame(texture.getRegionWidth())); //Ensure that the object don't rotate. body.setFixedRotation(true); //Create the userData of type Bonus and bonusType this.entity = new Entity(Entity.BONUS, true, bonusType); body.setUserData(this.entity); //Ensure that the body image position is set on the origin defined by //the jsonFile if (origin != null) { pos = positionVector.cpy(); pos = pos.sub(origin); sprite.setPosition(pos.x, pos.y); sprite.setOrigin(origin.x, origin.y); sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees); } // removed the bonus after a delay Timer.Task task = new Timer.Task() { @Override public void run() { entity.setAlive(false); } }; Timer timer = new Timer(); timer.scheduleTask(task, 10f); }
From source file:be.ac.ucl.lfsab1509.bouboule.game.body.Bouboule.java
License:Open Source License
/** * (non-Javadoc).//from www . ja v a 2s . c om * @see be.ac.ucl.lfsab1509.bouboule.game.body.GameBody# * draw(com.badlogic.gdx.graphics.g2d.SpriteBatch) */ public void draw(final SpriteBatch sp) { if (entity.isAlive()) { if (origin != null) { //Ensure that the body image position is set on the origin defined by //the jsonFile Vector2 pos = positionVector.cpy(); pos = pos.sub(origin); sprite.setPosition(pos.x, pos.y); sprite.setOrigin(origin.x, origin.y); sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees); sprite.draw(sp); } else { sp.draw(texture, positionVector.x, positionVector.y); } } }
From source file:br.cefetmg.games.minigames.FleeTheTartarus.java
private void spawnEnemy() { Vector2 tartarusPosition = new Vector2(); boolean appearFromSides = MathUtils.randomBoolean(); if (appearFromSides) { boolean appearFromLeft = MathUtils.randomBoolean(); if (appearFromLeft) { tartarusPosition.x = 0;/*from ww w. j a v a2s .c o m*/ tartarusPosition.y = MathUtils.random(Config.WORLD_HEIGHT); } else { tartarusPosition.x = Gdx.graphics.getWidth(); tartarusPosition.y = MathUtils.random(Config.WORLD_HEIGHT); } } else { boolean appearFromBottom = MathUtils.randomBoolean(); if (appearFromBottom) { tartarusPosition.y = 0; tartarusPosition.x = MathUtils.random(Config.WORLD_WIDTH); } else { tartarusPosition.y = Gdx.graphics.getHeight(); tartarusPosition.x = MathUtils.random(Config.WORLD_WIDTH); } } Vector2 obj = new Vector2(tooth.getToothPosition()); Vector2 tartarusSpeed = obj.sub(tartarusPosition).nor().scl(2 * this.maximumEnemySpeed); Tartarus enemy = new Tartarus(tartarusTexture); enemy.setPosition(tartarusPosition.x, tartarusPosition.y); enemy.setSpeed(tartarusSpeed); enemy.setScale(0.7f); enemies.add(enemy); tartarusAppearingSound.random().play(); //toca sempre a mesma musica }
From source file:br.cefetmg.games.minigames.SaveTheTeeth.java
private void throwFood() { Vector2 foodPos = new Vector2(); Vector2 fSpeed;/*from ww w. ja v a 2 s . c om*/ Vector2 goalCenter = new Vector2(); Vector2 goal = this.mouth.getBoundingRectangle().getCenter(goalCenter); float xOffset; float yOffset; boolean isGood = MathUtils.randomBoolean(); TextureRegion fTexture; Random rand = new Random(); if (isGood) { xOffset = Food.GOOD_WIDTH; yOffset = Food.GOOD_HEIGHT; fTexture = GoodFoodTextures[0][rand.nextInt(2)]; } else { xOffset = Food.BAD_WIDTH; yOffset = Food.BAD_HEIGHT; fTexture = BadFoodTextures[0][rand.nextInt(2)]; } if (MathUtils.randomBoolean()) { foodPos.x = MathUtils.randomBoolean() ? -xOffset : super.screen.viewport.getWorldWidth(); foodPos.y = MathUtils.random(-yOffset, super.screen.viewport.getWorldHeight()); } else { foodPos.y = MathUtils.randomBoolean() ? -yOffset : super.screen.viewport.getWorldHeight(); foodPos.x = MathUtils.random(-xOffset, super.screen.viewport.getWorldWidth()); } //fSpeed=new Vector2(goalCenter.x,goalCenter.y); //fSpeed.nor().scl(this.foodSpeed); fSpeed = goal.sub(foodPos).nor().scl(this.foodSpeed); Food f = new Food(fTexture, isGood); f.setSpeed(fSpeed); f.setPosition(foodPos.x, foodPos.y); food.add(f); }
From source file:com.bladecoder.engine.model.Sprite3DRenderer.java
License:Apache License
@Override public void startAnimation(String id, Tween.Type repeatType, int count, ActionCallback cb, Vector2 p0, Vector2 pf) {//from w w w . j a va 2s . c o m startAnimation(id, repeatType, count, null); if (currentAnimation != null) { Vector2 tmp = new Vector2(pf); float angle = tmp.sub(p0).angle() + 90; lookat(angle); } }
From source file:com.bladecoder.engineeditor.scneditor.ScnWidgetInputListener.java
License:Apache License
@Override public void touchDragged(InputEvent event, float x, float y, int pointer) { // EditorLogger.debug("Touch Dragged - X: " + Gdx.input.getX() + " Y: " // + Gdx.input.getY()); super.touchDragged(event, x, y, pointer); if (Gdx.input.isButtonPressed(Buttons.LEFT)) { Scene scn = scnWidget.getScene(); if (scn == null) return; Vector2 d = new Vector2(Gdx.input.getX(), Gdx.input.getY()); scnWidget.screenToWorldCoords(d); d.sub(org); org.add(d);//from w ww . j a v a 2 s. c o m if (draggingMode == DraggingModes.DRAGGING_ACTOR) { selActor.setPosition(selActor.getX() + d.x, selActor.getY() + d.y); Ctx.project.setModified(this, Project.POSITION_PROPERTY, null, selActor); } else if (draggingMode == DraggingModes.DRAGGING_BBOX_POINT) { Polygon poly = selActor.getBBox(); float verts[] = poly.getVertices(); verts[vertIndex] += d.x; verts[vertIndex + 1] += d.y; poly.dirty(); Ctx.project.setModified(); } else if (draggingMode == DraggingModes.DRAGGING_WALKZONE_POINT) { Polygon poly = scn.getPolygonalNavGraph().getWalkZone(); float verts[] = poly.getVertices(); verts[vertIndex] += d.x; verts[vertIndex + 1] += d.y; poly.dirty(); Ctx.project.setModified(); } else if (draggingMode == DraggingModes.DRAGGING_WALKZONE) { Polygon poly = scn.getPolygonalNavGraph().getWalkZone(); poly.translate(d.x, d.y); } else if (draggingMode == DraggingModes.DRAGGING_MARKER_0) { Vector2 depthVector = scnWidget.getScene().getDepthVector(); depthVector.x += d.y; Ctx.project.setModified(); updateFakeDepth(); } else if (draggingMode == DraggingModes.DRAGGING_MARKER_100) { Vector2 depthVector = scnWidget.getScene().getDepthVector(); depthVector.y += d.y; Ctx.project.setModified(); updateFakeDepth(); } } else if (Gdx.input.isButtonPressed(Buttons.RIGHT) || Gdx.input.isButtonPressed(Buttons.MIDDLE)) { Vector2 p = new Vector2(Gdx.input.getX(), Gdx.input.getY()); scnWidget.screenToWorldCoords(p); p.sub(org); scnWidget.translate(p); } }
From source file:com.dongbat.game.util.MovementUtil.java
/** * Move unit to specific location on map * * @param entity entity want to move//from w ww.j a v a 2 s .c om * @param destination location that entity will move to */ public static void moveTo(Entity entity, Vector2 destination) { World world = entity.getWorld(); Physics physicsComponent = EntityUtil.getComponent(world, entity, Physics.class); Stats stat = EntityUtil.getComponent(world, entity, Stats.class); Body body = physicsComponent.getBody(); float mass = body.getMass(); Vector2 currentVelocity = body.getLinearVelocity(); Vector2 position = body.getPosition(); float scale = stat.getBaseMovementSpeed() + stat.getModifierSpeed(); Vector2 desiredVelocity = destination.cpy().sub(position).nor().scl(scale); Vector2 impulse = desiredVelocity.sub(currentVelocity).scl(mass * 10); PhysicsUtil.applyImpulse(entity.getWorld(), entity, impulse); }
From source file:com.dongbat.invasion.system.EnemyMovementSystem.java
@Override protected void process(Entity entity) { Delay delay = delayMapper.get(entity); if (delay != null && delay.getDuration() > 0) { long timePassed = TimeUtil.getGameTimeSince(delay.getStartTime()); if (timePassed <= delay.getDuration()) { return; }/*from w w w .j a v a2 s . c o m*/ } // Vector2 prevVelocity = PhysicsUtil.getVelocity(entity).cpy(); Vector2 position = PhysicsUtil.getPosition(entity); EnemyMovement movement = EntityUtil.getComponent(entity, EnemyMovement.class); PhysicsUtil.setVelocity(entity, new Vector2()); Vector2 intent = movement.getIntent().cpy(); Vector2 travel; MovementUtil.moveEnemyTo(entity, intent); // TODO check free enemy intent.y = position.y; // TODO free movement unit use moveTo Vector2 distance = intent.cpy().sub(position); travel = PhysicsUtil.getVelocity(entity).cpy().scl(world.delta); if (travel.dot(distance) > 0 && travel.len2() - distance.len2() > 0) { PhysicsUtil.setPosition(entity, intent); PhysicsUtil.setVelocity(entity, new Vector2()); } if (TimeUtil.getGameTimeSince(movement.getStartDrag()) >= movement.getDragDuration()) { movement.setDraggedTo(null); } if (movement.getDraggedTo() == null) { return; } float max = movement.getDraggedTo().cpy().sub(movement.getDraggedFrom()).len2(); float actual = position.cpy().sub(movement.getDraggedFrom()).len2(); if (actual >= max) { return; } Vector2 drag = movement.getDraggedTo().cpy().sub(position); float dragLen2 = drag.len2(); if (dragLen2 > movement.getDragForce() * movement.getDragForce()) { drag.nor().scl(movement.getDragForce()); } PhysicsUtil.applyImpulseOnEnemy(entity, drag); // TODO fix this similar to above travel = PhysicsUtil.getVelocity(entity).cpy().scl(world.delta); if (travel.sub(drag).len2() <= Constants.GAME.EPSILON_SQUARED) { PhysicsUtil.setPosition(entity, movement.getDraggedTo()); PhysicsUtil.applyImpulseOnEnemy(entity, drag.scl(-1)); } }
From source file:com.dongbat.invasion.util.MovementUtil.java
public static void moveTo(Entity entity, Vector2 destination) { Physics physicsComponent = EntityUtil.getComponent(entity, Physics.class); Body body = physicsComponent.getBody(); float mass = body.getMass(); Vector2 currentVelocity = body.getLinearVelocity(); Vector2 position = body.getPosition(); Vector2 desiredVelocity = destination.cpy().sub(position).nor().scl(physicsComponent.getMaxSpeed()); Vector2 impulse = desiredVelocity.sub(currentVelocity).scl(mass); PhysicsUtil.applyImpulse(entity, impulse); }
From source file:com.esotericsoftware.spine.superspineboy.PlayerView.java
License:Open Source License
void update(float delta) { // When not shooting, reset the number of burst shots. if (!view.touched && burstTimer > 0) { burstTimer -= delta;/*from w w w .j a va 2s . co m*/ if (burstTimer < 0) burstShots = 0; } // If jump was pressed in the air, jump as soon as grounded. if (view.jumpPressed && player.isGrounded()) jump(); skeleton.setX(player.position.x + width / 2); skeleton.setY(player.position.y); if (!setAnimation(view.assets.playerStates.get(player.state), player.stateChanged)) animationState.update(delta); animationState.apply(skeleton); Vector2 mouse = temp1.set(Gdx.input.getX(), Gdx.input.getY()); view.viewport.unproject(mouse); // Determine if the player can shoot at the mouse position. canShoot = false; if (rearUpperArmBone == null || rearBracerBone == null || gunBone == null) canShoot = true; else if (player.hp > 0 && !view.ui.hasSplash && (Math.abs(skeleton.getY() - mouse.y) > 2.7f || Math.abs(skeleton.getX() - mouse.x) > 0.75f)) { // Store bone rotations from the animation that was applied. float rearUpperArmRotation = rearUpperArmBone.getRotation(); float rearBracerRotation = rearBracerBone.getRotation(); float gunRotation = gunBone.getRotation(); // Straighten the arm and don't flipX, so the arm can more easily point at the mouse. rearUpperArmBone.setRotation(0); float shootRotation = 11; if (animationState.getCurrent(1) == null) { rearBracerBone.setRotation(0); gunBone.setRotation(0); } else shootRotation += 25; // Use different rotation when shoot animation was applied. skeleton.setFlipX(false); skeleton.updateWorldTransform(); // Compute the arm's angle to the mouse, flipping it based on the direction the player faces. Vector2 bonePosition = temp2.set(rearUpperArmBone.getWorldX() + skeleton.getX(), rearUpperArmBone.getWorldY() + skeleton.getY()); float angle = bonePosition.sub(mouse).angle(); float behind = (angle < 90 || angle > 270) ? -1 : 1; if (behind == -1) angle = -angle; if (player.state == State.idle || (view.touched && (player.state == State.jump || player.state == State.fall))) player.dir = behind; if (behind != player.dir) angle = -angle; if (player.state != State.idle && behind != player.dir) { // Don't allow the player to shoot behind themselves unless idle. Use the rotations stored earlier from the animation. rearBracerBone.setRotation(rearBracerRotation); rearUpperArmBone.setRotation(rearUpperArmRotation); gunBone.setRotation(gunRotation); } else { if (behind == 1) angle += 180; // Adjust the angle upward based on the number of shots in the current burst. angle += kickbackAngle * Math.min(1, burstShots / kickbackShots) * (burstTimer / burstDuration); float gunArmAngle = angle - shootRotation; // Compute the head, torso and front arm angles so the player looks up or down. float headAngle; if (player.dir == -1) { angle += 360; if (angle < 180) headAngle = 25 * Interpolation.pow2In.apply(Math.min(1, angle / 50f)); else headAngle = -15 * Interpolation.pow2In.apply(1 - Math.max(0, angle - 310) / 50f); } else { if (angle < 360) headAngle = -15 * Interpolation.pow2In.apply(1 - Math.max(0, (angle - 310) / 50f)); else headAngle = 25 * Interpolation.pow2In.apply(1 - Math.max(0, (410 - angle) / 50f)); } float torsoAngle = headAngle * 0.75f; if (headBone != null) headBone.setRotation(headBone.getRotation() + headAngle); if (torsoBone != null) torsoBone.setRotation(torsoBone.getRotation() + torsoAngle); if (frontUpperArmBone != null) frontUpperArmBone.setRotation(frontUpperArmBone.getRotation() - headAngle * 1.4f); rearUpperArmBone.setRotation(gunArmAngle - torsoAngle - rearUpperArmBone.getWorldRotation()); canShoot = true; } } skeleton.setFlipX(player.dir == -1); skeleton.updateWorldTransform(); }