forked from Hutouben/FTC-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlookuptrajectory.java
More file actions
172 lines (139 loc) · 6.01 KB
/
lookuptrajectory.java
File metadata and controls
172 lines (139 loc) · 6.01 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.util.HashMap;
import java.util.Map;
public class ProjectileTrajectoryLookup {
// Lookup table structure: Key = encoded params, Value = trajectory data
private Map<String, TrajectoryData> lookupTable;
// Physical constants
private static final double GRAVITY = 9.81; // m/s^2
public ProjectileTrajectoryLookup() {
lookupTable = new HashMap<>();
buildLookupTable();
}
// Encode projectile parameters into a lookup key
private String encodeKey(double velocity, double angle, double height) {
// Round values to reduce table size (adjust precision as needed)
int v = (int) Math.round(velocity);
int a = (int) Math.round(angle);
int h = (int) Math.round(height * 10); // 0.1m precision
return String.format("%d_%d_%d", v, a, h);
}
// Decode key back to parameters
public ProjectileParams decodeKey(String key) {
String[] parts = key.split("_");
if (parts.length != 3) {
throw new IllegalArgumentException("Invalid key format");
}
double velocity = Double.parseDouble(parts[0]);
double angle = Double.parseDouble(parts[1]);
double height = Double.parseDouble(parts[2]) / 10.0;
return new ProjectileParams(velocity, angle, height);
}
// Build the lookup table with pre-calculated trajectories
private void buildLookupTable() {
// Example: Build table for common projectile parameters
for (int velocity = 10; velocity <= 50; velocity += 5) {
for (int angle = 15; angle <= 75; angle += 5) {
for (int height = 0; height <= 20; height += 5) {
String key = encodeKey(velocity, angle, height);
TrajectoryData data = calculateTrajectory(velocity, angle, height);
lookupTable.put(key, data);
}
}
}
System.out.println("Lookup table built with " + lookupTable.size() + " entries");
}
// Calculate trajectory physics
private TrajectoryData calculateTrajectory(double v0, double angleDeg, double h0) {
double angleRad = Math.toRadians(angleDeg);
double vx = v0 * Math.cos(angleRad);
double vy = v0 * Math.sin(angleRad);
// Time of flight: solve -0.5*g*t^2 + vy*t + h0 = 0
double discriminant = vy * vy + 2 * GRAVITY * h0;
double timeOfFlight = (vy + Math.sqrt(discriminant)) / GRAVITY;
// Maximum height
double maxHeight = h0 + (vy * vy) / (2 * GRAVITY);
// Range
double range = vx * timeOfFlight;
// Time to max height
double timeToMaxHeight = vy / GRAVITY;
return new TrajectoryData(range, maxHeight, timeOfFlight, timeToMaxHeight);
}
// Lookup trajectory data
public TrajectoryData lookup(double velocity, double angle, double height) {
String key = encodeKey(velocity, angle, height);
TrajectoryData data = lookupTable.get(key);
if (data == null) {
// Fallback: calculate on-the-fly if not in table
System.out.println("Key not found, calculating: " + key);
return calculateTrajectory(velocity, angle, height);
}
return data;
}
// Get position at time t
public Position getPositionAt(double velocity, double angle, double height, double t) {
double angleRad = Math.toRadians(angle);
double vx = velocity * Math.cos(angleRad);
double vy = velocity * Math.sin(angleRad);
double x = vx * t;
double y = height + vy * t - 0.5 * GRAVITY * t * t;
return new Position(x, y);
}
// Data classes
public static class TrajectoryData {
public final double range;
public final double maxHeight;
public final double timeOfFlight;
public final double timeToMaxHeight;
public TrajectoryData(double range, double maxHeight, double timeOfFlight, double timeToMaxHeight) {
this.range = range;
this.maxHeight = maxHeight;
this.timeOfFlight = timeOfFlight;
this.timeToMaxHeight = timeToMaxHeight;
}
@Override
public String toString() {
return String.format("Range: %.2fm, Max Height: %.2fm, Time: %.2fs",
range, maxHeight, timeOfFlight);
}
}
public static class ProjectileParams {
public final double velocity;
public final double angle;
public final double height;
public ProjectileParams(double velocity, double angle, double height) {
this.velocity = velocity;
this.angle = angle;
this.height = height;
}
}
public static class Position {
public final double x;
public final double y;
public Position(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return String.format("(%.2f, %.2f)", x, y);
}
}
// Example usage
public static void main(String[] args) {
ProjectileTrajectoryLookup lookup = new ProjectileTrajectoryLookup();
// Lookup trajectory
TrajectoryData data = lookup.lookup(30, 45, 0);
System.out.println("Trajectory for v=30m/s, angle=45°, h=0m:");
System.out.println(data);
// Decode a key
String key = "30_45_0";
ProjectileParams params = lookup.decodeKey(key);
System.out.println("\nDecoded key '" + key + "':");
System.out.println("Velocity: " + params.velocity + " m/s");
System.out.println("Angle: " + params.angle + "°");
System.out.println("Height: " + params.height + " m");
// Get position at specific time
Position pos = lookup.getPositionAt(30, 45, 0, 2.0);
System.out.println("\nPosition at t=2s: " + pos);
}
}