-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplineHelper.java
More file actions
132 lines (118 loc) · 4.88 KB
/
SplineHelper.java
File metadata and controls
132 lines (118 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.XmlReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.badlogic.gdx.math.CatmullRomSpline;
/** SplineHelper class provides helper methods for populating and traversing CatmullRomSpline objects using XML data generated by
* the libGDX Path Editor.
*
* Uses snippets from https://github.com/libgdx/libgdx/wiki/Path-interface-&-Splines
*
* @author bc_bytes
*/
public final class SplineHelper
{
private float screenWidth;
private float screenHeight;
public SplineHelper(float screenWidth, float screenHeight)
{
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
}
public Vector2[] FlipVerticesHorizontally(Vector2[] original)
{
Vector2[] flipped = new Vector2[original.length];
for (int i = 0; i < original.length; i++)
{
flipped[i] = new Vector2(-1 * original[i].x + screenWidth, original[i].y);
}
return flipped;
}
private Vector2[] FlipVerticesVertically(Vector2[] original)
{
Vector2[] flipped = new Vector2[original.length];
for (int i = 0; i < original.length; i++)
{
flipped[i] = new Vector2(original[i].x, -1 * original[i].y + screenHeight);
}
return flipped;
}
/** Method to load Path Editor XML file. XML data is assumed to be located in a folder named 'splines'.
*
* @param splineFileName String containing the name of the XML file containing the path data.
* @param reverse Boolean indicating if the order of the vertices should be reversed once read.
* @param flip Set this to true if you're using a coordinate system where 0,0 is top-left.
* @return Vector2 array containing vertex data for spline.
*/
public Vector2[] GetSplineData(String splineFileName, boolean reverse)
{
// read XML spline data
Array<XmlReader.Element> vertices = null;
List<Vector2> dataset = new ArrayList<Vector2>();
try
{
XmlReader reader = new XmlReader();
FileHandle xmlFile = Gdx.files.internal("splines/" + splineFileName + ".xml");
XmlReader.Element root = reader.parse(xmlFile);
vertices = root.getChildByName("vertices").getChildrenByName("vertex");
for (XmlReader.Element child : vertices)
{
String x = child.getChildByName("x").getText();
String y = child.getChildByName("y").getText();
dataset.add(new Vector2(Float.parseFloat(x), Float.parseFloat(y)));
}
}
catch (java.io.IOException e)
{
System.out.println("Error reading spline data: " + splineFileName);
}
if (reverse)
{
// reverses vertices in array
Collections.reverse(dataset);
}
// set vertices for spline path
Vector2[] data = new Vector2[dataset.size()];
data = dataset.toArray(data);
if (flip)
{
data = FlipVerticesVertically(data);
}
return data;
}
/** Method to traverse the vertices in a CatmullRomSpline object. Note that movement speed is constant. Call this method every
* frame to update the traversal.
*
* @param gameObject A GameObject representing the object you want to traverse the spline.
* The GameObject should maintain the state of its own spline, so must contain the following members:
* - Vector2 position
- float rotation
* - Vector2 splineOut
* - float splineCurrent
* - float splineSpeed
* @param CatmullRomSpline CatmullRomSpline object containing valid vertex data.
* @param boolean Boolean indicating if the object traversing the spline should point in the direction of travel.
*/
public void TraverseSpline(GameObject gameObject, CatmullRomSpline spline, boolean pointForward)
{
if (pointForward)
{
// set gameObject to point in the direction it is currently heading
spline.derivativeAt(gameObject.splineOut, gameObject.splineCurrent);
gameObject.rotation = gameObject.splineOut.angle();
}
// traverse spline path
gameObject.splineCurrent += (Gdx.graphics.getDeltaTime() * gameObject.splineSpeed) / gameObject.splineOut.len();
if (gameObject.splineCurrent >= 1)
{
gameObject.splineCurrent -= 1;
}
spline.valueAt(gameObject.splineOut, gameObject.splineCurrent);
gameObject.position.x = gameObject.splineOut.x;
gameObject.position.y = gameObject.splineOut.y;
}
}