OSDN Git Service

Remove global shared state mutation from Vector2d.lerp()
authorbitiotic <github@bitiotic.com>
Wed, 30 Jan 2013 20:53:40 +0000 (12:53 -0800)
committerbitiotic <github@bitiotic.com>
Wed, 30 Jan 2013 21:15:26 +0000 (13:15 -0800)
Add a Vector2dTest test case

gdx/src/com/badlogic/gdx/math/Vector2.java
tests/gdx-tests/src/com/badlogic/gdx/tests/Vector2dTest.java [new file with mode: 0644]

index 32a2a40..c956cc7 100644 (file)
@@ -285,9 +285,10 @@ public class Vector2 implements Serializable {
         * @param alpha The interpolation coefficient\r
         * @return This vector for chaining. */\r
        public Vector2 lerp (Vector2 target, float alpha) {\r
-               Vector2 r = this.mul(1.0f - alpha);\r
-               r.add(target.tmp().mul(alpha));\r
-               return r;\r
+               final float invAlpha = 1.0f - alpha;\r
+               this.x = (x * invAlpha) + (target.x * alpha);\r
+               this.y = (y * invAlpha) + (target.y * alpha);\r
+               return this;\r
        }\r
 \r
        @Override\r
diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/Vector2dTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/Vector2dTest.java
new file mode 100644 (file)
index 0000000..a2fda56
--- /dev/null
@@ -0,0 +1,97 @@
+package com.badlogic.gdx.tests;\r
+\r
+import com.badlogic.gdx.Gdx;\r
+import com.badlogic.gdx.graphics.Color;\r
+import com.badlogic.gdx.graphics.GL10;\r
+import com.badlogic.gdx.graphics.OrthographicCamera;\r
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer;\r
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;\r
+import com.badlogic.gdx.math.MathUtils;\r
+import com.badlogic.gdx.math.Vector2;\r
+import com.badlogic.gdx.scenes.scene2d.Stage;\r
+import com.badlogic.gdx.scenes.scene2d.ui.Table;\r
+import com.badlogic.gdx.tests.utils.GdxTest;\r
+\r
+public class Vector2dTest extends GdxTest {\r
+       private ShapeRenderer renderer;\r
+       private OrthographicCamera camera;\r
+\r
+       private Vector2 rotating = new Vector2(Vector2.X);\r
+       private Vector2 scalingX = new Vector2(Vector2.Y);\r
+       private Vector2 scalingY = new Vector2(Vector2.X);\r
+       private Vector2 lerping1 = new Vector2(Vector2.X);\r
+       private Vector2 lerpTarget = new Vector2(Vector2.Y);\r
+       private Vector2 sum = new Vector2().add(Vector2.X).add(Vector2.Y).nor();\r
+       private Vector2 mash = new Vector2(Vector2.Y);\r
+\r
+       \r
+       private final long start = System.currentTimeMillis();\r
+       \r
+       @Override\r
+       public void create () {\r
+               renderer = new ShapeRenderer();\r
+       }\r
+       \r
+       private void renderVectorAt(float x, float y, Vector2 v) {\r
+               renderer.line(x, y, x + v.x, y + v.y);\r
+       }\r
+\r
+       @Override\r
+       public void render () {\r
+               Gdx.gl.glClearColor(0, 0, 0, 0);\r
+               Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);\r
+               \r
+               renderer.setProjectionMatrix(camera.combined);\r
+               \r
+               // Render the 'lerp' vector target as a circle\r
+               renderer.begin(ShapeType.FilledCircle);\r
+               renderer.setColor(1.0f, 0, 0, 0.3f);\r
+               renderer.filledCircle(-2 + lerpTarget.x, 2 + lerpTarget.y , 0.08f, 16);\r
+               renderer.end();\r
+\r
+               renderer.begin(ShapeType.Line);\r
+               \r
+               // Render the three fixed X, Y and sum vectors:\r
+               renderer.setColor(Color.RED);\r
+               renderVectorAt(0, 0, Vector2.X);\r
+               renderer.setColor(Color.GREEN);\r
+               renderVectorAt(0, 0, Vector2.Y);\r
+               renderer.setColor(Color.YELLOW);\r
+               renderVectorAt(0, 0, sum);\r
+               \r
+               final float changeRate = Gdx.graphics.getDeltaTime();\r
+               renderer.setColor(Color.WHITE);\r
+               \r
+               renderVectorAt(2, 2, rotating);\r
+               rotating.rotate(93 * changeRate);\r
+               \r
+               renderVectorAt(2, -2, scalingX);\r
+               scalingX.set(0, MathUtils.sin((System.currentTimeMillis() - start)/520.0f));\r
+               renderVectorAt(2, -2, scalingY);\r
+               scalingY.set(MathUtils.cos((System.currentTimeMillis() - start)/260.0f), 0);\r
+               \r
+               renderVectorAt(-2, 2, lerping1);\r
+               lerping1.lerp(lerpTarget, 0.025f);\r
+               if (lerping1.epsilonEquals(lerpTarget, 0.05f)) {\r
+                       lerpTarget.set(-1.0f + MathUtils.random(2.0f), -1.0f + MathUtils.random(2.0f)).nor();\r
+               }\r
+\r
+               renderVectorAt(-2, -2, mash);\r
+               mash.set(0, 0).add(rotating).add(scalingX).add(scalingY).add(lerping1);\r
+       \r
+               renderer.end();\r
+       }\r
+\r
+       @Override\r
+       public void resize (int width, int height) {\r
+               float ratio = ((float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight());\r
+               int h = 10;\r
+               int w = (int)(h * ratio);\r
+               camera = new OrthographicCamera(w, h);\r
+       }\r
+       \r
+       @Override\r
+       public boolean needsGL20 () {\r
+               return false;\r
+       }\r
+}\r