Class: Jolt::CharacterVirtual

Inherits:
Object
  • Object
show all
Defined in:
lib/jolt/character_virtual.rb

Constant Summary collapse

GROUND_STATES =
{
  0 => :on_ground,
  1 => :sliding,
  2 => :in_air,
  3 => :in_air
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system, shape:, position: [0, 0, 0], rotation: [0, 0, 0, 1], max_slope: Math::PI / 4, mass: 70, layer: :moving) ⇒ CharacterVirtual

Returns a new instance of CharacterVirtual.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jolt/character_virtual.rb', line 14

def initialize(system, shape:, position: [0, 0, 0], rotation: [0, 0, 0, 1],
               max_slope: Math::PI / 4, mass: 70, layer: :moving)
  unless system.is_a?(System) && !system.destroyed?
    raise InvalidArgumentError, "system must be a live Jolt::System"
  end
  unless shape.is_a?(Shape) && !shape.released?
    raise InvalidArgumentError, "shape must be a live Jolt::Shape"
  end

  @system = system
  @shape = shape
  layer_id = @system.layers.object_layer_id(layer)
  @layer = layer.to_sym
  native_position = Conversions.native_vec3(position, name: "position")
  native_rotation = Conversions.native_quat(rotation)
  slope = Conversions.positive_float(max_slope, "max_slope")
  unless slope <= Math::PI / 2
    raise InvalidArgumentError, "max_slope must not exceed pi / 2"
  end
  character_mass = Conversions.positive_float(mass, "mass")
  @pointer = Native.JR_CharacterVirtual_Create(
    @system.__native_pointer,
    @shape.native_pointer,
    native_position.pointer,
    native_rotation.pointer,
    slope,
    character_mass
  )
  raise InitializationError, "failed to create virtual character" if @pointer.null?

  @layer_id = layer_id
  @destroyed = false
  @system.__register_character(self, @shape)
rescue StandardError
  Native.JPH_CharacterBase_Destroy(@pointer) if @pointer && !@pointer.null?
  raise
end

Instance Attribute Details

#layerObject (readonly)

Returns the value of attribute layer.



12
13
14
# File 'lib/jolt/character_virtual.rb', line 12

def layer
  @layer
end

#shapeObject (readonly)

Returns the value of attribute shape.



12
13
14
# File 'lib/jolt/character_virtual.rb', line 12

def shape
  @shape
end

#systemObject (readonly)

Returns the value of attribute system.



12
13
14
# File 'lib/jolt/character_virtual.rb', line 12

def system
  @system
end

Instance Method Details

#__destroy_nativeObject



135
136
137
138
139
# File 'lib/jolt/character_virtual.rb', line 135

def __destroy_native
  Native.JPH_CharacterBase_Destroy(@pointer) if @pointer && !@pointer.null?
  @pointer = nil
  @destroyed = true
end

#__native_pointerObject



130
131
132
133
# File 'lib/jolt/character_virtual.rb', line 130

def __native_pointer
  check_alive!
  @pointer
end

#destroyObject



122
123
124
# File 'lib/jolt/character_virtual.rb', line 122

def destroy
  @system.__destroy_character(self)
end

#destroyed?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/jolt/character_virtual.rb', line 126

def destroyed?
  @destroyed
end

#ground_stateObject



112
113
114
115
# File 'lib/jolt/character_virtual.rb', line 112

def ground_state
  check_alive!
  GROUND_STATES.fetch(Native.JPH_CharacterBase_GetGroundState(@pointer))
end

#massObject



84
85
86
87
# File 'lib/jolt/character_virtual.rb', line 84

def mass
  check_alive!
  Native.JPH_CharacterVirtual_GetMass(@pointer)
end

#mass=(value) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/jolt/character_virtual.rb', line 89

def mass=(value)
  check_alive!
  Native.JPH_CharacterVirtual_SetMass(
    @pointer,
    Conversions.positive_float(value, "mass")
  )
  value
end

#positionObject



52
53
54
# File 'lib/jolt/character_virtual.rb', line 52

def position
  read_vec3(:JPH_CharacterVirtual_GetPosition)
end

#position=(value) ⇒ Object



56
57
58
# File 'lib/jolt/character_virtual.rb', line 56

def position=(value)
  write_vec3(:JPH_CharacterVirtual_SetPosition, value, "position")
end

#rotationObject



60
61
62
63
64
65
# File 'lib/jolt/character_virtual.rb', line 60

def rotation
  check_alive!
  native = Native::Quat.new
  Native.JPH_CharacterVirtual_GetRotation(@pointer, native.pointer)
  Conversions.quat(native)
end

#rotation=(value) ⇒ Object



67
68
69
70
71
72
# File 'lib/jolt/character_virtual.rb', line 67

def rotation=(value)
  check_alive!
  native = Conversions.native_quat(value)
  Native.JPH_CharacterVirtual_SetRotation(@pointer, native.pointer)
  value
end

#supported?Boolean

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/jolt/character_virtual.rb', line 117

def supported?
  check_alive!
  Native.JPH_CharacterBase_IsSupported(@pointer)
end

#update(delta_time) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jolt/character_virtual.rb', line 98

def update(delta_time)
  check_alive!
  delta_time = Conversions.positive_float(delta_time, "delta_time")
  @system.__with_native_operation("recursive virtual character update is not allowed") do
    Native.JR_CharacterVirtual_ExtendedUpdate(
      @pointer,
      delta_time,
      @layer_id,
      @system.__native_pointer
    )
    self
  end
end

#velocityObject Also known as: linear_velocity



74
75
76
# File 'lib/jolt/character_virtual.rb', line 74

def velocity
  read_vec3(:JPH_CharacterVirtual_GetLinearVelocity)
end

#velocity=(value) ⇒ Object Also known as: linear_velocity=



79
80
81
# File 'lib/jolt/character_virtual.rb', line 79

def velocity=(value)
  write_vec3(:JPH_CharacterVirtual_SetLinearVelocity, value, "velocity")
end