Gravity constant moved into constants.rb, camera can now report what it is looking at, entity has knowledge of camera now, AABB tree search can now use either BoundingBox or Ray colliders

This commit is contained in:
2019-08-13 09:47:23 -05:00
parent 771b6a4247
commit 016e8109de
9 changed files with 57 additions and 33 deletions

View File

@@ -8,4 +8,9 @@ class IMICFPS
# Objects exported from blender using the default or meter object scale will be close to 1 GL unit # Objects exported from blender using the default or meter object scale will be close to 1 GL unit
MODEL_METER_SCALE = 1.0 MODEL_METER_SCALE = 1.0
# Earth
GRAVITY = 9.8 # m/s
# Moon
# GRAVITY = 1.625 # m/s
end end

View File

@@ -19,17 +19,18 @@ class IMICFPS
next unless node = @aabb_tree.objects[entity] next unless node = @aabb_tree.objects[entity]
unless entity.bounding_box == node.bounding_box unless entity.bounding_box == node.bounding_box
@aabb_tree.update(entity, entity.bounding_box) puts "Updating #{entity.class}"
@aabb_tree.update(entity, entity.bounding_box.clone)
end end
end end
check_broadphase check_broadphase
@physics_manager.update @physics_manager.update
collisions.each do |ent, list|
# puts "#{ent.class} -> [#{list.map { |e| e.class }.join(', ')}] (#{Gosu.milliseconds})"
end end
def search(collider)
@aabb_tree.search(collider)
end end
def remove(entity) def remove(entity)

View File

@@ -2,13 +2,11 @@ class IMICFPS
class Camera class Camera
include CommonMethods include CommonMethods
attr_accessor :field_of_view, :pitch, :yaw, :roll, :mouse_sensitivity attr_accessor :field_of_view, :mouse_sensitivity
attr_reader :entity, :position attr_reader :entity, :position, :orientation, :mouse_captured
def initialize(x: 0, y: 0, z: 0, fov: 70.0, view_distance: 155.0) def initialize(x: 0, y: 0, z: 0, fov: 70.0, view_distance: 155.0)
@position = Vector.new(x,y,z) @position = Vector.new(x,y,z)
@pitch = 0.0 @orientation = Vector.new(0, 0, 0)
@yaw = 0.0
@roll = 0.0
@field_of_view = fov @field_of_view = fov
@view_distance = view_distance @view_distance = view_distance
@constant_pitch = 20.0 @constant_pitch = 20.0
@@ -17,8 +15,8 @@ class IMICFPS
@distance = 4 @distance = 4
@origin_distance = @distance @origin_distance = @distance
self.mouse_x, self.mouse_y = window.width/2, window.height/2 self.mouse_x, self.mouse_y = window.width / 2, window.height / 2
@true_mouse = Point.new(window.width/2, window.height/2) @true_mouse = Point.new(window.width / 2, window.height / 2)
@mouse_sensitivity = 20.0 # Less is faster, more is slower @mouse_sensitivity = 20.0 # Less is faster, more is slower
@mouse_captured = true @mouse_captured = true
@mouse_checked = 0 @mouse_checked = 0
@@ -27,9 +25,11 @@ class IMICFPS
def attach_to(entity) def attach_to(entity)
raise "Not an Entity!" unless entity.is_a?(Entity) raise "Not an Entity!" unless entity.is_a?(Entity)
@entity = entity @entity = entity
@entity.attach_camera(self)
end end
def detach def detach
@entity.detach_camera
@entity = nil @entity = nil
end end
@@ -61,7 +61,7 @@ class IMICFPS
@position.y = @entity.position.y + 2 @position.y = @entity.position.y + 2
@position.z = @entity.position.z - z_offset @position.z = @entity.position.z - z_offset
@yaw = 180 - @entity.rotation.y @orientation.y = 180 - @entity.rotation.y
end end
def draw def draw
@@ -71,8 +71,8 @@ class IMICFPS
# Calculates aspect ratio of the window. Gets perspective view. 45 is degree viewing angle, (0.1, 100) are ranges how deep can we draw into the screen # Calculates aspect ratio of the window. Gets perspective view. 45 is degree viewing angle, (0.1, 100) are ranges how deep can we draw into the screen
gluPerspective(@field_of_view, window.width / window.height, 0.1, @view_distance) gluPerspective(@field_of_view, window.width / window.height, 0.1, @view_distance)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glRotatef(@pitch,1,0,0) glRotatef(@orientation.z, 1, 0, 0)
glRotatef(@yaw,0,1,0) glRotatef(@orientation.y, 0, 1, 0)
glTranslatef(-@position.x, -@position.y, -@position.z) glTranslatef(-@position.x, -@position.y, -@position.z)
glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored. glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored.
glLoadIdentity glLoadIdentity
@@ -82,25 +82,30 @@ class IMICFPS
def update def update
if @mouse_captured if @mouse_captured
delta = Float(@true_mouse.x-self.mouse_x)/(@mouse_sensitivity*@field_of_view)*70 delta = Float(@true_mouse.x - self.mouse_x) / (@mouse_sensitivity * @field_of_view) * 70
@yaw -= delta @orientation.y -= delta
@yaw %= 360.0 @orientation.y %= 360.0
@pitch -= Float(@true_mouse.y-self.mouse_y)/(@mouse_sensitivity*@field_of_view)*70 @orientation.z -= Float(@true_mouse.y - self.mouse_y) / (@mouse_sensitivity * @field_of_view) * 70
@pitch = @pitch.clamp(-90.0, 90.0) @orientation.z = @orientation.z.clamp(-90.0, 90.0)
@entity.rotation.y += delta if @entity @entity.rotation.y += delta if @entity
free_move unless @entity free_move unless @entity
position_camera if @entity position_camera if @entity
self.mouse_x = window.width/2 if self.mouse_x <= 1 || window.mouse_x >= window.width-1 self.mouse_x = window.width / 2 if self.mouse_x <= 1 || window.mouse_x >= window.width-1
self.mouse_y = window.height/2 if self.mouse_y <= 1 || window.mouse_y >= window.height-1 self.mouse_y = window.height / 2 if self.mouse_y <= 1 || window.mouse_y >= window.height-1
@true_mouse.x, @true_mouse.y = self.mouse_x, self.mouse_y @true_mouse.x, @true_mouse.y = self.mouse_x, self.mouse_y
end end
end end
def looking_at
ray = Ray.new(@position, @orientation.direction * -1)
window.current_state.collision_manager.search(ray)
end
def free_move def free_move
relative_y_rotation = (@yaw + 180) relative_y_rotation = (@orientation.y + 180)
relative_speed = 0.25 relative_speed = 0.25
if InputMapper.down?( :forward) if InputMapper.down?( :forward)

