-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from VirxEC/rocketsim
Incorporate accuracy improvements from RocketSim
- Loading branch information
Showing
45 changed files
with
1,264 additions
and
380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
.vscode/ | ||
/.vscode | ||
/target | ||
/venv | ||
ball_prediction.json | ||
Cargo.lock | ||
*.svg | ||
perf* | ||
*.dump | ||
*.json | ||
/collision_meshes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
# run `cargo r --example accuracy --features=serde` first! | ||
|
||
with open(Path(__file__).parent / "accuracy.json") as f: | ||
data = json.load(f) | ||
|
||
rocketsim = [[], [], [], []] | ||
rl_ball_sym = [[], [], [], []] | ||
|
||
for ball in data["rocketsim"]: | ||
rocketsim[0].append(ball["time"]) | ||
rocketsim[1].append(ball["location"]) | ||
rocketsim[2].append(ball["velocity"]) | ||
rocketsim[3].append(ball["angular_velocity"]) | ||
|
||
for ball in data["rl_ball_sym"]: | ||
rl_ball_sym[0].append(ball["time"]) | ||
rl_ball_sym[1].append(ball["location"]) | ||
rl_ball_sym[2].append(ball["velocity"]) | ||
rl_ball_sym[3].append(ball["angular_velocity"]) | ||
|
||
start_time = 0 * 120 | ||
# time_limit = 250 | ||
time_limit = len(rocketsim[0]) | ||
|
||
# start_time = 7 * 120 | ||
# time_limit = 8 * 120 | ||
|
||
location_diff = [] | ||
|
||
for i in range(start_time, time_limit): | ||
location_diff.append( | ||
( | ||
(rocketsim[1][i][0] - rl_ball_sym[1][i][0]) ** 2 | ||
+ (rocketsim[1][i][1] - rl_ball_sym[1][i][1]) ** 2 | ||
+ (rocketsim[1][i][2] - rl_ball_sym[1][i][2]) ** 2 | ||
) | ||
** 0.5 | ||
) | ||
|
||
if location_diff[-1] > 0: | ||
print(rl_ball_sym[0][i]) | ||
|
||
velocity_diff = [] | ||
|
||
for i in range(start_time, time_limit): | ||
velocity_diff.append( | ||
( | ||
(rocketsim[2][i][0] - rl_ball_sym[2][i][0]) ** 2 | ||
+ (rocketsim[2][i][1] - rl_ball_sym[2][i][1]) ** 2 | ||
+ (rocketsim[2][i][2] - rl_ball_sym[2][i][2]) ** 2 | ||
) | ||
** 0.5 | ||
) | ||
|
||
angular_velocity_diff = [] | ||
|
||
for i in range(start_time, time_limit): | ||
angular_velocity_diff.append( | ||
( | ||
(rocketsim[3][i][0] - rl_ball_sym[3][i][0]) ** 2 | ||
+ (rocketsim[3][i][1] - rl_ball_sym[3][i][1]) ** 2 | ||
+ (rocketsim[3][i][2] - rl_ball_sym[3][i][2]) ** 2 | ||
) | ||
** 0.5 | ||
) | ||
|
||
ax = plt.figure().add_subplot() | ||
|
||
ax.plot(rocketsim[0][start_time:time_limit], location_diff, label="location") | ||
ax.plot(rocketsim[0][start_time:time_limit], velocity_diff, label="velocity") | ||
ax.plot(rocketsim[0][start_time:time_limit], angular_velocity_diff, label="angular velocity") | ||
|
||
ax.legend() | ||
ax.set_title("Time vs Difference") | ||
ax.set_xlabel("Time") | ||
ax.set_ylabel("Magnitude of difference") | ||
|
||
plt.show() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import json | ||
import matplotlib.pyplot as plt | ||
|
||
with open("accuracy.json") as f: | ||
data = json.load(f) | ||
|
||
rocketsim = [[], [], []] | ||
rl_ball_sym = [[], [], []] | ||
|
||
for ball in data["rocketsim"]: | ||
rocketsim[0].append(ball["angular_velocity"][0]) | ||
rocketsim[1].append(ball["angular_velocity"][1]) | ||
rocketsim[2].append(ball["angular_velocity"][2]) | ||
|
||
for ball in data["rl_ball_sym"]: | ||
rl_ball_sym[0].append(ball["angular_velocity"][0]) | ||
rl_ball_sym[1].append(ball["angular_velocity"][1]) | ||
rl_ball_sym[2].append(ball["angular_velocity"][2]) | ||
|
||
ax = plt.figure().add_subplot(projection='3d') | ||
|
||
ax.plot(rocketsim[0], rocketsim[1], rocketsim[2], label="rocketsim") | ||
ax.plot(rl_ball_sym[0], rl_ball_sym[1], rl_ball_sym[2], label="rl_ball_sym") | ||
|
||
ax.legend() | ||
ax.set_title("Angular Velocity") | ||
ax.set_xlabel("X") | ||
ax.set_ylabel("Z") | ||
ax.set_zlabel("Y") | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import json | ||
import matplotlib.pyplot as plt | ||
|
||
with open("accuracy.json") as f: | ||
data = json.load(f) | ||
|
||
rocketsim = [[], [], []] | ||
rl_ball_sym = [[], [], []] | ||
|
||
for ball in data["rocketsim"]: | ||
rocketsim[0].append(ball["location"][0]) | ||
rocketsim[1].append(ball["location"][1]) | ||
rocketsim[2].append(ball["location"][2]) | ||
|
||
for ball in data["rl_ball_sym"]: | ||
rl_ball_sym[0].append(ball["location"][0]) | ||
rl_ball_sym[1].append(ball["location"][1]) | ||
rl_ball_sym[2].append(ball["location"][2]) | ||
|
||
ax = plt.figure().add_subplot(projection='3d') | ||
|
||
ax.plot(rocketsim[0], rocketsim[1], rocketsim[2], label="rocketsim") | ||
ax.plot(rl_ball_sym[0], rl_ball_sym[1], rl_ball_sym[2], label="rl_ball_sym") | ||
|
||
ax.legend() | ||
ax.set_title("Location") | ||
ax.set_xlabel("X") | ||
ax.set_ylabel("Z") | ||
ax.set_zlabel("Y") | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import json | ||
import matplotlib.pyplot as plt | ||
|
||
with open("accuracy.json") as f: | ||
data = json.load(f) | ||
|
||
rocketsim = [[], [], []] | ||
rl_ball_sym = [[], [], []] | ||
|
||
for ball in data["rocketsim"]: | ||
rocketsim[0].append(ball["velocity"][0]) | ||
rocketsim[1].append(ball["velocity"][1]) | ||
rocketsim[2].append(ball["velocity"][2]) | ||
|
||
for ball in data["rl_ball_sym"]: | ||
rl_ball_sym[0].append(ball["velocity"][0]) | ||
rl_ball_sym[1].append(ball["velocity"][1]) | ||
rl_ball_sym[2].append(ball["velocity"][2]) | ||
|
||
ax = plt.figure().add_subplot(projection='3d') | ||
|
||
ax.plot(rocketsim[0], rocketsim[1], rocketsim[2], label="rocketsim") | ||
ax.plot(rl_ball_sym[0], rl_ball_sym[1], rl_ball_sym[2], label="rl_ball_sym") | ||
|
||
ax.legend() | ||
ax.set_title("Velocity") | ||
ax.set_xlabel("X") | ||
ax.set_ylabel("Z") | ||
ax.set_zlabel("Y") | ||
|
||
plt.show() |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use byteorder::{LittleEndian, ReadBytesExt}; | ||
use colored::Colorize; | ||
use rl_ball_sym::{load_standard, Ball}; | ||
use std::{fs, io}; | ||
|
||
fn read_balls(file_name: &str, mut ball: Ball) -> io::Result<Vec<Ball>> { | ||
let mut file = fs::File::open(file_name)?; | ||
let num_balls = file.read_u16::<LittleEndian>()? as usize; | ||
|
||
let mut balls = Vec::with_capacity(num_balls); | ||
|
||
for _ in 0..num_balls { | ||
ball.time = file.read_f32::<LittleEndian>()?; | ||
ball.location.x = file.read_f32::<LittleEndian>()?; | ||
ball.location.y = file.read_f32::<LittleEndian>()?; | ||
ball.location.z = file.read_f32::<LittleEndian>()?; | ||
ball.velocity.x = file.read_f32::<LittleEndian>()?; | ||
ball.velocity.y = file.read_f32::<LittleEndian>()?; | ||
ball.velocity.z = file.read_f32::<LittleEndian>()?; | ||
ball.angular_velocity.x = file.read_f32::<LittleEndian>()?; | ||
ball.angular_velocity.y = file.read_f32::<LittleEndian>()?; | ||
ball.angular_velocity.z = file.read_f32::<LittleEndian>()?; | ||
|
||
balls.push(ball); | ||
} | ||
|
||
Ok(balls) | ||
} | ||
|
||
#[derive(serde::Serialize)] | ||
struct BallDump { | ||
rocketsim: Vec<Ball>, | ||
rl_ball_sym: Vec<Ball>, | ||
} | ||
|
||
fn main() -> io::Result<()> { | ||
let (game, mut ball) = load_standard(); | ||
|
||
let rocketsim = read_balls("examples/ball.dump", ball)?; | ||
|
||
println!("Starting config:"); | ||
println!("Location: {}", rocketsim[0].location); | ||
println!("Velocity: {}", rocketsim[0].velocity); | ||
println!("Angular Velocity: {}", rocketsim[0].angular_velocity); | ||
|
||
println!(); | ||
|
||
let mut rl_ball_sym = Vec::with_capacity(rocketsim.len()); | ||
rl_ball_sym.push(rocketsim[0]); | ||
|
||
ball = rocketsim[0]; | ||
for cball in rocketsim.iter().copied().take(5) { | ||
print_error(ball, cball); | ||
|
||
println!("{}", "[START OF TICK]".green()); | ||
ball = cball; | ||
ball.step(&game, 1. / 120.); | ||
rl_ball_sym.push(ball); | ||
println!("{}", "[END OF TICK]".red()); | ||
} | ||
|
||
fs::write( | ||
"analysis/accuracy.json", | ||
serde_json::to_string(&BallDump { rocketsim, rl_ball_sym }).unwrap(), | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn print_error(ball: Ball, cball: Ball) { | ||
println!("{} | {}", ball.location / 50., cball.location / 50.); | ||
println!("{} | {}", ball.velocity / 50., cball.velocity / 50.); | ||
println!( | ||
"Error: {}, {}, {}", | ||
ball.location.distance(cball.location), | ||
ball.velocity.distance(cball.velocity), | ||
ball.angular_velocity.distance(cball.angular_velocity) | ||
); | ||
println!("Vel error: {}", ball.velocity - cball.velocity); | ||
} |
Oops, something went wrong.