Class: Box2D::Events

Inherits:
Object
  • Object
show all
Defined in:
lib/box2d/events.rb

Constant Summary collapse

EMPTY =
[].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(begin_contacts:, end_contacts:, hits:, sensor_begins:, sensor_ends:) ⇒ Events

Returns a new instance of Events.



68
69
70
71
72
73
74
75
# File 'lib/box2d/events.rb', line 68

def initialize(begin_contacts:, end_contacts:, hits:, sensor_begins:, sensor_ends:)
  @begin_contacts = begin_contacts.freeze
  @end_contacts = end_contacts.freeze
  @hits = hits.freeze
  @sensor_begins = sensor_begins.freeze
  @sensor_ends = sensor_ends.freeze
  freeze
end

Instance Attribute Details

#begin_contactsObject (readonly)

Returns the value of attribute begin_contacts.



15
16
17
# File 'lib/box2d/events.rb', line 15

def begin_contacts
  @begin_contacts
end

#end_contactsObject (readonly)

Returns the value of attribute end_contacts.



15
16
17
# File 'lib/box2d/events.rb', line 15

def end_contacts
  @end_contacts
end

#hitsObject (readonly)

Returns the value of attribute hits.



15
16
17
# File 'lib/box2d/events.rb', line 15

def hits
  @hits
end

#sensor_beginsObject (readonly)

Returns the value of attribute sensor_begins.



15
16
17
# File 'lib/box2d/events.rb', line 15

def sensor_begins
  @sensor_begins
end

#sensor_endsObject (readonly)

Returns the value of attribute sensor_ends.



15
16
17
# File 'lib/box2d/events.rb', line 15

def sensor_ends
  @sensor_ends
end

Class Method Details

.capture(world) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/box2d/events.rb', line 27

def self.capture(world)
  contact_events = Native.b2World_GetContactEvents(world.id)
  sensor_events = Native.b2World_GetSensorEvents(world.id)
  new(
    begin_contacts: read_array(contact_events[:beginEvents], contact_events[:beginCount], Native::ContactBeginTouchEvent) do |event|
      ContactBegin.new(
        shape_a: world.shape_for_id(event[:shapeIdA]),
        shape_b: world.shape_for_id(event[:shapeIdB]),
        manifold: capture_manifold(event[:manifold])
      )
    end,
    end_contacts: read_array(contact_events[:endEvents], contact_events[:endCount], Native::ContactEndTouchEvent) do |event|
      ContactEnd.new(
        shape_a: world.shape_for_id(event[:shapeIdA]),
        shape_b: world.shape_for_id(event[:shapeIdB])
      )
    end,
    hits: read_array(contact_events[:hitEvents], contact_events[:hitCount], Native::ContactHitEvent) do |event|
      ContactHit.new(
        shape_a: world.shape_for_id(event[:shapeIdA]),
        shape_b: world.shape_for_id(event[:shapeIdB]),
        point: ValueConversion.vec2(event[:point]),
        normal: ValueConversion.vec2(event[:normal]),
        approach_speed: event[:approachSpeed]
      )
    end,
    sensor_begins: read_array(sensor_events[:beginEvents], sensor_events[:beginCount], Native::SensorBeginTouchEvent) do |event|
      SensorBegin.new(
        sensor: world.shape_for_id(event[:sensorShapeId]),
        visitor: world.shape_for_id(event[:visitorShapeId])
      )
    end,
    sensor_ends: read_array(sensor_events[:endEvents], sensor_events[:endCount], Native::SensorEndTouchEvent) do |event|
      SensorEnd.new(
        sensor: world.shape_for_id(event[:sensorShapeId]),
        visitor: world.shape_for_id(event[:visitorShapeId])
      )
    end
  )
end

.emptyObject



17
18
19
20
21
22
23
24
25
# File 'lib/box2d/events.rb', line 17

def self.empty
  new(
    begin_contacts: EMPTY,
    end_contacts: EMPTY,
    hits: EMPTY,
    sensor_begins: EMPTY,
    sensor_ends: EMPTY
  )
end