Starsector API
Loading...
Searching...
No Matches
SmoothMovementUtil.java
Go to the documentation of this file.
1package com.fs.starfarer.api.util;
2
3import org.lwjgl.util.vector.ReadableVector2f;
4import org.lwjgl.util.vector.Vector2f;
5
6public class SmoothMovementUtil {
7
8 protected Vector2f vel = new Vector2f();
9 protected Vector2f loc = new Vector2f();
10 protected Vector2f accel = new Vector2f();
11
12 protected Vector2f dest = new Vector2f();
13 protected Vector2f destVel = new Vector2f();
14
15 protected float acceleration, maxSpeed;
16 protected boolean smoothCap = false;
17
18 protected float hardSpeedLimit = -1f;
19
20 public float getHardSpeedLimit() {
21 return hardSpeedLimit;
22 }
23
24 public void setHardSpeedLimit(float hardSpeedLimit) {
25 this.hardSpeedLimit = hardSpeedLimit;
26 }
27
29 acceleration = 1f;
30 maxSpeed = 1f;
31 smoothCap = true;
32 }
33
35 if (dest == null) {
36 this.dest.set(0, 0);
37 } else {
38 this.dest.set(dest);
39 }
40 if (destVel == null) {
41 float dir = Misc.getAngleInDegrees(loc, this.dest);
42 this.destVel = Misc.getUnitVectorAtDegreeAngle(dir);
43 this.destVel.scale(10000f);
44 } else {
45 this.destVel.set(destVel);
46 }
47 }
48
49 public void advance(float amount) {
50
51 if (amount * amount == 0 || amount <= 0) {
52 return;
53 }
54
56 float accelMult = 1f;
57// if (true && fleet != null) {
58// accelMult = 1f - Math.max(0, (fleet.getRadius() - 22f) / (100 - 22f));
59// accelMult = 0.25f + accelMult * 0.75f;
60// }
61// if (fleet != null && fleet.isPlayerFleet()) {
62// System.out.println("fwefwefe");
63// }
64
65 float speed = vel.length();
66 float acc = acceleration;
67// if (speed > effectiveMaxSpeed) {
68// acc = Math.max(speed, 200f) + fleet.getAcceleration();
69// }
70
71 float effectiveAccel = acc * accelMult;
72
73 if (effectiveAccel <= 0f) {
74 accel.set(0, 0);
75 return;
76 }
77 //amount *= 20f;
78
79 Vector2f toDest = Vector2f.sub(dest, loc, new Vector2f());
81
82 //toDest.scale(3f);
83
84 //Vector2f dir = Vector2f.sub(dest, loc, new Vector2f());
85 //Utils.normalise(dir);
86
87 // positive means away from loc
88 //float relativeSpeed = Vector2f.dot(dir, destVel);
89 //float dist = Utils.getDistance(loc, dest);
90// if (delegate == CampaignEngine.getInstance().getPlayerFleet()) {
91// System.out.println("23rsd1sdfew");
92// }
93 float timeToMatchVel = velDiff.length() / effectiveAccel;
94 //toDest.scale(timeToMatchVel + 2f);
95 velDiff.scale(timeToMatchVel + 0.75f);
96 //velDiff.scale(0.5f * timeToMatchVel);
97
98// if (relativeSpeed > 0) {
99// velDiff.scale(dist / relativeSpeed);
100// } else {
101// velDiff.scale(timeToMatchVel);
102// }
103
104 // this is the vector we want to negate (reduce to zero) in order to match
105 // location and speed. Units are distance.
106 Vector2f negate = (Vector2f) Vector2f.add(toDest, velDiff, new Vector2f()).negate();
107 //if (negate.lengthSquared() < 0.25f) negate.set(0, 0);
108
109// if (delegate == CampaignEngine.getInstance().getPlayerFleet()) {
110// System.out.println(toDest);
111// }
112
113 float maxAccel = negate.length() / (amount * amount);
115 //maxAccel = 280f;
116 //maxAccel = acceleration;
117 if (maxAccel > 0) {
118 accel = (Vector2f) Misc.normalise(negate).scale(-maxAccel);
119 } else {
120 accel = (Vector2f) negate.negate();
121 }
122// accel = (Vector2f) Utils.normalise(negate).scale(-maxAccel);
123// if (delegate == CampaignEngine.getInstance().getPlayerFleet()) {
124// System.out.println("Max: " + maxAccel);
125// }
126
127// float speedPre = vel.length();
128// if (delegate == CampaignEngine.getInstance().getPlayerFleet()) {
129// System.out.println("Speed pre: " + vel.length());
130// }
131 vel.x += accel.x * amount;
132 vel.y += accel.y * amount;
133
134 speed = vel.length();
135// float speedPost = vel.length();
136// if (delegate == CampaignEngine.getInstance().getPlayerFleet()) {
137// System.out.println("Speed post: " + vel.length());
138// System.out.println("");
139// }
140// if (delegate == CampaignEngine.getInstance().getPlayerFleet()) {
141// if (speedPre < speedPost) {
142// System.out.println(accel);
143// }
144// }
145
146 //float speed = vel.length();
147 if (speed >= effectiveMaxSpeed && speed > 0) {
148 if (smoothCap) {
149// if (delegate == CampaignEngine.getInstance().getPlayerFleet()) {
150// System.out.println("23refwef");
151// }
152 Vector2f cap = new Vector2f(vel);
153 cap.negate();
155 //float mag = 1f;
156 //if (maxAccel * 2f > 1) {
157 //mag = maxAccel * 2f;
158 //}
159 //if (mag < 1000) mag = 1000;
160 float mag = speed - effectiveMaxSpeed;
161 if (mag < 50f) mag = 50f;
162
163 float minMag = maxAccel * 2f;
164 if (mag < minMag) mag = minMag;
165
166 if (mag * amount > (speed - effectiveMaxSpeed) && amount > 0) {
167 mag = (speed - effectiveMaxSpeed) / amount;
168 }
169 cap.scale(mag);
170 vel.x += cap.x * amount;
171 vel.y += cap.y * amount;
172 } else {
173 vel.scale(effectiveMaxSpeed / speed);
174 }
175 }
176
177 if (hardSpeedLimit >= 0 && speed > 0) {
178 vel.scale(hardSpeedLimit / speed);
179 }
180
181 loc.x += vel.x * amount;
182 loc.y += vel.y * amount;
183
184 }
185
186
188 return accel;
189 }
190
191 public float getAcceleration() {
192 return acceleration;
193 }
194
195 public void setAcceleration(float acceleration) {
196 this.acceleration = acceleration;
197 }
198
199 public float getMaxSpeed() {
200 return maxSpeed;
201 }
202
203 public void setMaxSpeed(float maxSpeed) {
204 this.maxSpeed = maxSpeed;
205 }
206
208 return vel;
209 }
210
212 return loc;
213 }
214
215 public Vector2f getDest() {
216 return dest;
217 }
218
219}
220
221
222
223
static Vector2f getUnitVectorAtDegreeAngle(float degrees)
Definition Misc.java:1189
static float getAngleInDegrees(Vector2f v)
Definition Misc.java:1119
static Vector2f normalise(Vector2f v)
Definition Misc.java:1127
void setDest(Vector2f dest, Vector2f destVel)