From 28acb12efc239663cf2533450e3b40c2cace4901 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Fri, 23 Mar 2018 15:34:03 -0500 Subject: [PATCH] We can now attach an object to the camera- bit buggy atm. --- lib/objects/camera.rb | 58 +++++++++++++++++++++++++++++++++++-------- lib/window.rb | 7 +++--- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/lib/objects/camera.rb b/lib/objects/camera.rb index 86c3222..7757050 100644 --- a/lib/objects/camera.rb +++ b/lib/objects/camera.rb @@ -4,21 +4,57 @@ class IMICFPS include OpenGL include GLU - attr_accessor :x,:y,:z, :field_of_view, :vertical_angle, :horizontal_angle, :mouse_sensitivity - attr_reader :bound_model + attr_accessor :x,:y,:z, :field_of_view, :pitch, :yaw, :roll, :mouse_sensitivity + attr_reader :game_object def initialize(x: 0, y: 0, z: 0, fov: 70.0, distance: 100.0) @x,@y,@z = x,y,z - @vertical_angle = 0.0 - @horizontal_angle = 0.0 + @pitch = 0.0 + @yaw = 0.0 + @roll = 0.0 @field_of_view = fov @view_distance = distance + @game_object = nil + @distance = 2 + self.mouse_x, self.mouse_y = Gosu.screen_width/2, Gosu.screen_height/2 @true_mouse = Point.new(Gosu.screen_width/2, Gosu.screen_height/2) @true_mouse_checked = 0 @mouse_sensitivity = 20.0 end + def attach_to(game_object) + raise "Not a game object!" unless game_object.is_a?(GameObject) + @game_object = game_object + end + + def detach + @game_object = nil + end + + def distance_from_object + @distance + end + + def horizontal_distance_from_object + distance_from_object * Math.cos(@pitch) + end + + def vertical_distance_from_object + distance_from_object * Math.sin(@pitch) + end + + def position_camera + x_offset = horizontal_distance_from_object * Math.sin(@game_object.y_rotation.degrees_to_radians) + z_offset = horizontal_distance_from_object * Math.cos(@game_object.y_rotation.degrees_to_radians) + # p @game_object.x, @game_object.z;exit + @x = @game_object.x - x_offset + @y = @game_object.y - 2 + @z = @game_object.z - z_offset + + @yaw = 180 - @game_object.y_rotation + end + def draw #glMatrixMode(matrix) indicates that following [matrix] is going to get used glMatrixMode(GL_PROJECTION) # The projection matrix is responsible for adding perspective to our scene. @@ -26,8 +62,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 gluPerspective(@field_of_view, $window.width / $window.height, 0.1, @view_distance) glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) - glRotatef(@vertical_angle,1,0,0) - glRotatef(@horizontal_angle,0,1,0) + glRotatef(@pitch,1,0,0) + glRotatef(@yaw,0,1,0) glTranslatef(@x, @y, @z) glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored. @@ -35,11 +71,13 @@ class IMICFPS end def update + position_camera if @game_object + if @true_mouse_checked > 2 - @horizontal_angle-=Float(@true_mouse.x-self.mouse_x)/(@mouse_sensitivity*@field_of_view)*70 - @vertical_angle-=Float(@true_mouse.y-self.mouse_y)/(@mouse_sensitivity*@field_of_view)*70 - @horizontal_angle %= 360.0 - @vertical_angle = @vertical_angle.clamp(-90.0, 90.0) + @yaw-=Float(@true_mouse.x-self.mouse_x)/(@mouse_sensitivity*@field_of_view)*70 unless @game_object + @pitch-=Float(@true_mouse.y-self.mouse_y)/(@mouse_sensitivity*@field_of_view)*70 unless @game_object + @yaw %= 360.0 + @pitch = @pitch.clamp(-90.0, 90.0) else @true_mouse_checked+=1 @true_mouse.x = self.mouse_x diff --git a/lib/window.rb b/lib/window.rb index 9a6b55b..50cbe70 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -27,8 +27,9 @@ class IMICFPS # Model.new(type: :obj, file_path: "objects/sponza.obj", scale: 1, y: -0.2) @terrain = Terrain.new(size: 20, height: 0) - @camera = Camera.new(x: 0, y: -2, z: 1) @player = Player.new(x: 1, y: 0, z: -10) + @camera = Camera.new(x: 0, y: -2, z: 1) + @camera.attach_to(@player) @crosshair_size = 10 @crosshair_thickness = 3 @@ -81,9 +82,9 @@ class IMICFPS OpenGL Version: #{glGetString(GL_VERSION)}~ OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)}~ ~ - Vertical Angle: #{@camera.vertical_angle.round(2)} Horizontal Angle: #{@camera.horizontal_angle.round(2)} ~ + Camera pitch: #{@camera.pitch.round(2)} Yaw: #{@camera.yaw.round(2)} Roll #{@camera.roll.round(2)} ~ Camera X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} ~ - #{if @camera.bound_model then "Actor X:#{@camera.bound_model.x.round(2)} Y:#{@camera.bound_model.y.round(2)} Z:#{@camera.bound_model.z.round(2)}";end} ~ + #{if @camera.game_object then "Actor X:#{@camera.game_object.x.round(2)} Y:#{@camera.game_object.y.round(2)} Z:#{@camera.game_object.z.round(2)}";end} ~ Field Of View: #{@camera.field_of_view} ~ Mouse Sesitivity: #{@camera.mouse_sensitivity} ~ Faces: #{@number_of_faces} ~