karl2d_box2d.odin 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package karl2d_box2d_example
  2. import b2 "vendor:box2d"
  3. import k2 "../.."
  4. import "core:math"
  5. import "core:time"
  6. import "core:log"
  7. create_box :: proc(world_id: b2.WorldId, pos: b2.Vec2) -> b2.BodyId{
  8. body_def := b2.DefaultBodyDef()
  9. body_def.type = .dynamicBody
  10. body_def.position = pos
  11. body_id := b2.CreateBody(world_id, body_def)
  12. shape_def := b2.DefaultShapeDef()
  13. shape_def.density = 1
  14. shape_def.material.friction = 0.3
  15. box := b2.MakeBox(20, 20)
  16. box_def := b2.DefaultShapeDef()
  17. _ = b2.CreatePolygonShape(body_id, box_def, box)
  18. return body_id
  19. }
  20. main :: proc() {
  21. context.logger = log.create_console_logger()
  22. k2.init(1280, 720, "Karl2D + Box2D example")
  23. b2.SetLengthUnitsPerMeter(40)
  24. world_def := b2.DefaultWorldDef()
  25. world_def.gravity = b2.Vec2{0, -900}
  26. world_id := b2.CreateWorld(world_def)
  27. defer b2.DestroyWorld(world_id)
  28. ground := k2.Rect {
  29. 0, 600,
  30. 1280, 120,
  31. }
  32. ground_body_def := b2.DefaultBodyDef()
  33. ground_body_def.position = b2.Vec2{ground.x, -ground.y-ground.h}
  34. ground_body_id := b2.CreateBody(world_id, ground_body_def)
  35. ground_box := b2.MakeBox(ground.w, ground.h)
  36. ground_shape_def := b2.DefaultShapeDef()
  37. _ = b2.CreatePolygonShape(ground_body_id, ground_shape_def, ground_box)
  38. bodies: [dynamic]b2.BodyId
  39. px: f32 = 400
  40. py: f32 = -400
  41. num_per_row := 10
  42. num_in_row := 0
  43. for _ in 0..<50 {
  44. b := create_box(world_id, {px, py})
  45. append(&bodies, b)
  46. num_in_row += 1
  47. if num_in_row == num_per_row {
  48. py += 30
  49. px = 200
  50. num_per_row -= 1
  51. num_in_row = 0
  52. }
  53. px += 30
  54. }
  55. body_def := b2.DefaultBodyDef()
  56. body_def.type = .dynamicBody
  57. body_def.position = b2.Vec2{0, 4}
  58. body_id := b2.CreateBody(world_id, body_def)
  59. shape_def := b2.DefaultShapeDef()
  60. shape_def.density = 1000
  61. shape_def.material.friction = 0.3
  62. circle: b2.Circle
  63. circle.radius = 40
  64. _ = b2.CreateCircleShape(body_id, shape_def, circle)
  65. time_step: f32 = 1.0 / 60
  66. sub_steps: i32 = 4
  67. prev_time := time.now()
  68. time_acc: f32
  69. for !k2.shutdown_wanted() {
  70. cur_time := time.now()
  71. dt := f32(time.duration_seconds(time.diff(prev_time, cur_time)))
  72. prev_time = cur_time
  73. time_acc += dt
  74. k2.process_events()
  75. k2.clear(k2.BLACK)
  76. k2.draw_rect(ground, k2.RED)
  77. mouse_pos := k2.get_mouse_position()
  78. b2.Body_SetTransform(body_id, {mouse_pos.x, -mouse_pos.y}, {})
  79. for time_acc >= time_step {
  80. b2.World_Step(world_id, time_step, sub_steps)
  81. time_acc -= time_step
  82. }
  83. for b in bodies {
  84. position := b2.Body_GetPosition(b)
  85. r := b2.Body_GetRotation(b)
  86. a := math.atan2(r.s, r.c)
  87. // Y position is flipped because raylib has Y down and box2d has Y up.
  88. k2.draw_rect_ex({position.x, -position.y, 40, 40}, {20, 20}, a*(180/3.14), k2.YELLOW)
  89. }
  90. k2.draw_circle(mouse_pos, 40, k2.MAGENTA)
  91. k2.present()
  92. free_all(context.temp_allocator)
  93. }
  94. k2.shutdown()
  95. }