Last active
May 3, 2024 16:36
-
-
Save karl-zylinski/5ef25b68281b899acb1dd7774f035177 to your computer and use it in GitHub Desktop.
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
// A small test program of me trying Odin + Box2D + Raylib | |
// Made during this stream: https://www.youtube.com/watch?v=LYW7jdwEnaI | |
// I used these bindings https://github.com/cr1sth0fer/odin-box2d download them and put them in a sub-folder "box2d" next to this file. | |
// Then you can build using `odin run .` | |
package game | |
import b2 "box2d" | |
import rl "vendor:raylib" | |
create_box :: proc(world_id: b2.World_ID, pos: b2.Vec2) -> b2.Body_ID{ | |
body_def := b2.default_body_def() | |
body_def.type = .Dynamic | |
body_def.position = pos | |
body_id := b2.create_body(world_id, &body_def) | |
shape_def := b2.default_shape_def() | |
shape_def.density = 1 | |
shape_def.friction = 0.3 | |
box := b2.make_box(20, 20) | |
box_def := b2.default_shape_def() | |
b2.create_polygon_shape(body_id, &box_def, &box) | |
return body_id | |
} | |
main :: proc() { | |
rl.InitWindow(1280, 720, "Box2D trial") | |
world_def := b2.default_world_def() | |
world_def.gravity = b2.Vec2{0, -1} | |
world_id := b2.create_world(&world_def) | |
defer b2.destroy_world(world_id) | |
ground := rl.Rectangle { | |
0, 600, | |
1280, 120, | |
} | |
ground_body_def := b2.default_body_def() | |
ground_body_def.position = b2.Vec2{ground.x, -ground.y-ground.height} | |
ground_body_id := b2.create_body(world_id, &ground_body_def) | |
ground_box := b2.make_box(ground.width, ground.height) | |
ground_shape_def := b2.default_shape_def() | |
b2.create_polygon_shape(ground_body_id, &ground_shape_def, &ground_box) | |
bodies: [dynamic]b2.Body_ID | |
px: f32 = 400 | |
py: f32 = -400 | |
num_per_row := 10 | |
num_in_row := 0 | |
for _ in 0..<50 { | |
b := create_box(world_id, {px, py}) | |
append(&bodies, b) | |
num_in_row += 1 | |
if num_in_row == num_per_row { | |
py += 30 | |
px = 200 | |
num_per_row -= 1 | |
num_in_row = 0 | |
} | |
px += 30 | |
} | |
body_def := b2.default_body_def() | |
body_def.type = .Dynamic | |
body_def.position = b2.Vec2{0, 4} | |
body_id := b2.create_body(world_id, &body_def) | |
shape_def := b2.default_shape_def() | |
shape_def.density = 1000 | |
shape_def.friction = 0.3 | |
circle: b2.Circle | |
circle.radius = 40 | |
b2.create_circle_shape(body_id, &shape_def, &circle) | |
time_step: f32 = 1.0 / 60 | |
sub_steps: i32 = 4 | |
for !rl.WindowShouldClose() { | |
rl.BeginDrawing() | |
rl.ClearBackground(rl.BLACK) | |
rl.DrawRectangleRec(ground, rl.RED) | |
mouse_pos := rl.GetMousePosition() | |
b2.body_set_transform(body_id, {mouse_pos.x, -mouse_pos.y}, 0) | |
b2.world_step(world_id, time_step, sub_steps) | |
for b in bodies { | |
position := b2.body_get_position(b) | |
a := b2.body_get_angle(b) | |
rl.DrawRectanglePro({position.x, -position.y, 40, 40}, {20, 20}, a*(180/3.14), rl.YELLOW) | |
// I changed the line above after the stream, it was originally like this. The line above puts the origin at the correct place and uses the correct type of rotation angle: | |
// rl.DrawRectanglePro({position.x, -position.y, 40, 40}, {0, 20}, a, rl.YELLOW) | |
} | |
rl.DrawCircleV(mouse_pos, 40, rl.MAGENTA) | |
rl.EndDrawing() | |
} | |
rl.CloseWindow() | |
} |
Thank you!
Yeah the axis flipping in code of the rendering is a dangerous move :D I think perhaps somehow consistently interfacing with box2d and taking flip into account is the way to go, but I'm not sure yet.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah! Your videos are great. I mostly bash things together with raylib for fun every once in a while, and your tutorials on Odin definitely helped a lot getting started. I thought it would be nice and a good excuse to try out Box2d.
Btw, I removed the axis flipping from my code. Everything is flipped when I reverse the y-axis, including text/textures/etc. I never noticed before since I almost always just use circles and squares.