Skip to content

Commit 3489360

Browse files
committedOct 16, 2024
Initial commit
0 parents  commit 3489360

10 files changed

+4718
-0
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
/node_modules

‎Cargo.lock

+145
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "physicium"
3+
version = "0.1.0"
4+
authors = ["Adrien de Joux <adrien@d4x.fr>"]
5+
edition = "2021"
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
wasm-bindgen = "0.2"
12+
js-sys = "0.3"
13+
14+
[dependencies.web-sys]
15+
version = "0.3"
16+
features = ['CanvasRenderingContext2d', 'Document', 'Element', 'HtmlCanvasElement', 'Window']

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Physicium
2+
A physic engine on the web build with wasm in rust.

‎index.html

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
4+
</head>
5+
<body>
6+
<canvas id="canvas" height="800" width="800"></canvas>
7+
</body>
8+
</html>

‎index.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// For more comments about what's going on here, check out the `hello_world`
2+
// example.
3+
import('./pkg')
4+
.then(wasm => {
5+
const canvas = document.getElementById('canvas');
6+
const ctx = canvas.getContext('2d');
7+
8+
let world = wasm.World.new();
9+
world.add_object(100., 20., 0., 0., 20., 40.);
10+
world.add_object(20., 40., 0., 0., 30., 40.);
11+
12+
let view = wasm.WorldView.new(800, 800)
13+
function loop() {
14+
world.apply_physic(16);
15+
view.draw(world, ctx);
16+
requestAnimationFrame(loop);
17+
}
18+
loop();
19+
20+
})
21+
.catch(console.error);

‎package-lock.json

+4,367
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"scripts": {
3+
"build": "webpack",
4+
"serve": "webpack serve"
5+
},
6+
"devDependencies": {
7+
"@wasm-tool/wasm-pack-plugin": "1.5.0",
8+
"html-webpack-plugin": "^5.6.0",
9+
"webpack": "^5.93.0",
10+
"webpack-cli": "^5.1.4",
11+
"webpack-dev-server": "^5.0.4"
12+
}
13+
}

‎src/lib.rs

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
use std::f64;
2+
use wasm_bindgen::prelude::*;
3+
use web_sys::CanvasRenderingContext2d;
4+
5+
const STD_GRAVITY: f32 = 9.8;
6+
const STD_METER_SIZE: f32 = 50.0;
7+
8+
#[derive(Clone)]
9+
pub struct Object {
10+
pos_x: f32,
11+
pos_y: f32,
12+
velocity_x: f32,
13+
velocity_y: f32,
14+
radius: f32,
15+
mass: f32,
16+
}
17+
18+
19+
#[wasm_bindgen]
20+
pub struct World {
21+
gravity_x: f32,
22+
gravity_y: f32,
23+
meter_size: f32,
24+
objects: Vec<Object>
25+
}
26+
27+
#[wasm_bindgen]
28+
pub struct WorldView {
29+
canvas_width: u32,
30+
canvas_height: u32,
31+
center: (f32, f32),
32+
scale: f32
33+
}
34+
35+
/// Public methods, exported to JavaScript.
36+
#[wasm_bindgen]
37+
impl World {
38+
pub fn new() -> World {
39+
World{
40+
gravity_x: 0.0,
41+
gravity_y: STD_GRAVITY,
42+
meter_size: STD_METER_SIZE,
43+
objects: Vec::new()
44+
}
45+
}
46+
47+
pub fn set_gravity_x(&mut self, gravity: f32) {
48+
self.gravity_x = gravity;
49+
}
50+
51+
pub fn set_gravity_y(&mut self, gravity: f32) {
52+
self.gravity_y = gravity;
53+
}
54+
55+
pub fn set_meter_size(&mut self, size: f32) {
56+
self.meter_size = size;
57+
}
58+
59+
pub fn add_object(
60+
&mut self, pos_x: f32, pos_y: f32,
61+
velocity_x: f32, velocity_y: f32,
62+
radius: f32, mass: f32
63+
) {
64+
self.objects.push(
65+
Object{pos_x, pos_y, velocity_x, velocity_y, radius, mass});
66+
}
67+
68+
pub fn get_nb_object(&self) -> usize {
69+
self.objects.len()
70+
}
71+
72+
pub fn apply_physic(&mut self, elapsed_time_ms: u32) {
73+
let elapsed_time: f32 = elapsed_time_ms as f32 / 1000.0;
74+
for obj in self.objects.iter_mut() {
75+
obj.velocity_x += self.gravity_x * elapsed_time * self.meter_size;
76+
obj.velocity_y += self.gravity_y * elapsed_time * self.meter_size;
77+
}
78+
for obj in self.objects.iter_mut() {
79+
obj.pos_x += obj.velocity_x * elapsed_time;
80+
obj.pos_y += obj.velocity_y * elapsed_time;
81+
}
82+
}
83+
}
84+
85+
86+
#[wasm_bindgen]
87+
impl WorldView {
88+
pub fn new(canvas_width: u32, canvas_height: u32) -> WorldView {
89+
WorldView{canvas_width, canvas_height, center:(0.0, 0.0), scale: 1.0 }
90+
}
91+
92+
pub fn set_view_center(&mut self, canvas_width: u32, canvas_height: u32) {
93+
self.canvas_width = canvas_width;
94+
self.canvas_height = canvas_height;
95+
}
96+
97+
pub fn set_scale(&mut self, scale: f32) {
98+
self.scale = scale;
99+
}
100+
101+
pub fn draw(&self, world: &World, ctx: CanvasRenderingContext2d) {
102+
ctx.set_fill_style(&"white".into());
103+
ctx.fill_rect(0., 0., 800., 800.);
104+
ctx.set_fill_style(&"black".into());
105+
for obj in world.objects.iter() {
106+
ctx.begin_path();
107+
ctx.arc(
108+
(obj.pos_x - self.center.0) as f64,
109+
(obj.pos_y - self.center.1) as f64,
110+
obj.radius as f64,
111+
0.0,
112+
3.1415 * 2.0
113+
);
114+
// ctx.set_fill_style(&"rgb(0,0,0)".into());
115+
ctx.fill();
116+
ctx.close_path();
117+
}
118+
}
119+
120+
}

‎webpack.config.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const path = require('path');
2+
const HtmlWebpackPlugin = require('html-webpack-plugin');
3+
const webpack = require('webpack');
4+
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
5+
6+
module.exports = {
7+
entry: './index.js',
8+
output: {
9+
path: path.resolve(__dirname, 'dist'),
10+
filename: 'index.js',
11+
},
12+
plugins: [
13+
new HtmlWebpackPlugin({
14+
template: 'index.html'
15+
}),
16+
new WasmPackPlugin({
17+
crateDirectory: path.resolve(__dirname, ".")
18+
}),
19+
],
20+
mode: 'development',
21+
experiments: {
22+
asyncWebAssembly: true
23+
}
24+
};

0 commit comments

Comments
 (0)
Please sign in to comment.