main.odin ¶
1package main
2
3import rl "vendor:raylib"
4import gl "vendor:raylib/rlgl"
5import b3 "vendor:box3d"
6
7NUM_BOXES :: 25
8
9main :: proc() {
10 rl.InitWindow(1024, 768, "Box3D + Raylib 6 sample")
11
12 camera := rl.Camera3D{
13 position = {25, 15, 25},
14 up = {0, 1, 0},
15 fovy = 45,
16 projection = .PERSPECTIVE,
17 }
18
19 rl.SetTargetFPS(60)
20
21 world_def := b3.DefaultWorldDef()
22 world_def.gravity = {0, -10, 0}
23 world_id := b3.CreateWorld(world_def)
24
25 ground_body_def := b3.DefaultBodyDef()
26 ground_body_def.position = {0, -10, 0}
27 ground_id := b3.CreateBody(world_id, ground_body_def)
28
29 ground_box := b3.MakeBoxHull(50, 10, 50)
30 ground_shape_def := b3.DefaultShapeDef()
31 _ = b3.CreateHullShape(ground_id, ground_shape_def, &ground_box.base)
32
33 boxes: [NUM_BOXES]b3.BodyId
34 for i in 0..<len(boxes) {
35 body_def := b3.DefaultBodyDef()
36 body_def.type = .dynamicBody
37
38 offset_x := f32((i % 2 == 0) ? .05 : -.05)
39 body_def.position = {offset_x, 2. + f32(i) * 2.5, 0}
40 boxes[i] = b3.CreateBody(world_id, body_def)
41
42 dynamic_box := b3.MakeCubeHull(1)
43 shape_def := b3.DefaultShapeDef()
44 shape_def.density = 1
45 shape_def.baseMaterial.friction = .3
46
47 _ = b3.CreateHullShape(boxes[i], shape_def, &dynamic_box.base)
48 }
49
50 time_step: f32 = 1. / 60.
51 sub_step_count: i32 = 4
52
53 for !rl.WindowShouldClose() {
54 b3.World_Step(world_id, time_step, sub_step_count)
55
56 rl.BeginDrawing()
57 {
58 defer rl.EndDrawing()
59
60 rl.ClearBackground(rl.RAYWHITE)
61
62 rl.BeginMode3D(camera)
63 {
64 defer rl.EndMode3D()
65
66 rl.DrawCube({0, -2, 0}, 100, 4, 100, rl.LIGHTGRAY)
67 rl.DrawCubeWires({0, -2, 0}, 100, 4, 100, rl.GRAY)
68
69 for box in boxes {
70 pos := b3.Body_GetPosition(box)
71 rot := b3.Body_GetRotation(box)
72
73 angle, axis := b3.GetAxisAngle(rot)
74
75 gl.PushMatrix()
76 {
77 defer gl.PopMatrix()
78
79 gl.Translatef(pos.x, pos.y, pos.z)
80 gl.Rotatef(angle * rl.RAD2DEG, axis.x, axis.y, axis.z)
81
82 rl.DrawCube(0, 2, 2, 2, rl.BLUE)
83 rl.DrawCubeWires(0, 2, 2, 2, rl.DARKBLUE)
84 }
85 }
86
87 rl.DrawGrid(20, 5)
88 }
89
90 rl.DrawFPS(10, 10)
91 rl.DrawText("Box3D + Raylib 6 sample", 10, 35, 20, rl.DARKGRAY)
92 }
93 }
94
95 b3.DestroyWorld(world_id)
96 rl.CloseWindow()
97}