--- /dev/null
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ *
+ * 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.badlogic.gdx.tests;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.InputProcessor;
+import com.badlogic.gdx.graphics.GL10;
+import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.graphics.g2d.Sprite;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer10;
+import com.badlogic.gdx.math.BSpline;
+import com.badlogic.gdx.math.Bezier;
+import com.badlogic.gdx.math.CatmullRomSpline;
+import com.badlogic.gdx.math.Path;
+import com.badlogic.gdx.math.Vector2;
+import com.badlogic.gdx.tests.utils.GdxTest;
+import com.badlogic.gdx.utils.Array;
+
+/** @author Xoppa */
+public class PathTest extends GdxTest {
+ int SAMPLE_POINTS = 100;
+ float SAMPLE_POINT_DISTANCE = 1f/SAMPLE_POINTS;
+
+ SpriteBatch spriteBatch;
+ ImmediateModeRenderer10 renderer;
+ Sprite obj;
+ Array<Path<Vector2>> paths = new Array<Path<Vector2>>();
+ int currentPath = 0;
+ float t;
+ float speed = 0.3f;
+ float wait = 0f;
+
+ @Override
+ public boolean needsGL20 () {
+ return false;
+ }
+
+ @Override
+ public void create () {
+ renderer = new ImmediateModeRenderer10();
+ spriteBatch = new SpriteBatch();
+ obj = new Sprite(new Texture(Gdx.files.internal("data/badlogicsmall.jpg")));
+
+ float w = Gdx.graphics.getWidth() - obj.getWidth();
+ float h = Gdx.graphics.getHeight() - obj.getHeight();
+
+ paths.add(new Bezier<Vector2>(new Vector2(0,0), new Vector2(w, h)));
+ paths.add(new Bezier<Vector2>(new Vector2(0,0), new Vector2(0, h), new Vector2(w, h)));
+ paths.add(new Bezier<Vector2>(new Vector2(0,0), new Vector2(w, 0), new Vector2(0, h), new Vector2(w, h)));
+
+ Vector2 cp[] = new Vector2[]{
+ new Vector2(0, 0), new Vector2(w * 0.25f, h * 0.5f), new Vector2(0, h), new Vector2(w*0.5f, h*0.75f),
+ new Vector2(w, h), new Vector2(w * 0.75f, h * 0.5f), new Vector2(w, 0), new Vector2(w*0.5f, h*0.25f)
+ };
+ paths.add(new BSpline<Vector2>(cp, 3, true));
+
+ paths.add(new CatmullRomSpline<Vector2>(cp, true));
+
+ Gdx.input.setInputProcessor(this);
+ }
+
+ final Vector2 tmpV = new Vector2();
+ @Override
+ public void render () {
+ GL10 gl = Gdx.graphics.getGL10();
+ gl.glClearColor(0.7f, 0.7f, 0.7f, 1);
+ gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
+
+ if (wait > 0)
+ wait -= Gdx.graphics.getDeltaTime();
+ else {
+ t += speed * Gdx.graphics.getDeltaTime();
+ while (t >= 1f) {
+ currentPath = (currentPath + 1) % paths.size;
+ t -= 1f;
+ }
+
+ paths.get(currentPath).valueAt(tmpV, t);
+ obj.setPosition(tmpV.x, tmpV.y);
+ }
+
+ spriteBatch.begin();
+
+ renderer.begin(GL10.GL_LINE_STRIP);
+ float val = 0f;
+ while (val <= 1f) {
+ renderer.color(0f, 0f, 0f, 1f);
+ paths.get(currentPath).valueAt(Vector2.tmp, val);
+ renderer.vertex(Vector2.tmp.x, Vector2.tmp.y, 0);
+ val += SAMPLE_POINT_DISTANCE;
+ }
+ renderer.end();
+
+ obj.draw(spriteBatch);
+ spriteBatch.end();
+ }
+
+ private void touch(int x, int y) {
+ t = paths.get(currentPath).approximate(tmpV.set(x, Gdx.graphics.getHeight()-y));
+ paths.get(currentPath).valueAt(tmpV, t);
+ obj.setPosition(tmpV.x, tmpV.y);
+ wait = 0.2f;
+ }
+
+ @Override
+ public boolean touchUp (int screenX, int screenY, int pointer, int button) {
+ touch(screenX, screenY);
+ return super.touchUp(screenX, screenY, pointer, button);
+ }
+
+ @Override
+ public boolean touchDragged (int screenX, int screenY, int pointer) {
+ touch(screenX, screenY);
+ return super.touchDragged(screenX, screenY, pointer);
+ }
+}
package com.badlogic.gdx.tests;\r
\r
import com.badlogic.gdx.Gdx;\r
-import com.badlogic.gdx.InputProcessor;\r
import com.badlogic.gdx.graphics.GL10;\r
-import com.badlogic.gdx.graphics.Texture;\r
-import com.badlogic.gdx.graphics.g2d.Sprite;\r
-import com.badlogic.gdx.graphics.g2d.SpriteBatch;\r
+import com.badlogic.gdx.graphics.OrthographicCamera;\r
import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer10;\r
-import com.badlogic.gdx.math.BSpline;\r
-import com.badlogic.gdx.math.Bezier;\r
import com.badlogic.gdx.math.CatmullRomSpline;\r
-import com.badlogic.gdx.math.Path;\r
-import com.badlogic.gdx.math.Vector2;\r
+import com.badlogic.gdx.math.Vector3;\r
import com.badlogic.gdx.tests.utils.GdxTest;\r
-import com.badlogic.gdx.utils.Array;\r
\r
-/** @author Xoppa */\r
public class SplineTest extends GdxTest {\r
- int SAMPLE_POINTS = 100;\r
- float SAMPLE_POINT_DISTANCE = 1f/SAMPLE_POINTS;\r
- \r
- SpriteBatch spriteBatch;\r
- ImmediateModeRenderer10 renderer;\r
- Sprite obj;\r
- Array<Path<Vector2>> paths = new Array<Path<Vector2>>();\r
- int currentPath = 0;\r
- float t;\r
- float speed = 0.3f;\r
- float wait = 0f;\r
\r
@Override\r
public boolean needsGL20 () {\r
return false;\r
}\r
- \r
+\r
+ final int CONTROL_POINTS = 10;\r
+ OrthographicCamera cam;\r
+ ImmediateModeRenderer10 renderer;\r
+ CatmullRomSpline spline;\r
+ Vector3[] path;\r
+\r
@Override\r
public void create () {\r
+ cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());\r
+ cam.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);\r
renderer = new ImmediateModeRenderer10();\r
- spriteBatch = new SpriteBatch();\r
- obj = new Sprite(new Texture(Gdx.files.internal("data/badlogicsmall.jpg")));\r
- \r
- float w = Gdx.graphics.getWidth() - obj.getWidth();\r
- float h = Gdx.graphics.getHeight() - obj.getHeight();\r
- \r
- paths.add(new Bezier<Vector2>(new Vector2(0,0), new Vector2(w, h)));\r
- paths.add(new Bezier<Vector2>(new Vector2(0,0), new Vector2(0, h), new Vector2(w, h)));\r
- paths.add(new Bezier<Vector2>(new Vector2(0,0), new Vector2(w, 0), new Vector2(0, h), new Vector2(w, h)));\r
- \r
- Vector2 cp[] = new Vector2[]{\r
- new Vector2(0, 0), new Vector2(w * 0.25f, h * 0.5f), new Vector2(0, h), new Vector2(w*0.5f, h*0.75f),\r
- new Vector2(w, h), new Vector2(w * 0.75f, h * 0.5f), new Vector2(w, 0), new Vector2(w*0.5f, h*0.25f)\r
- };\r
- paths.add(new BSpline<Vector2>(cp, 3, true));\r
- \r
- paths.add(new CatmullRomSpline<Vector2>(cp, true));\r
- \r
- Gdx.input.setInputProcessor(this);\r
+ spline = new CatmullRomSpline();\r
+ float x = 0;\r
+ float y = Gdx.graphics.getHeight() / 2;\r
+ spline.add(new Vector3(x - 50, y, 0));\r
+ for (int i = 0; i < CONTROL_POINTS; i++) {\r
+ spline.add(new Vector3(x, y, 0));\r
+ x += Gdx.graphics.getWidth() / (CONTROL_POINTS - 2);\r
+ }\r
+ spline.add(new Vector3(Gdx.graphics.getWidth() + 50, y, 0));\r
+ path = new Vector3[(CONTROL_POINTS - 2) * 7 - 1];\r
+ for (int i = 0; i < path.length; i++)\r
+ path[i] = new Vector3();\r
+ spline.getPath(path, 5);\r
}\r
- \r
- final Vector2 tmpV = new Vector2();\r
+\r
@Override\r
public void render () {\r
- GL10 gl = Gdx.graphics.getGL10();\r
- gl.glClearColor(0.7f, 0.7f, 0.7f, 1);\r
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT);\r
- \r
- if (wait > 0)\r
- wait -= Gdx.graphics.getDeltaTime();\r
- else {\r
- t += speed * Gdx.graphics.getDeltaTime();\r
- while (t >= 1f) {\r
- currentPath = (currentPath + 1) % paths.size;\r
- t -= 1f;\r
- }\r
- \r
- paths.get(currentPath).valueAt(tmpV, t);\r
- obj.setPosition(tmpV.x, tmpV.y);\r
+ Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);\r
+ cam.update();\r
+ Gdx.gl10.glMatrixMode(GL10.GL_PROJECTION);\r
+ Gdx.gl10.glLoadMatrixf(cam.projection.val, 0);\r
+ Gdx.gl10.glMatrixMode(GL10.GL_MODELVIEW);\r
+ Gdx.gl10.glLoadMatrixf(cam.view.val, 0);\r
+\r
+ renderer.begin(GL10.GL_TRIANGLES);\r
+ for (int i = 0; i < path.length - 1; i++) {\r
+ Vector3 point1 = path[i];\r
+ Vector3 point2 = path[i + 1];\r
+ renderer.color(1, 1, 1, 1);\r
+ renderer.vertex(point1.x, point1.y, 0);\r
+ renderer.color(1, 1, 1, 1);\r
+ renderer.vertex(point1.x, 0, 0);\r
+ renderer.color(1, 1, 1, 1);\r
+ renderer.vertex(point2.x, point2.y, 0);\r
+\r
+ renderer.color(1, 1, 1, 1);\r
+ renderer.vertex(point2.x, point2.y, 0);\r
+ renderer.color(1, 1, 1, 1);\r
+ renderer.vertex(point1.x, 0, 0);\r
+ renderer.color(1, 1, 1, 1);\r
+ renderer.vertex(point2.x, 0, 0);\r
}\r
- \r
- spriteBatch.begin();\r
- \r
- renderer.begin(GL10.GL_LINE_STRIP);\r
- float val = 0f;\r
- while (val <= 1f) {\r
- renderer.color(0f, 0f, 0f, 1f);\r
- paths.get(currentPath).valueAt(Vector2.tmp, val);\r
- renderer.vertex(Vector2.tmp.x, Vector2.tmp.y, 0);\r
- val += SAMPLE_POINT_DISTANCE;\r
+ renderer.end();\r
+\r
+ Gdx.gl10.glPointSize(4);\r
+ renderer.begin(GL10.GL_POINTS);\r
+ for (int i = 0; i < spline.getControlPoints().size(); i++) {\r
+ Vector3 point = spline.getControlPoints().get(i);\r
+ renderer.color(1, 0, 0, 1);\r
+ renderer.vertex(point.x, point.y, 0);\r
}\r
renderer.end();\r
- \r
- obj.draw(spriteBatch);\r
- spriteBatch.end();\r
- }\r
- \r
- private void touch(int x, int y) {\r
- t = paths.get(currentPath).approximate(tmpV.set(x, Gdx.graphics.getHeight()-y));\r
- paths.get(currentPath).valueAt(tmpV, t);\r
- obj.setPosition(tmpV.x, tmpV.y);\r
- wait = 0.2f; \r
- }\r
- \r
- @Override\r
- public boolean touchUp (int screenX, int screenY, int pointer, int button) {\r
- touch(screenX, screenY);\r
- return super.touchUp(screenX, screenY, pointer, button);\r
+ Gdx.gl10.glPointSize(1);\r
+\r
+ processInput();\r
}\r
- \r
- @Override\r
- public boolean touchDragged (int screenX, int screenY, int pointer) {\r
- touch(screenX, screenY);\r
- return super.touchDragged(screenX, screenY, pointer);\r
+\r
+ Vector3 point = new Vector3();\r
+\r
+ private void processInput () {\r
+// if(Gdx.input.isTouched()) {\r
+// Vector3 nearest = null;\r
+// float nearestDist = Float.MAX_VALUE;\r
+// point.set(cam.getScreenToWorldX(Gdx.input.getX()),\r
+// cam.getScreenToWorldY(Gdx.input.getY()),\r
+// 0);\r
+//\r
+// for(int i = 0; i < spline.getControlPoints().size(); i++) {\r
+// Vector3 controlPoint = spline.getControlPoints().get(i);\r
+// float dist = Math.abs(point.x - controlPoint.x);\r
+// if(dist < nearestDist) {\r
+// nearest = controlPoint;\r
+// nearestDist = dist;\r
+// }\r
+// }\r
+//\r
+// nearest.y += (point.y - nearest.y) * Gdx.graphics.getDeltaTime();\r
+// spline.getPath(path, 5);\r
+// }\r
}\r
}\r