Back to project page DolphinOES.
The source code is released under:
Apache License
If you think the Android project DolphinOES listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/******************************************************************************* * Copyright 2014 See AUTHORS file.// w ww.ja va2 s .co m * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package com.sidereal.dolphinoes.util; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.sidereal.dolphinoes.behaviors.triggers.Activatable; import com.sidereal.dolphinoes.behaviors.triggers.Collider; /** Provides static access to mathematical methods. * * @author Claudiu Bele */ public class Utility { public static Vector2 interpolate(Vector2 from, Vector2 to, float factor) { factor = Math.max(0, Math.min(1, factor)); float newX = from.x * (1 - factor) + to.x * factor; float newY = from.y * (1 - factor) + to.y * factor; from.x = newX; from.y = newY; return new Vector2(newX, newY); } public static float lerpTowards(float from, float to, float factor) { // clamp the factor factor = Math.max(0, Math.min(1, factor)); return from * (1 - factor) + to * factor; } public static float lerpTowards(float from, float to, float factor, float limit) { float newValue = lerpTowards(from, to, factor); if (Math.abs(newValue - to) <= limit) { newValue = to; } return newValue; } /** Adds the x and y values to the already-assigned values of the vector2 * passed * * @param vector * Variable to be updated * @param x * the value we want to add on the X axis * @param y * the value we want to add on the Y axis */ public static void setRelativePosition(Vector2 vector, float x, float y) { vector.x += x; vector.y += y; } public static void setRelativePosition(Vector3 vector, float x, float y, float z) { vector.x += x; vector.y += y; vector.z += z; } public static boolean rectanglesIntersect(Rectangle r1, Rectangle r2) { if (r2.contains(r1.x, r1.y) || r2.contains(r1.x, r1.y + r1.height) || r2.contains(r1.x + r1.width, r1.y) || r2.contains(r1.x + r1.width, r1.y + r1.height) || r1.contains(r2.x, r2.y) || r1.contains(r2.x, r2.y + r2.height) || r1.contains(r2.x + r2.width, r2.y) || r1.contains(r2.x + r2.width, r2.y + r2.height) || (((r2.y <= r1.y && r2.y >= (r1.y + r1.height)) || (r2.y + r2.height >= r1.y && r2.y + r1.height <= (r1.y + r1.height))) && ((r2.x <= r1.x && r2.x >= (r1.x + r1.width)) || (r2.x + r2.width >= r1.x && r2.x + r2.width <= (r1.x + r1.width)))) || (((r1.y <= r2.y && r1.y >= (r2.y + r2.height)) || (r1.y + r1.height >= r2.y && r1.y + r2.height <= (r2.y + r2.height))) && ((r1.x <= r2.x && r1.x >= (r2.x + r2.width)) || (r1.x + r1.width >= r2.x && r1.x + r1.width <= (r2.x + r2.width)))) ) { return true; } return false; } public static boolean doCollidersIntersect(Collider c1, Collider c2) { for (int i = 0; i < c1.getColliders().size(); i++) { for (int j = 0; j < c2.getColliders().size(); j++) { if (Utility.rectanglesIntersect(c1.getColliders().get(i).rect, c2.getColliders().get(j).rect)) { return true; } } } return false; } public static boolean doActivationAreasIntersect(Activatable c1, Activatable c2) { for (int i = 0; i < c1.getActivators().size(); i++) { for (int j = 0; j < c2.getActivators().size(); j++) { if (c1.getActivators().get(i).rect.overlaps(c2.getActivators().get(j).rect)) { return true; } } } return false; } public static Float[] fillArray(int size, Float value) { Float[] array = new Float[size]; for (int i = 0; i < size; i++) { array[i] = value; } return array; } public static Float[] fillArray(int size, Float initValue, Float increment, boolean goBackFromMiddle) { Float[] array = new Float[size]; for (int i = 0; i < size; i++) { if (goBackFromMiddle && i >= size / 2) { array[i] = initValue - increment * (i - size / 2); } else { array[i] = initValue + increment * i; } } return array; } public static float moveTowards(float from, float to, float factor, boolean onlyTowards) { if (from == to) return from; if (onlyTowards) { if (from < to && to - from < to - (from + factor)) factor *= -1; if (from > to && from - to < (from + factor) - to) factor *= -1; } float newValue = from + factor; if (from > to && newValue < to) newValue = to; if (from < to && newValue > to) newValue = to; return newValue; } }