List of usage examples for com.badlogic.gdx.math Vector2 scl
@Override
public Vector2 scl(Vector2 v)
From source file:com.tnf.ptm.handler.input.Shooter.java
License:Apache License
public static float calcShootAngle(Vector2 gunPos, Vector2 gunSpd, Vector2 ePos, Vector2 eSpd, float projSpd, boolean sharp) { Vector2 eSpdShortened = PtmMath.getVec(eSpd); if (!sharp) { eSpdShortened.scl(E_SPD_PERC); }//from ww w. j a v a 2 s .com Vector2 relESpd = PtmMath.distVec(gunSpd, eSpdShortened); PtmMath.free(eSpdShortened); float rotAngle = PtmMath.angle(relESpd); float v = relESpd.len(); float v2 = projSpd; PtmMath.free(relESpd); Vector2 toE = PtmMath.distVec(gunPos, ePos); PtmMath.rotate(toE, -rotAngle); float x = toE.x; float y = toE.y; float a = v * v - v2 * v2; float b = 2 * x * v; float c = x * x + y * y; float t = PtmMath.genQuad(a, b, c); float res; if (t != t) { res = Float.NaN; } else { toE.x += t * v; res = PtmMath.angle(toE) + rotAngle; } PtmMath.free(toE); return res; }
From source file:com.torrosoft.sopistan.SopistanMain.java
License:Open Source License
void drawDebug() { Array<Vector2> input = swipe.getInput(); // draw the raw input shapes.begin(ShapeType.Line); shapes.setColor(Color.GRAY);// w ww . jav a 2s . c o m for (int i = 0; i < input.size - 1; i++) { Vector2 p = input.get(i); Vector2 p2 = input.get(i + 1); shapes.line(p.x, p.y, p2.x, p2.y); } shapes.end(); // draw the smoothed and simplified path shapes.begin(ShapeType.Line); shapes.setColor(Color.RED); Array<Vector2> out = swipe.getPath(); for (int i = 0; i < out.size - 1; i++) { Vector2 p = out.get(i); Vector2 p2 = out.get(i + 1); shapes.line(p.x, p.y, p2.x, p2.y); } shapes.end(); // render our perpendiculars shapes.begin(ShapeType.Line); Vector2 perp = new Vector2(); for (int i = 1; i < input.size - 1; i++) { Vector2 p = input.get(i); Vector2 p2 = input.get(i + 1); shapes.setColor(Color.LIGHT_GRAY); perp.set(p).sub(p2).nor(); perp.set(perp.y, -perp.x); perp.scl(10f); shapes.line(p.x, p.y, p.x + perp.x, p.y + perp.y); perp.scl(-1f); shapes.setColor(Color.BLUE); shapes.line(p.x, p.y, p.x + perp.x, p.y + perp.y); } shapes.end(); }
From source file:com.trueMagic.object.creature.Player.java
@Override public void gameTick(float deltaTime) { energyPool += deltaTime * 5;/*from w w w . ja v a 2 s . c om*/ if (energyPool > maxEnergy) { energyPool = maxEnergy; } Vector2 mousePosition = this.worldManager.getGame() .unprojectVector(new Vector2(Gdx.input.getX(), Gdx.input.getY())); mousePosition.scl(1f / 64f); if (mousePosition.x > this.body.getPosition().x) { facingRight = true; } else { facingRight = false; } super.gameTick(deltaTime); }
From source file:com.uwsoft.editor.gdx.ui.components.ItemPhysicsEditor.java
License:Apache License
private void crateEdgPlatform(float x, float y, float w, float h) { BodyDef bodyDef = new BodyDef(); Vector2 vec = new Vector2(x, y); this.localToStageCoordinates(vec); vec.scl(PhysicsBodyLoader.SCALE); bodyDef.position.set(vec);/*from ww w.jav a 2 s. c o m*/ bodyDef.type = BodyDef.BodyType.StaticBody; Body platformBody = physicsEditorWorld.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); EdgeShape polygonShape = new EdgeShape(); polygonShape.set(0, 0, w * PhysicsBodyLoader.SCALE, h * PhysicsBodyLoader.SCALE); fixtureDef.shape = polygonShape; platformBody.createFixture(fixtureDef); edgBodyList.add(platformBody); }
From source file:com.uwsoft.editor.gdx.ui.components.ItemPhysicsEditor.java
License:Apache License
private void setListeners() { addListener(new InputListener() { public float touchDownX; public float touchDownY; @Override/*w w w . ja v a 2 s . c o m*/ public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (currentMode == EditMode.Test) { touchDownX = x; touchDownY = y; return true; } stage.setKeyboardFocus(ItemPhysicsEditor.this); localToStageCoordinates(tmpvector.set(x, y)); if (vertices.length == 0) { verticesList.add(tmpvector.cpy()); System.out.println("ADD POINT " + tmpvector.toString()); } selectedPoint.unSet(); if (currentMode == EditMode.Edit && vertices.length > 1) { for (int i = 0; i < vertices.length; i++) { //shapeRenderer.rect(vertices[i-1]-POINT_WIDTH/2, vertices[i]-POINT_WIDTH/2, POINT_WIDTH, POINT_WIDTH); if (tmpvector.x >= (vertices[i].x - POINT_WIDTH / 2) && tmpvector.x <= (vertices[i].x + POINT_WIDTH / 2) && tmpvector.y >= (vertices[i].y - POINT_WIDTH / 2) && tmpvector.y <= (vertices[i].y + POINT_WIDTH / 2)) { selectedPoint.set(vertices[i], i); //System.out.println("waaaaaaaaaaaaaaaaaaaaaaaaa"); break; } } if (currentMode == EditMode.Edit && lineIndex >= 0 && !selectedPoint.isSet) { // if(lineIndex == vertices.length-1){ // verticesList.add(tmpvector.cpy()); // }else{ verticesList.add(lineIndex, tmpvector.cpy()); selectedPoint.set(vertices[lineIndex], lineIndex); vertices = verticesList.toArray(new Vector2[0]); //} finish(); } } // else if (currentMode==EditMode.Test){ // } isDragging = false; return true; } @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { if (currentMode == EditMode.Test) { BodyDef bodyDef = new BodyDef(); Vector2 vec = new Vector2(touchDownX, touchDownY); ItemPhysicsEditor.this.localToStageCoordinates(vec); vec.scl(PhysicsBodyLoader.SCALE); bodyDef.position.set(vec); bodyDef.type = BodyDef.BodyType.DynamicBody; Body body = physicsEditorWorld.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = new CircleShape(); fixtureDef.shape.setRadius(10 * PhysicsBodyLoader.SCALE); body.createFixture(fixtureDef); body.applyLinearImpulse((x - touchDownX) * PhysicsBodyLoader.SCALE * 5, (y - touchDownY) * PhysicsBodyLoader.SCALE * 5, 0, 0, false); return; } System.out.println("waaat up"); isDragging = true; localToStageCoordinates(tmpvector.set(x, y)); if (currentMode == EditMode.Edit) { finish(); //selectedPoint.unSet(); nearestPoint.unSet(); return; } if (nearestPoint.isSet && nearestPoint.index == 0 && currentMode == EditMode.Create) { finish(); selectedPoint.unSet(); nearestPoint.unSet(); return; } if (vertices.length > 0 && !checkForIntersection(vertices[vertices.length - 1], tmpvector) && currentMode == EditMode.Create) { if (!verticesList.contains(tmpvector)) { verticesList.add(tmpvector.cpy()); System.out.println("ADD POINT " + tmpvector.toString()); } } nextPoint.set(tmpvector); vertices = verticesList.toArray(new Vector2[0]); } @Override public void touchDragged(InputEvent event, float x, float y, int pointer) { if (selectedPoint.isSet) { localToStageCoordinates(tmpvector.set(x, y)); vertices[selectedPoint.index].set(tmpvector); verticesList.get(selectedPoint.index).set(tmpvector); finish(); } super.touchDragged(event, x, y, pointer); } @Override public boolean mouseMoved(InputEvent event, float x, float y) { localToStageCoordinates(tmpvector.set(x, y)); if (vertices.length > 0 && isDragging && currentMode == EditMode.Create) { nextPoint.set(tmpvector); lastLineColor = goodColor; if (checkForIntersection(vertices[vertices.length - 1], nextPoint)) { lastLineColor = badColor; } } nearestPoint.unSet(); for (int i = 0; i < vertices.length; i++) { if (vertices.length > 1 && tmpvector.x >= (vertices[i].x - POINT_WIDTH / 2) && tmpvector.x <= (vertices[i].x + POINT_WIDTH / 2) && tmpvector.y >= (vertices[i].y - POINT_WIDTH / 2) && tmpvector.y <= (vertices[i].y + POINT_WIDTH / 2)) { nearestPoint.set(vertices[i], i); //System.out.println("PTUK SET " +i); //System.out.println(tmpvector.x+">="+(vertices[i].x-POINT_WIDTH/2)+" && " +tmpvector.x+"<="+(vertices[i].x+POINT_WIDTH/2)+" && "+tmpvector.y+">="+(vertices[i].y-POINT_WIDTH/2) +" && "+ tmpvector.y+"<="+(vertices[i].y+POINT_WIDTH/2)); break; } } if (currentMode == EditMode.Edit && !nearestPoint.isSet) { lineIndex = -1; for (int i = 1; i < vertices.length; i++) { if (Intersector.intersectSegmentCircle(vertices[i - 1], vertices[i], tmpvector, CIRCLE_RADIUS)) { lineIndex = i; break; } } if (vertices.length > 0 && Intersector.intersectSegmentCircle(vertices[vertices.length - 1], vertices[0], tmpvector, CIRCLE_RADIUS)) { lineIndex = 0; } } return super.mouseMoved(event, x, y); } @Override public boolean scrolled(InputEvent event, float x, float y, int amount) { if (currentMode == EditMode.Test) { return false; } float oldzoomfactor = zoomFactor; zoomFactor += amount / 10f; if (zoomFactor <= 0.2f) { zoomFactor = 0.2f; } currentActor.setScale(zoomFactor); currentActor.setX((getWidth() - currentActor.getWidth() * zoomFactor) / 2); currentActor.setY((getHeight() - currentActor.getHeight() * zoomFactor) / 2); float mul = zoomFactor / oldzoomfactor; Vector2 diff = new Vector2(getWidth() / 2, getHeight() / 2); localToStageCoordinates(diff); for (int i = 0; i < verticesList.size(); i++) { verticesList.get(i).sub(diff); verticesList.get(i).scl(mul); verticesList.get(i).add(diff); } if (currentMode == EditMode.Edit) { finish(); } return super.scrolled(event, x, y, amount); } @Override public boolean keyDown(InputEvent event, int keycode) { System.out.println("DELETE DELE TE DELETE"); if (keycode == 67 && currentMode == EditMode.Edit && selectedPoint.isSet) { verticesList.remove(selectedPoint.index); selectedPoint.unSet(); finish(); } return super.keyDown(event, keycode); } private boolean checkForIntersection(Vector2 v1, Vector2 v2) { if (vertices.length < 3) { return false; } Vector2 intersect = new Vector2(); for (int i = 1; i < vertices.length - 1; i++) { if (Intersector.intersectSegments(v1, v2, vertices[i - 1], vertices[i], intersect)) { return true; } } return false; } }); }
From source file:com.uwsoft.editor.view.stage.Sandbox.java
License:Apache License
public Vector2 screenToWorld(Vector2 vector) { // TODO: now unproject doesnot do well too. I am completely lost here. how hard is it to do screen to world, madafakas. //getViewport().unproject(vector); int pixelPerWU = sceneControl.sceneLoader.getRm().getProjectVO().pixelToWorld; OrthographicCamera camera = Sandbox.getInstance().getCamera(); Viewport viewport = Sandbox.getInstance().getViewport(); vector.x = (vector.x - (viewport.getScreenWidth() / 2 - camera.position.x * pixelPerWU / camera.zoom)) * camera.zoom;//from w w w . j av a2 s.com vector.y = (vector.y - (viewport.getScreenHeight() / 2 - camera.position.y * pixelPerWU / camera.zoom)) * camera.zoom; vector.scl(1f / pixelPerWU); return vector; }
From source file:com.uwsoft.editor.view.stage.Sandbox.java
License:Apache License
public Vector2 worldToScreen(Vector2 vector) { // TODO: WTF, project had to work instead I am back to this barbarian methods of unholy land! //vector = getViewport().project(vector); int pixelPerWU = sceneControl.sceneLoader.getRm().getProjectVO().pixelToWorld; OrthographicCamera camera = Sandbox.getInstance().getCamera(); Viewport viewport = Sandbox.getInstance().getViewport(); vector.x = vector.x / camera.zoom + (viewport.getWorldWidth() / 2 - (camera.position.x) / camera.zoom); vector.y = vector.y / camera.zoom + (viewport.getWorldHeight() / 2 - (camera.position.y) / camera.zoom); vector.scl(pixelPerWU); return vector; }
From source file:com.vlaaad.dice.game.world.controllers.SpawnController.java
License:Open Source License
private DragListener createDragToSpawnListener(final WorldObjectView view, final Creature creature) { return new DragListener() { private float initialX; private float initialY; {/*from w ww . ja v a 2 s. c o m*/ setTapSquareSize(4); } private boolean shouldHideOnDrag; private WorldObjectView draggedView = ViewController.createView(world.viewer, world.playerColors, creature); @Override public void touchUp(InputEvent event, float x, float y, int pointer, int button) { if (!isDragging()) { creatureInfoWindow.show(new CreatureInfoWindow.Params(creature, world)); } if (shouldHideOnDrag) { table.clearActions(); table.addAction(Actions.alpha(1, 0.3f)); } super.touchUp(event, x, y, pointer, button); } @Override public void dragStart(InputEvent event, float x, float y, int pointer) { if (scrollingEnabled) { if (Math.abs(event.getStageX() - initialX) < Math.abs(event.getStageY() - initialY)) { event.getStage().cancelTouchFocusExcept(this, event.getListenerActor()); } else { return; } } if (placed.contains(creature)) return; // event.getStage().cancelTouchFocus(this, event.getListenerActor()); shouldHideOnDrag = shouldHideOnDrag(); if (shouldHideOnDrag) { table.clearActions(); table.addAction(Actions.alpha(0, 0.3f)); } view.getColor().a = 0f; view.setTouchable(Touchable.disabled); world.stage.addActor(draggedView); drag(event, x, y, pointer); SoundManager.instance.playSound("ui-button-down"); } @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { initialX = event.getStageX(); initialY = event.getStageY(); return super.touchDown(event, x, y, pointer, button); } @Override public void drag(InputEvent event, float x, float y, int pointer) { if (placed.contains(creature)) return; draggedView.setPosition(event.getStageX() - view.getWidth() / 2, event.getStageY() - view.getHeight() / 2); } @Override public void dragStop(InputEvent event, float x, float y, int pointer) { if (placed.contains(creature)) return; draggedView.remove(); Vector2 coordinate = world.getController(ViewController.class).root .stageToLocalCoordinates(new Vector2(draggedView.getX() + draggedView.getWidth() / 2, draggedView.getY() + draggedView.getHeight() / 2)); coordinate.scl(1 / ViewController.CELL_SIZE); final int cellX = MathUtils.floor(coordinate.x); final int cellY = MathUtils.floor(coordinate.y); WorldObject w = world.get(cellX, cellY); if (cellX < 0 || cellY < 0 || cellX >= world.width || cellY >= world.height || (w != null && !(w instanceof Creature && ((Creature) w).initialPlayer == world.viewer))) { view.getColor().a = 1f; view.setTouchable(Touchable.enabled); refreshStartButton(); return; } if (world.level.getElement(LevelElementType.spawn, cellX, cellY) == world.viewer.fraction) { if (w != null) { removeFromPlaced((Creature) w); } SoundManager.instance.playSound("ui-button-up"); place(creature, cellX, cellY); } else { view.setTouchable(Touchable.enabled); view.getColor().a = 1f; } } }; }
From source file:com.vlaaad.dice.game.world.controllers.ViewController.java
License:Open Source License
public Vector2 worldToStageCoordinates(Vector2 worldCoordinates) { return root.localToStageCoordinates(worldCoordinates.scl(ViewController.CELL_SIZE)); }
From source file:com.vlaaad.dice.game.world.controllers.ViewController.java
License:Open Source License
public Vector2 stageToWorldCoordinates(Vector2 input) { root.stageToLocalCoordinates(input); input.scl(1 / ViewController.CELL_SIZE); int x = MathUtils.floor(input.x); int y = MathUtils.floor(input.y); if (input.x < 0) { x = -Math.abs(x);//from w ww . jav a 2s.co m } if (input.y < 0) { y = -Math.abs(y); } input.set(x, y); return input; }