View File

@@ -90,6 +90,9 @@ class IMICFPS
end end
def update def update
# Do not handle movement if mouse is not captured
return if @camera && !@camera.mouse_captured
relative_speed = @speed relative_speed = @speed
if InputMapper.down?(:sprint) if InputMapper.down?(:sprint)
relative_speed = @running_speed relative_speed = @running_speed

View File

@@ -7,7 +7,7 @@ class IMICFPS
attr_accessor :scale, :visible, :renderable, :backface_culling attr_accessor :scale, :visible, :renderable, :backface_culling
attr_accessor :position, :rotation, :velocity attr_accessor :position, :rotation, :velocity
attr_reader :name, :debug_color, :bounding_box, :collision, :physics, :mass, :drag attr_reader :name, :debug_color, :bounding_box, :collision, :physics, :mass, :drag, :camera
def initialize(x: 0, y: 0, z: 0, bound_model: nil, scale: MODEL_METER_SCALE, backface_culling: true, auto_manage: true, manifest_file: nil) def initialize(x: 0, y: 0, z: 0, bound_model: nil, scale: MODEL_METER_SCALE, backface_culling: true, auto_manage: true, manifest_file: nil)
@position = Vector.new(x, y, z) @position = Vector.new(x, y, z)
@@ -43,6 +43,8 @@ class IMICFPS
normalize_bounding_box normalize_bounding_box
end end
@camera = nil
return self return self
end end
@@ -70,6 +72,14 @@ class IMICFPS
@bound_model = nil @bound_model = nil
end end
def attach_camera(camera)
@camera = camera
end
def detach_camera
@camera = nil
end
def setup def setup
end end

View File

@@ -1,5 +1,7 @@
class IMICFPS class IMICFPS
class Game < GameState class Game < GameState
attr_reader :collision_manager
def setup def setup
@collision_manager = CollisionManager.new(game_state: self) @collision_manager = CollisionManager.new(game_state: self)
@renderer = Renderer.new(game_state: self) @renderer = Renderer.new(game_state: self)
@@ -154,7 +156,7 @@ OpenGL Renderer: #{glGetString(GL_RENDERER)}
OpenGL Version: #{glGetString(GL_VERSION)} OpenGL Version: #{glGetString(GL_VERSION)}
OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)} OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)}
Camera pitch: #{@camera.pitch.round(2)} Yaw: #{@camera.yaw.round(2)} Roll #{@camera.roll.round(2)} Camera pitch: #{@camera.orientation.z.round(2)} Yaw: #{@camera.orientation.y.round(2)} Roll #{@camera.orientation.x.round(2)}
Camera X:#{@camera.position.x.round(2)} Y:#{@camera.position.y.round(2)} Z:#{@camera.position.z.round(2)} Camera X:#{@camera.position.x.round(2)} Y:#{@camera.position.y.round(2)} Z:#{@camera.position.z.round(2)}
#{if @camera.entity then "Actor X:#{@camera.entity.position.x.round(2)} Y:#{@camera.entity.position.y.round(2)} Z:#{@camera.entity.position.z.round(2)}";end} #{if @camera.entity then "Actor X:#{@camera.entity.position.x.round(2)} Y:#{@camera.entity.position.y.round(2)} Z:#{@camera.entity.position.z.round(2)}";end}
Field Of View: #{@camera.field_of_view} Field Of View: #{@camera.field_of_view}

View File

@@ -55,13 +55,13 @@ class IMICFPS
end end
end end
def search_subtree(bounding_box, items = []) def search_subtree(collider, items = [])
if @bounding_box.intersect?(bounding_box) if @bounding_box.intersect?(collider)
if leaf? if leaf?
items << self items << self
else else
@a.search_subtree(bounding_box, items) @a.search_subtree(collider, items)
@b.search_subtree(bounding_box, items) @b.search_subtree(collider, items)
end end
end end

View File

@@ -35,11 +35,11 @@ class IMICFPS
insert_leaf(leaf) insert_leaf(leaf)
end end
# Returns a list of all collided objects inside Bounding Box # Returns a list of all objects that collided with collider
def search(bounding_box, return_nodes = false) def search(collider, return_nodes = false)
items = [] items = []
if @root if @root
items = @root.search_subtree(bounding_box) items = @root.search_subtree(collider)
items.map! {|e| e.object} unless return_nodes items.map! {|e| e.object} unless return_nodes
end end

View File

@@ -1,6 +1,4 @@
class IMICFPS class IMICFPS
GRAVITY = 9.8 # m/s
class Window < CyberarmEngine::Engine class Window < CyberarmEngine::Engine
attr_accessor :number_of_vertices, :needs_cursor attr_accessor :number_of_vertices, :needs_cursor
attr_reader :camera attr_reader :camera