Class: Box2D::World
Constant Summary
collapse
- BODY_TYPES =
Body::TYPE_VALUES
Instance Attribute Summary collapse
Instance Method Summary
collapse
#overlap_aabb, #overlap_capsule, #overlap_circle, #overlap_polygon, #raycast
#create_distance_joint, #create_mouse_joint, #create_prismatic_joint, #create_revolute_joint, #create_weld_joint
#body_for_id, #body_user_data, #register_body, #register_chain, #register_joint, #register_shape, #set_body_user_data, #set_shape_user_data, #shape_for_id, #shape_user_data, #unregister_body, #unregister_chain, #unregister_joint, #unregister_shape
Constructor Details
#initialize(gravity: [0, -9.8], substeps: 4, sleep: true, continuous: true) ⇒ World
Returns a new instance of World.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/box2d/world.rb', line 14
def initialize(gravity: [0, -9.8], substeps: 4, sleep: true, continuous: true)
definition = Native.b2DefaultWorldDef
definition[:gravity] = ValueConversion.native_vec2(gravity, label: "gravity")
definition[:enableSleep] = !!sleep
definition[:enableContinuous] = !!continuous
@id = Native.b2CreateWorld(definition.pointer)
@destroyed = false
@stepping = false
@drawing = false
initialize_registries
@events = Events.empty
self.substeps = substeps
yield self if block_given?
rescue Exception
destroy if @id && !@destroyed
raise
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
12
13
14
|
# File 'lib/box2d/world.rb', line 12
def id
@id
end
|
Instance Method Details
#create_body(type: :static, position: [0, 0], angle: 0, **options) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/box2d/world.rb', line 94
def create_body(type: :static, position: [0, 0], angle: 0, **options)
ensure_access!
definition = build_body_definition(type:, position:, angle:, options:)
body = Body.new(self, Native.b2CreateBody(@id, definition.pointer))
register_body(body)
body.user_data = options[:user_data] if options.key?(:user_data)
begin
yield body if block_given?
rescue Exception
body.destroy if body.valid?
raise
end
body
end
|
#debug_draw(flags: [:shapes]) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/box2d/world.rb', line 110
def debug_draw(flags: [:shapes])
raise ArgumentError, "a debug draw block is required" unless block_given?
ensure_access!
@debug_draw = DebugDraw.new(flags) { |command| yield command }
@drawing = true
begin
Native.b2World_Draw(@id, @debug_draw.definition.pointer)
ensure
@drawing = false
end
@debug_draw.raise_callback_error
self
end
|
#destroy ⇒ Object
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/box2d/world.rb', line 44
def destroy
return false if @destroyed
ensure_access!
Native.b2DestroyWorld(@id)
@destroyed = true
clear_registries
@events = Events.empty
true
end
|
#destroyed? ⇒ Boolean
40
41
42
|
# File 'lib/box2d/world.rb', line 40
def destroyed?
!valid?
end
|
#ensure_access! ⇒ Object
125
126
127
128
129
130
131
132
|
# File 'lib/box2d/world.rb', line 125
def ensure_access!
raise UseAfterDestroyError, "Box2D::World has been destroyed" if @destroyed
if @stepping || @drawing
raise ReentrantStepError, "the world cannot be accessed during a native operation"
end
true
end
|
#events ⇒ Object
89
90
91
92
|
# File 'lib/box2d/world.rb', line 89
def events
ensure_access!
@events
end
|
#gravity ⇒ Object
66
67
68
69
|
# File 'lib/box2d/world.rb', line 66
def gravity
ensure_access!
ValueConversion.vec2(Native.b2World_GetGravity(@id))
end
|
#gravity=(value) ⇒ Object
71
72
73
74
|
# File 'lib/box2d/world.rb', line 71
def gravity=(value)
ensure_access!
Native.b2World_SetGravity(@id, ValueConversion.native_vec2(value, label: "gravity"))
end
|
#step(delta_time) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/box2d/world.rb', line 76
def step(delta_time)
ensure_access!
time_step = ValueConversion.non_negative_float(delta_time, label: "delta_time")
@stepping = true
begin
Native.b2World_Step(@id, time_step, @substeps)
@events = Events.capture(self)
self
ensure
@stepping = false
end
end
|
#substeps ⇒ Object
55
56
57
|
# File 'lib/box2d/world.rb', line 55
def substeps
@substeps
end
|
#substeps=(value) ⇒ Object
59
60
61
62
63
64
|
# File 'lib/box2d/world.rb', line 59
def substeps=(value)
count = Integer(value)
raise ArgumentError, "substeps must be greater than zero" unless count.positive?
@substeps = count
end
|
#valid? ⇒ Boolean
34
35
36
37
38
|
# File 'lib/box2d/world.rb', line 34
def valid?
raise ReentrantStepError, "the world cannot be accessed during a native operation" if @stepping || @drawing
!@destroyed && Native.b2World_IsValid(@id)
end
|