Skip to content

Commit

Permalink
Merge pull request #16 from ben9583/0.6.0
Browse files Browse the repository at this point in the history
Release 0.6.0
  • Loading branch information
ben9583 committed Jul 27, 2022
2 parents 4bc3770 + 7e0c163 commit 64fde30
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "solar-sim"
version = "0.5.1"
version = "0.6.0"
authors = ["Ben Plate <bplate9583@gmail.com>"]
edition = "2018"
description = "Physics simulator written in Rust WASM for use in Solar Sim website"
Expand Down
13 changes: 13 additions & 0 deletions rust-src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ pub fn get_positions() -> Array {
return out;
}

#[wasm_bindgen]
pub fn get_velocities() -> Array {
let uni = UNIVERSE.read().unwrap();
let out: Array = Array::new();

for i in 0..uni.len() {
out.push(&JsValue::from_f64(uni[i].velocity.x));
out.push(&JsValue::from_f64(uni[i].velocity.y));
}

return out;
}

#[wasm_bindgen]
pub fn set_simulation_accuracy(time_step: f64, num_sims_per_step: i32) {
let mut ts = TIME_STEP.write().unwrap();
Expand Down
69 changes: 69 additions & 0 deletions website-src/cookiePolicy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Lagrange Demonstration - Cookie Policy</title>
<style>
table,
td {
border: 1px solid #333;
border-collapse: collapse;
padding: 5px;
}

thead,
tfoot {
background-color: #333;
color: #fff;
text-align: center;
}

</style>
</head>

<body>
<div style="max-width: 960px">
<h1>Cookie Policy</h1>
<p>Last updated: 24 June 2022</p>
<p>
In order to provide a better user experience, this website uses cookies to save data across browsing sessions.
Your use of this website does <b>not</b> imply an acceptance of these cookies and you must manually accept them by checking the box at the top of the landing page.
In doing so, you agree to the complete use of cookies by this website according to this policy as permitted by law.
</p>
<p>
Although this website uses cookies, it does not track personal data that can be used for identification.
The website also does not use any third-party cookies, including tracking technologies such as Google Analytics.
Your cookies will not be sold or provided to any other third parties except where required by law.
</p>
<p>
Should you choose not to accept the use of cookies, you will still be able to use this website with the understanding that some functionality may be limited.
</p>
<p>
A list of all cookies used and their purpose can be found below:
</p>
<table>
<thead>
<tr>
<td>Cookie</td>
<td>Information</td>
<td>Reason</td>
</tr>
</thead>
<tbody>
<tr>
<td>cookiesAccepted</td>
<td>Whether you've accepted cookies on this device.</td>
<td>If cookies are enabled, you won't need to enable them again when you revisit the website.</td>
</tr>
<tr>
<td>configSaves</td>
<td>User-saved configurations of SolarSim</td>
<td>Allows users to save SolarSim configurations across browsing sessions. Without this, users can still save and load configurations, but they will be lost when the webpage is closed.</td>
</tr>
</tbody>
</table>
<br />
<a href="./">Return to SolarSim</a>
</div>
</body>
</html>
41 changes: 40 additions & 1 deletion website-src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@
</head>

<body>
<div id="cookieNotice">
<input type="checkbox" id="cookieAcceptance" /><span style="color:white">This website optionally uses cookies. A comprehensive policy can be viewed <a href="cookiePolicy.html">here</a> (updated 24 June 2022). By checking this box, you give permission for using these cookies; you can revoke permissions at any time by unchecking this box.</span>
<script>
if(document.cookie.length > 0) document.getElementById("cookieAcceptance").checked = true;

document.getElementById("cookieAcceptance").addEventListener("change", function() {
if (this.checked) {
document.cookie = "cookiesAccepted=true;max-age=315360000;SameSite=Strict";
} else {
document.cookie = "cookiesAccepted=;max-age=0;SameSite=Strict";
}
});
</script>
<p></p>
</div>
<button id="toggle">Pause</button>
<button id="reset">Reset</button>
<input type="checkbox" id="debug" /><span style="color:white">Enable Debug (slightly lowers performance)</span>
<input type="checkbox" id="debug" /><span style="color:white">Enable Debug (May need to enable precise timers in browser)</span>
<div id="debugSection" style="color:white;display:inline;margin-left:10px">
<span style="margin-right:10px"># of bodies: <span id="numBodies">6</span></span>
<span style="margin-right:10px">Simulation tick time: <span id="simTickTime">10</span><span>μs</span></span>
Expand Down Expand Up @@ -45,6 +60,30 @@
<input id="preciseVelocityY" type="number" placeholder="Velocity: Y" />
<button id="preciseSpawn">Spawn</button>
</div>
</div><br />

<div style="color: white">
<input id="saveName" type="text" placeholder="Save Name" />

<button id="saveButton">Save</button>
<button id="loadButton">Load</button>
<button id="clearButton">Clear</button>
<span id="saveMessage"></span><br />
<div style="margin-top:5px"><span>Saves list: </span><button id="showSavesList">Show</button></div>
<p id="savesList" style="display:none"></p>
<script>
document.getElementById("showSavesList").addEventListener("click", function() {
let savesList = document.getElementById("savesList");
let savesListButton = document.getElementById("showSavesList");
if (savesList.style.display == "none") {
savesList.style.display = "block";
savesListButton.innerHTML = "Hide";
} else {
savesList.style.display = "none";
savesListButton.innerHTML = "Show";
}
});
</script>
</div>

<div style="color:white;">
Expand Down
114 changes: 104 additions & 10 deletions website-src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,63 @@ import * as SolarSim from "solar-sim";
const WIDTH = 1280;
const HEIGHT = 720;

let cookesAccepted = () => document.cookie.length > 0;

let bodies = [
{
name: "Sun",
mass: 5e12,
radius: 32,
position: [WIDTH / 2, HEIGHT / 2],
initialVelocity: [0, 0],
velocity: [0, 0],
color: "#c8c800",
},
{
name: "Earth",
mass: 1e10,
radius: 10,
position: [WIDTH / 2 + 160, HEIGHT / 2],
initialVelocity: [0, 1.444169311403618],
velocity: [0, 1.444169311403618],
color: "#0064c8",
},
{
name: "L1",
mass: 1,
radius: 4,
position: [WIDTH / 2 + 174.3684756886812, HEIGHT / 2],
initialVelocity: [0, 1.57386005],
velocity: [0, 1.57386005],
color: "#969696",
},
{
name: "L2",
mass: 1,
radius: 4,
position: [WIDTH / 2 + 146.4437229236931, HEIGHT / 2],
initialVelocity: [0, 1.321809565],
velocity: [0, 1.321809565],
color: "#969696",
},
{
name: "L3",
mass: 1,
radius: 4,
position: [WIDTH / 2 - 159.813705852, HEIGHT / 2],
initialVelocity: [0, -1.444169311403618],
velocity: [0, -1.444169311403618],
color: "#4b964b",
},
{
name: "L4",
mass: 1,
radius: 4,
position: [WIDTH / 2 + 80, HEIGHT / 2 + (Math.sqrt(3)/2) * 160],
initialVelocity: [-1.25180591705918, 0.7227304831872841],
velocity: [-1.25180591705918, 0.7227304831872841],
color: "#4b964b",
},
{
name: "L5",
mass: 1,
radius: 4,
position: [WIDTH / 2 + 80, HEIGHT / 2 - (Math.sqrt(3)/2) * 160],
initialVelocity: [1.25180591705918, 0.7227304831872841],
velocity: [1.25180591705918, 0.7227304831872841],
color: "#4b964b",
},
];
Expand All @@ -74,7 +76,7 @@ let trails = [

for(let i = 0; i < bodies.length; i++) {
let body = bodies[i];
SolarSim.add_body(body.mass, body.position[0], body.position[1], body.initialVelocity[0], body.initialVelocity[1]);
SolarSim.add_body(body.mass, body.position[0], body.position[1], body.velocity[0], body.velocity[1]);
}

function addBody(name, mass, radius, positionX, positionY, velocityX, velocityY) {
Expand All @@ -94,7 +96,7 @@ function addBody(name, mass, radius, positionX, positionY, velocityX, velocityY)
mass: mass,
radius: radius,
position: [positionX, positionY],
initialVelocity: [velocityX, velocityY],
velocity: [velocityX, velocityY],
color: "#" + red + green + blue,
});

Expand Down Expand Up @@ -278,14 +280,17 @@ function step(simulate) {

if(debug && count % 10 == 0) tickTime = performance.now();

const newPositions = SolarSim.get_positions();
let newPositions = SolarSim.get_positions();
let newVelocities = SolarSim.get_velocities();
for(let i = 0; i < bodies.length; i++) {
let body = bodies[i];
let inBounds = (newPositions[i * 2] >= 0 && newPositions[i * 2] < WIDTH && newPositions[i * 2 + 1] >= 0 && newPositions[i * 2 + 1] < HEIGHT);
let inSimBounds = (newPositions[i * 2] >= -WIDTH && newPositions[i * 2] < 2 * WIDTH && newPositions[i * 2 + 1] >= -HEIGHT && newPositions[i * 2 + 1] < 2 * HEIGHT)

body.position[0] = newPositions[i * 2];
body.position[1] = newPositions[i * 2 + 1];
body.velocity[0] = newVelocities[i * 2];
body.velocity[1] = newVelocities[i * 2 + 1];

if(!inSimBounds) {
ctx2.fillStyle = "black";
Expand All @@ -294,6 +299,7 @@ function step(simulate) {
}
bodies.splice(i, 1);
trails.splice(i, 1);
newPositions.splice(i * 2, 2);
SolarSim.remove_body(i);

i--;
Expand Down Expand Up @@ -366,3 +372,91 @@ spawnButton.addEventListener("click", (elem, e) => {
})

window.requestAnimationFrame(step);

let saves = {};

let saveNameField = document.getElementById("saveName");
let saveMessageField = document.getElementById("saveMessage");
let savesList = document.getElementById("savesList");
if(cookesAccepted()) {
let saveString = document.cookie.split("; ").find(cookie => cookie.startsWith("configSaves="));
if(saveString) {
saves = JSON.parse(atob(saveString.split("=")[1]));
}

savesList.innerHTML = "";
for(let i = 0; i < Object.keys(saves).length; i++) {
savesList.innerHTML += Object.keys(saves)[i] + "<br />";
}
}
document.getElementById("cookieAcceptance").addEventListener("click", (elem, e) => {
if(document.getElementById("cookieAcceptance").checked) {
document.cookie = "configSaves=" + btoa(JSON.stringify(saves)) + ";max-age=315360000;SameSite=Strict";
} else {
document.cookie = "configSaves=;max-age=0;SameSite=Strict";
}
});

let saveButton = document.getElementById("saveButton");
saveButton.addEventListener("click", (elem, e) => {
if(saveNameField.value.length == 0) return;

saves[saveNameField.value] = structuredClone(bodies);
if(cookesAccepted()) {
document.cookie = "configSaves=" + btoa(JSON.stringify(saves)) + ";max-age=315360000;SameSite=Strict";
}

savesList.innerHTML = "";
for(let i = 0; i < Object.keys(saves).length; i++) {
savesList.innerHTML += Object.keys(saves)[i] + "<br />";
}
saveMessageField.innerHTML = "Saved as " + saveNameField.value;
setTimeout(() => saveMessageField.innerHTML = "", 3000);
});

let loadButton = document.getElementById("loadButton");
loadButton.addEventListener("click", (elem, e) => {
let saveName = document.getElementById("saveName").value;
console.log(saves);
if(saveName in saves) {

ctx.clearRect(0, 0, WIDTH, HEIGHT);
ctx2.clearRect(0, 0, WIDTH, HEIGHT);

bodies = structuredClone(saves[saveName]);

trails = []
for(let i = 0; i < bodies.length; i++) {
trails.push([]);
}

SolarSim.clear_universe();
for(let i = 0; i < bodies.length; i++) {
SolarSim.add_body(bodies[i].mass, bodies[i].position[0], bodies[i].position[1], bodies[i].velocity[0], bodies[i].velocity[1]);
}

step(false);

saveMessageField.innerHTML = "Loaded " + saveName;
} else {
saveMessageField.innerHTML = "No save with that name";
}

setTimeout(() => saveMessageField.innerHTML = "", 3000);
});

let clearButton = document.getElementById("clearButton");
clearButton.addEventListener("click", (elem, e) => {
if(clearButton.innerHTML === "Are you sure?") {
saves = {};
if(cookesAccepted()) {
document.cookie = "configSaves=;max-age=0;SameSite=Strict";
}

saveMessageField.innerHTML = "Cleared saves";
savesList.innerHTML = "";
} else {
clearButton.innerHTML = "Are you sure?";
setTimeout(() => clearButton.innerHTML = "Clear", 3000);
}
});
2 changes: 1 addition & 1 deletion website-src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion website-src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solar-sim-app",
"version": "0.5.1",
"version": "0.6.0",
"description": "Creates the Solar Sim website.",
"main": "index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions website-src/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
new CopyPlugin({
patterns: [
"index.html",
"cookiePolicy.html",
{from: "static", to: "static"}
]
})
Expand Down

0 comments on commit 64fde30

Please sign in to comment.