diff --git a/physics.jai b/physics.jai index b60c5d5..10823a4 100644 --- a/physics.jai +++ b/physics.jai @@ -4,6 +4,7 @@ Input :: #import "Input"; Simp :: #import "Simp"; +TIMESTEP: float = 1.0 / 60.0; window_width : s32 = 1280; window_height : s32 = 720; @@ -11,7 +12,12 @@ xy :: (x: int, y: int) -> Vector2 { return xy(cast(float)x, cast(float)y); } drawing_in_world_space: bool = false; -line :: (_from: Vector2, _to: Vector2, width: float = 1.0) { +WHITE :: Vector4.{1.0, 1.0, 1.0, 1.0}; +GREEN :: Vector4.{0.0, 1.0, 0.0, 1.0}; +drawing_color: Vector4 = WHITE; +line_thickness: float = 1.0; +line :: (_from: Vector2, _to: Vector2) { + width := line_thickness; from := _from; to := _to; if drawing_in_world_space { @@ -20,16 +26,21 @@ line :: (_from: Vector2, _to: Vector2, width: float = 1.0) { } Simp.set_shader_for_color(); normal := rotate(unit_vector(to - from), PI/2.0); - Simp.immediate_quad(from + normal*width, from - normal*width, to + normal*width, to - normal*width); + Simp.immediate_quad(from + normal*width, from - normal*width, to + normal*width, to - normal*width, color = drawing_color); } Rect :: struct { - pos, halfsize: Vector2; // pos is in world space - rotation: float; + halfsize: Vector2; + pos , vel , force : Vector2; // pos is in world space + angle, angle_vel, torque: float; }; +MASS :: 1.0; +moment_of_inertia :: (using r: Rect) -> float { + return MASS*(halfsize.y*halfsize.y + halfsize.x*halfsize.x)/12.0; +} draw_rect :: (using r: Rect) { - facing_to_right := rotate(xy(halfsize.x,0.0), rotation); - facing_to_up := rotate(xy(0.0,halfsize.y), rotation); + facing_to_right := rotate(xy(halfsize.x,0.0), angle); + facing_to_up := rotate(xy(0.0,halfsize.y), angle); upper_right := pos + facing_to_right + facing_to_up; upper_left := pos - facing_to_right + facing_to_up; @@ -75,16 +86,21 @@ main :: () { // Actual render size in pixels can be different from the window dimensions we specified above (for example on high-resolution displays on macOS/iOS). window_width, window_height = Simp.get_render_dimensions(window); + camera.pos = -xy(window_width, window_height)/2.0; + camera.zoom = 0.01; + Simp.set_render_target(window); rects: [..]Rect; - array_add(*rects, .{pos = #run xy(0.0, 0.0), halfsize = #run xy(30.0)}); - array_add(*rects, .{pos = #run xy(40.0, 0.0), halfsize = #run xy(30.0)}); + array_add(*rects, .{pos = #run xy(0.0, 0.0), halfsize = #run xy(0.3)}); + array_add(*rects, .{pos = #run xy(1.5, 0.0), halfsize = #run xy(0.3)}); + quit := false; last_time := get_time(); panning: bool = false; last_mouse_pos := mouse(); + unprocessed_time: float = 0.0; while !quit { dt := cast(float)(get_time() - last_time); last_time = get_time(); @@ -105,25 +121,52 @@ main :: () { } } - print("%\n", Input.mouse_delta_z); camera.zoom *= 1.0 - 0.1*Input.mouse_delta_z/120.0; if panning { camera.pos -= mouse_delta; - //camera.pos -= xy(Input.mouse_delta_x, -Input.mouse_delta_y); - // this doesn't fix it //Input.mouse_delta_x = 0; //Input.mouse_delta_y = 0; } - rects[0].rotation += dt * 1.0; + // process physics + unprocessed_time += dt; + { + dt: string = "do not use"; + while unprocessed_time > TIMESTEP + { + defer unprocessed_time -= TIMESTEP; + + + + + rects[0].angle += TIMESTEP * 1.0; + } + } + Simp.clear_render_target(0.0, 0.0, 0.0, 1.0); drawing_in_world_space = true; + + // draw grid + drawing_color = .{0.2, 0.2, 0.2, 0.2}; + for x: -30..30 { + line(xy(x, 30), xy(x, -30)); + } + for y: -30..30 { + line(xy(30, y), xy(-30, y)); + } + drawing_color = WHITE; + + + line_thickness = 2.0; + drawing_color = GREEN; for rects draw_rect(it); - line(xy(0.0, 0.0), screen_to_world(mouse())); + line_thickness = 1.0; + drawing_color = WHITE; + //line(xy(0.0, 0.0), screen_to_world(mouse())); drawing_in_world_space = false; Simp.swap_buffers(window);