Exported door model, added CollisionManager#on_ground?(entity), made gravity work in PhyisicsManager if Entity has physics enabled, updated test map

This commit is contained in:
2019-09-25 18:17:18 -05:00
parent 954c6899be
commit ecee086590
10 changed files with 247 additions and 47 deletions

View File

@@ -91,7 +91,6 @@ class IMICFPS
if @entity
@entity.orientation.y += delta
@entity.orientation.y %= 360
position_camera
else
free_move

View File

@@ -9,6 +9,7 @@ class IMICFPS
def setup
bind_model("base", "biped")
@collision = :dynamic
@physics = true
@speed = 2.5 # meter's per second
@running_speed = 5.0 # meter's per second
@@ -93,11 +94,6 @@ class IMICFPS
# Do not handle movement if mouse is not captured
return if @camera && !@camera.mouse_captured
if @_time_in_air
air_time = (Gosu.milliseconds - @_time_in_air) / 1000.0
@velocity.y -= IMICFPS::GRAVITY * air_time * delta_time
end
super
end
@@ -138,25 +134,8 @@ class IMICFPS
end
def jump
if InputMapper.down?(:jump) && !@jumping
@jumping = true
@_time_in_air = Gosu.milliseconds
elsif !@jumping
@falling = true
@_time_in_air ||= Gosu.milliseconds # FIXME
else
if @jumping && @velocity.y <= 0
@falling = false
@jumping = false
end
end
if @jumping && !@falling
if InputMapper.down?(:jump)
@velocity.y = 1.5
@falling = true
end
if InputMapper.down?(:jump) && window.current_state.collision_manager.on_ground?(self)
@velocity.y = 1.5
end
end

View File

@@ -9,13 +9,14 @@ class IMICFPS
attr_accessor :position, :orientation, :velocity
attr_reader :name, :debug_color, :bounding_box, :collision, :physics, :mass, :drag, :camera
def initialize(manifest:, map_entity: nil, spawnpoint: nil, backface_culling: true, auto_manage: true)
def initialize(manifest:, map_entity: nil, spawnpoint: nil, backface_culling: false, auto_manage: true)
@manifest = manifest
@position = map_entity ? map_entity.position : spawnpoint.position
@orientation = map_entity ? map_entity.orientation : spawnpoint.orientation
@position = map_entity ? map_entity.position.clone : spawnpoint.position.clone
@orientation = map_entity ? map_entity.orientation.clone : spawnpoint.orientation.clone
@scale = map_entity ? map_entity.scale : 1.0
@backface_culling = backface_culling
@name = @manifest.name
@bound_model = map_entity ? bind_model(map_entity.package, map_entity.model) : nil
@visible = true
@@ -41,7 +42,6 @@ class IMICFPS
if @bound_model
@bound_model.model.entity = self
@bound_model.model.objects.each { |o| o.scale = self.scale }
@normalized_bounding_box = normalize_bounding_box_with_offset
normalize_bounding_box
@@ -62,7 +62,6 @@ class IMICFPS
raise "model isn't a model!" unless model.is_a?(ModelLoader)
@bound_model = model
@bound_model.model.entity = self
@bound_model.model.objects.each { |o| o.scale = self.scale }
@bounding_box = normalize_bounding_box_with_offset
return model

View File

@@ -82,5 +82,29 @@ class IMICFPS
return box
end
def on_ground?(entity) # TODO: Use some form of caching to speed this up
on_ground = false
@collisions.detect do |a, b|
next unless entity == a || entity == b
vs = a
vs = b if a == entity
broadphase = search(Ray.new(entity.position, Vector.new(0, -1, 0), entity.velocity.y.abs))
broadphase.detect do |ent|
ray = Ray.new(entity.position - ent.position, Vector.new(0, -1, 0))
if ent.model.aabb_tree.search(ray).size > 0
on_ground = true
return true
end
end
break if on_ground
end
return on_ground
end
end
end

View File

@@ -15,21 +15,24 @@ class IMICFPS
end
def resolve(entity, other)
if other.is_a?(Terrain)
entity.velocity.y = 0 if entity.velocity.y < 0
else
entity.velocity.y = other.velocity.y if other.velocity.y < entity.velocity.y && entity.velocity.y < 0
end
entity.velocity.y = other.velocity.y if other.velocity.y < entity.velocity.y && entity.velocity.y < 0
end
def simulate
@collision_manager.game_state.entities.each do |entity|
entity.velocity.x *= entity.drag
entity.velocity.z *= entity.drag
entity.position.x += entity.velocity.x * entity.delta_time
entity.position.y += entity.velocity.y * entity.delta_time
entity.position.z += entity.velocity.z * entity.delta_time
on_ground = @collision_manager.on_ground?(entity)
entity.velocity.x *= entity.drag
entity.velocity.z *= entity.drag
if on_ground
entity.velocity.y = 0
else
entity.velocity.y -= IMICFPS::GRAVITY * entity.delta_time if entity.physics
end
end
end
end