List of usage examples for com.badlogic.gdx.math Vector2 cpy
@Override
public Vector2 cpy()
From source file:org.matheusdev.ror.controller.component.ComponentMovement.java
License:Open Source License
@Override public void apply(Entity entity) { Body body = entity.getBody();/* w ww . j a v a 2 s .c o m*/ moving = false; // If trying to move (pressing buttons on Keyboard, steering with Gamepad) if (xsteer != 0f || ysteer != 0f) { moving = true; if (Math.abs(xsteer) > Math.abs(ysteer)) { if (xsteer < 0) { direction = Dir.LEFT; } else { direction = Dir.RIGHT; } } else { if (ysteer < 0) { direction = Dir.DOWN; } else { direction = Dir.UP; } } } Vector2 linVel = body.getLinearVelocity(); if (linVel.len() > maxspeed) { body.setLinearVelocity(linVel.cpy().nor().scl(maxspeed)); } body.applyForceToCenter(strength * xsteer, strength * ysteer); if (friction > 1f && !moving) { body.setLinearVelocity(linVel.div(friction)); } }
From source file:org.matheusdev.ror.view.EntityView.java
License:Open Source License
public void draw(SpriteBatch batch, Entity e, Sprite sprite, float width, float xoffset, float yoffset) { Body body = e.getBody();//from w w w . j ava2 s. c o m // Super-Duper important Box2D-Magic code: // This should be / is in almost every Box2D project // It takes the Body and the associated sprite and // renders the sprite properly, using the body's // position, rotation and origin. final float worldToSprite = sprite.getWidth() / width; final float spriteToWorld = width / sprite.getWidth(); // Get body position: final float bodyX = e.getX(); final float bodyY = e.getY(); // Get body center: final Vector2 center = body.getLocalCenter(); final Vector2 massCenter = body.getMassData().center; center.sub(massCenter).add(xoffset, yoffset); // Compute sprite-space center: final Vector2 spriteCenter = new Vector2(sprite.getWidth() / 2, sprite.getHeight() / 2) .sub((center.cpy().scl(worldToSprite))); // Upload to sprite: sprite.setScale(1f * spriteToWorld); sprite.setRotation(e.getRotation() * MathUtils.radiansToDegrees); sprite.setOrigin(spriteCenter.x, spriteCenter.y); sprite.setPosition(bodyX - spriteCenter.x, bodyY - spriteCenter.y); // Draw Sprite: sprite.draw(batch); }
From source file:org.saltosion.pixelprophecy.gui.GUIManager.java
License:Open Source License
/** * Renders the GUINode (and all it's children) on screen. *//* w ww .j av a2 s . c o m*/ private void drawGUINode(GUINode node) { if (!node.isVisible()) { return; } Vector2 pos = node.getWorldLocation(); if (node instanceof ImageProperty) { Sprite sprite = ((ImageProperty) node).getSprite(); sprite.setScale(node.getScale() * guiScale); sprite.setPosition(pos.x - sprite.getWidth() / 2, pos.y - sprite.getHeight() / 2); sprite.draw(spriteBatch); } if (node instanceof TextProperty) { String text = ((TextProperty) node).getText(); font.setColor(Color.WHITE); font.getData().setScale(node.getScale() * guiScale); font.setColor(Color.WHITE); font.draw(spriteBatch, text, pos.x - (getTextWidth(text) / 2) * node.getScale() * guiScale, pos.y + getTextHeight() / 2); } if (Globals.GUI_DEBUG) { guiDebugRenderer.setColor(Globals.GUI_DEBUG_COLOR); if (node.hovered) { guiDebugRenderer.setColor(Globals.GUI_DEBUG_HOVER_COLOR); } guiDebugRenderer.line(pos.cpy().add(20 * guiScale, 20 * guiScale), pos.cpy().sub(20 * guiScale, 20 * guiScale)); guiDebugRenderer.line(pos.cpy().add(-20 * guiScale, 20 * guiScale), pos.cpy().add(20 * guiScale, -20 * guiScale)); guiDebugRenderer.circle(pos.x, pos.y, 20 * guiScale); } for (GUINode child : node.getChildren()) { drawGUINode(child); } }
From source file:org.saltosion.pixelprophecy.input.GUIInputAdapter.java
License:Open Source License
private ArrayList<GUINode> getInteractiveNodes(GUINode node, int screenX, int screenY, ArrayList<GUINode> interactives, boolean interactive) { Vector2 loc = node.getWorldLocation().cpy().sub(node.getSize().cpy().scl(.5f)); Vector2 locSize = loc.cpy().add(node.getSize()); float x1 = loc.x; float y1 = loc.y; float x2 = locSize.x; float y2 = locSize.y; if (x1 <= screenX && y1 <= screenY && x2 >= screenX && y2 >= screenY && (node.isVisible()) && (node.isInteractive() || node.isInteractive() == interactive)) { interactives.add(0, node);//from w w w . j a v a2s .c om } for (GUINode child : node.getChildren()) { interactives = getInteractiveNodes(child, screenX, screenY, interactives, interactive); } return interactives; }
From source file:quantum.game.Tree.java
License:Open Source License
public Tree(Simulation sim, Vector2 pos, int planet) { super(sim, pos); this.planet = planet; float angle = 0; Vector2 tmp = pos.cpy().sub(sim.getPlanet(planet).getPosition()); angle = (float) Math.toDegrees(Math.atan2(tmp.y, tmp.x)); root = new Branch(sim, this, planet, pos, angle, Constants.TREE_HEIGHT, 1); // timer.start(); start_time = System.nanoTime(); }
From source file:us.thirdmillenium.strategicassaultsimulator.agents.PuppetAgent.java
License:Apache License
@Override public void updateAgent(float deltaTime) { // Bounds Check - Do nothing if (this.CurrentPath == null) { return;//www . j a va 2s . c o m } else if (this.CurrentPath.getCount() < 1 || this.CurrentPathIndex < 0) { //writeTrainingData(); this.CurrentPath = null; return; } this.agentShoot = 0; // First, calculate inputs double[] timeStepData = calculateTrainingInputs(); // Collect next intermediate node to move to TileNode tempTile = this.CurrentPath.get(this.CurrentPathIndex); // Calculate pixel distance between positions Vector2 currentPosition = this.position.cpy(); Vector2 nextPosition = tempTile.getPixelVector2(); float distance = currentPosition.dst2(nextPosition); // Make sure to move as far as possible if (distance < (Params.AgentMaxMovement * Params.AgentMaxMovement)) { if (this.CurrentPathIndex + 1 < this.CurrentPath.getCount()) { this.CurrentPathIndex++; tempTile = this.CurrentPath.get(this.CurrentPathIndex); nextPosition = tempTile.getPixelVector2(); } else { // We have arrived! this.position.set(nextPosition.x, nextPosition.y); this.Sprite.setCenter(nextPosition.x, nextPosition.y); // Clear Path this.CurrentPath = null; this.CurrentPathIndex = -1; // Write Data //writeTrainingData(); return; } } // Collect angle information from direction Vector2 direction = nextPosition.cpy().sub(currentPosition).nor(); float desiredAngle = direction.angle() - 90; // The angle the sprite should be pointing (90 deg shift) desiredAngle = (desiredAngle < 0) ? 360 + desiredAngle : desiredAngle; // Bring back to 0 - 360 float wantedAngleChange = desiredAngle - this.Angle; // How much angle needs to be added between current angle and desired angle. // Rotate in shortest direction if (wantedAngleChange >= 180) { wantedAngleChange -= 360; } else if (wantedAngleChange <= -180) { wantedAngleChange += 360; } this.deltaAngle = wantedAngleChange; // Check that turn is legal (i.e. within Max Turn Per Frame) if (Math.abs(this.deltaAngle) > Params.AgentMaxTurnAngle) { this.deltaAngle = this.deltaAngle > 0 ? Params.AgentMaxTurnAngle : -Params.AgentMaxTurnAngle; } // Update Position this.position.x += direction.x * Params.AgentMaxMovement; this.position.y += direction.y * Params.AgentMaxMovement; this.Sprite.setCenter(this.position.x, this.position.y); // Update Rotation this.Angle += this.deltaAngle; // Keep between 0 and 360 (sanity purposes) if (this.Angle > 360) { this.Angle -= 360; } else if (this.Angle < 0) { this.Angle += 360; } this.Sprite.setRotation(this.Angle); // Tack on Training Outputs from observed change calculateTrainingOutputs(timeStepData, 52); // Finally, store snapshot of this time step for training this.trainingData.add(timeStepData); }
From source file:us.thirdmillenium.strategicassaultsimulator.agents.TrainingShooter.java
License:Apache License
/** * If Agent is alive, will scan surrounding for Trainee, and fire at it if not in cooldown. *//* w w w . j a va 2s .c om*/ public void updateAgent(float timeDiff) { // Check if Agent can shoot now if (!this.canShoot) { this.timeSinceLastShot += timeDiff; if (this.timeSinceLastShot > Params.AgentFireRate) { this.timeSinceLastShot = 0; this.canShoot = true; } else { return; } } // If Agent is alive and can shoot, scan for target in range. if (this.alive && this.canShoot) { // If a Trainee has wondered within firing range, fire at it. Iterator<AgentModel> itr = this.trainees.iterator(); while (itr.hasNext()) { AgentModel trainee = itr.next(); Vector2 traineePosition = trainee.getPosition(); // Trainee in Range, Shoot it! if (this.position.dst(traineePosition) < (Params.MapTileSize * 3)) { Vector2 direction = traineePosition.cpy().sub(this.position).nor(); Vector2 unitVec = new Vector2(0, 1); float angle = unitVec.angle(direction); this.sprite.setRotation(angle); this.bulletTracker.add(new GreenBullet(new Vector2(this.position.x, this.position.y), calcFireAngle(angle), this)); this.canShoot = false; this.timeSinceLastShot = 0; return; } } } }
From source file:vault.clockwork.actors.DustbinActor.java
License:Open Source License
/** * @see Actor#setPosition(com.badlogic.gdx.math.Vector2) * @param newPosition // ww w. j a v a 2 s.c om */ @Override public void setPosition(Vector2 newPosition) { body.setTransform(newPosition.cpy().scl(Physics.SCALE), body.getTransform().getRotation()); dustbinBg.setPosition(newPosition); }
From source file:vault.clockwork.actors.DustbinActorBg.java
License:Open Source License
/** * @see Actor#setPosition(com.badlogic.gdx.math.Vector2) * @param newPosition /*from ww w .j a va 2 s .c o m*/ */ @Override public void setPosition(Vector2 newPosition) { body.setTransform(newPosition.cpy().scl(Physics.SCALE), body.getTransform().getRotation()); }
From source file:vault.clockwork.editor.gui.GUIElement.java
License:Open Source License
/** * Calculates the element bounds./*from www . jav a2 s. c o m*/ * @param anchor * @param space * @param position * @param width * @param height * @return */ public static Rectangle calculateBounds(Anchor anchor, ScreenSpace space, Vector2 position, float width, float height) { final Vector2 screen = position.cpy(); // calculate screen spacing position switch (space) { case TopRight: screen.set(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); screen.x -= position.x; screen.y -= position.y; break; case TopLeft: screen.set(0, Gdx.graphics.getHeight()); screen.x += position.x; screen.y -= position.y; break; case BottomRight: screen.set(Gdx.graphics.getWidth(), 0); screen.x -= position.x; screen.y += position.y; break; case Center: screen.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2); screen.x += position.x; screen.y += position.y; break; } // calculate screen bounds over the anchor switch (anchor) { case TopRight: return new Rectangle(screen.x - width, screen.y - height, width, height); case TopLeft: return new Rectangle(screen.x, screen.y - height, width, height); case BottomRight: return new Rectangle(screen.x - width, screen.y, width, height); case Center: return new Rectangle(screen.x - width / 2, screen.y - height / 2, width, height); default: return new Rectangle(screen.x, screen.y, width, height); } }