/** Returns a point on the segment nearest to the specified point. */ public static Vector2 nearestSegmentPoint (Vector2 start, Vector2 end, Vector2 point, Vector2 nearest) { float length2 = start.dst2(end); if (length2 == 0) return nearest.set(start); float t = ((point.x - start.x) * (end.x - start.x) + (point.y - start.y) * (end.y - start.y)) / length2; if (t < 0) return nearest.set(start); if (t > 1) return nearest.set(end); return nearest.set(start.x + t * (end.x - start.x), start.y + t * (end.y - start.y)); }
/** Returns a point on the segment nearest to the specified point. */ public static Vector2 nearestSegmentPoint (Vector2 start, Vector2 end, Vector2 point, Vector2 nearest) { float length2 = start.dst2(end); if (length2 == 0) return nearest.set(start); float t = ((point.x - start.x) * (end.x - start.x) + (point.y - start.y) * (end.y - start.y)) / length2; if (t < 0) return nearest.set(start); if (t > 1) return nearest.set(end); return nearest.set(start.x + t * (end.x - start.x), start.y + t * (end.y - start.y)); }
public static Vector2 getClosestNode(float x, float y, List<Vector2> nodes) { Vector2 closest = null; float closestDistance = Float.MAX_VALUE; for(Vector2 vertex : nodes){ float distance = vertex.dst2(x, y); if(closest == null || closestDistance > distance){ closest = vertex; closestDistance = distance; } } return closest; }
public static boolean isBetween (Vector2 a, Vector2 b, Vector2 c) { float dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y) * (b.y - a.y); if (dotproduct < 0) { return false; } float squaredlengthba = a.dst2(b); if (dotproduct > squaredlengthba) { return false; } return true; }
public GDXLevel getClosestLevel(float x, float y) { GDXLevel closest = null; float closestDistance = Float.MAX_VALUE; for(GDXLevel level : levels){ float distance = level.getCoordinates().dst2(x, y); if(closest == null || closestDistance > distance){ closest = level; closestDistance = distance; } } return closest; }
public GDXParticle getClosestParticle(float x, float y) { GDXParticle closest = null; float closestDistance = Float.MAX_VALUE; for(GDXParticle path : particles){ float distance = path.getPosition().dst2(x, y); if(closest == null || closestDistance > distance){ closest = path; closestDistance = distance; } } return closest; }
@Override public float getDistance(float x, float y) { float closestDistance = Float.MAX_VALUE; for(Vector2 vertex : vertices) closestDistance = Math.min(closestDistance, vertex.cpy().add(center).dst2(x, y)); return (float) Math.sqrt(closestDistance);//just doing one sqrt, assuming its faster..? }
public GDXJoint getClosestJoint(float x, float y, World world) { GDXJoint closest = null; float closestDistance = Float.MAX_VALUE; for(GDXJoint path : getJoints()){ float distance = path.getCenter().dst2(x, y); if(closest == null || closestDistance > distance){ closest = path; closestDistance = distance; } } return closest; }
public GDXNPC getClosestNPC(float x, float y) { GDXNPC closest = null; float closestDistance = Float.MAX_VALUE; for(GDXNPC npc : getNpcs()){ float distance = npc.getCoordinates().dst2(x, y); if(closest == null || closestDistance > distance){ closest = npc; closestDistance = distance; } } return closest; }
@Override public void setInfo(Cell deadCell, Vector2 culprit) { cell = deadCell; vanishSize = cell.size; vanishColor = cell.getColorCopy(); vanishLifetime = 1f; // The vanish distance is this measure (distanceĀ² + sizeĀ³ * 20% size) // because it seems good enough. The more the distance, the more the // delay, but we decrease the delay depending on the cell size too or // it would be way too high Vector2 center = new Vector2(cell.pos.x + cell.size * 0.5f, cell.pos.y + 0.5f); float vanishDist = Vector2.dst2( culprit.x, culprit.y, center.x, center.y) / ((float) Math.pow(cell.size, 4.0f) * 0.2f); // Negative time = delay, + 0.4*lifetime because elastic interpolation has that delay vanishElapsed = vanishLifetime * 0.4f - vanishDist; }
public GDXPath getClosestPath(float x, float y) { GDXPath closest = null; float closestDistance = Float.MAX_VALUE; for(GDXPath path : getPaths()){ for(Vector2 node : path.getNodes()){ float distance = node.dst2(x, y); if(closest == null || closestDistance > distance){ closest = path; closestDistance = distance; } } } return closest; }
public boolean check_collision(Player player) { Vector2 pvector=new Vector2(player.xcenter , player.ycenter); Vector2 svector=new Vector2(this.xcenter , this.ycenter); float radi=player.radius; if( pvector.dst2(svector) < (width + radi) * (width + radi) + (height + radi) * (height + radi) )// dont check if player is too far for collision { Vector2 rvector= pvector.sub(svector);// rvector from square center to player center rvector.setangle(rvector.angle()+ this.rotation);//make sure rotations is CCW pvector=rvector.add(svector); //new player vector to check collision return new Rectangle(pvector.x-radi/2f ,pvector.y-radi/2f,radi,radi).overlaps(new Rectangle(svector.x-width/2f ,svector.y-height/2f,width,height)); //assume that player is also rectangle because we already checked worst case with if condition. } return false; }
public static float distance2( final Entity a, final Entity b) { final Pos pa = (Pos)a.getComponent(Pos.class); final Pos pb = (Pos)b.getComponent(Pos.class); return tmp.set(pa.xy.x, pa.xy.y).dst2(pb.xy.x, pb.xy.y); }
public static float distance2( final Entity a, final Entity b) { final Pos pa = (Pos)a.getComponent(Pos.class); final Pos pb = (Pos)b.getComponent(Pos.class); return tmp.set(pa.xy.x, pa.xy.y).dst2(pb.xy.x, pb.xy.y); }
public static float distance2( final Entity a, final Entity b) { final Pos pa = (Pos)a.getComponent(Pos.class); final Pos pb = (Pos)b.getComponent(Pos.class); return tmp.set(pa.x, pa.y).dst2(pb.x, pb.y); }
/** Returns a point on the segment nearest to the specified point. */ public static Vector2 nearestSegmentPoint (Vector2 start, Vector2 end, Vector2 point, Vector2 nearest) { float length2 = start.dst2(end); if (length2 == 0) return nearest.set(start); float t = ((point.x - start.x) * (end.x - start.x) + (point.y - start.y) * (end.y - start.y)) / length2; if (t < 0) return nearest.set(start); if (t > 1) return nearest.set(end); return nearest.set(start.x + t * (end.x - start.x), start.y + t * (end.y - start.y)); }
/** * Get distance to given creature * * @param creature the creature to measure distance to * @return the distance yo creature */ public float getDistanceToCreature(CreatureControl creature) { // FIXME: now just direct distance, should be perhaps real distance that the creature needs to traverse to reach the target if (getPosition() == null || creature.getPosition() == null) { return Float.MAX_VALUE; } return getPosition().dst2(creature.getPosition()); }
/** * @return closest body to the given x,y in world coordinates */ public static Body closestBody(World world, float x, float y){ Body body = null; float closestDistance = Float.MAX_VALUE; Array<Body> bodyArray = new Array<>(world.getBodyCount()); world.getBodies(bodyArray); for(Body worldBody : bodyArray){ float distance = worldBody.getPosition().dst2(x,y); if(body == null || closestDistance > distance){ body = worldBody; closestDistance = distance; } } return body; } }
@Override public boolean touchDragged(int screenX, int screenY, int pointer) { dragCurrent.set(screenX, screenY); if (dragCurrent.dst2(lastTouchDown) > GameSettings.MOUSE_DRAG_THRESHOLD) { isDragging = true; if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) { dragCurrentRay.set(viewport.getPickRay(dragCurrent.x, dragCurrent.y)); lastDragProcessedRay.set(viewport.getPickRay(lastDragProcessed.x, lastDragProcessed.y)); cameraController.processDragPan(dragCurrentRay, lastDragProcessedRay); } else if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)) { cursorDelta.set(lastDragProcessed).sub(screenX, screenY).scl(GameSettings.MOUSE_SENSITIVITY); cameraController.processDragRotation(cursorDelta); } lastDragProcessed.set(screenX, screenY); } mouseMoved(screenX, screenY); return true; }