We can now attach an object to the camera- bit buggy atm.

This commit is contained in:
2018-03-23 15:34:03 -05:00
parent 021805cd96
commit 28acb12efc
2 changed files with 52 additions and 13 deletions

View File

@@ -4,21 +4,57 @@ class IMICFPS
include OpenGL include OpenGL
include GLU include GLU
attr_accessor :x,:y,:z, :field_of_view, :vertical_angle, :horizontal_angle, :mouse_sensitivity attr_accessor :x,:y,:z, :field_of_view, :pitch, :yaw, :roll, :mouse_sensitivity
attr_reader :bound_model attr_reader :game_object
def initialize(x: 0, y: 0, z: 0, fov: 70.0, distance: 100.0) def initialize(x: 0, y: 0, z: 0, fov: 70.0, distance: 100.0)
@x,@y,@z = x,y,z @x,@y,@z = x,y,z
@vertical_angle = 0.0 @pitch = 0.0
@horizontal_angle = 0.0 @yaw = 0.0
@roll = 0.0
@field_of_view = fov @field_of_view = fov
@view_distance = distance @view_distance = distance
@game_object = nil
@distance = 2
self.mouse_x, self.mouse_y = Gosu.screen_width/2, Gosu.screen_height/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 = Point.new(Gosu.screen_width/2, Gosu.screen_height/2)
@true_mouse_checked = 0 @true_mouse_checked = 0
@mouse_sensitivity = 20.0 @mouse_sensitivity = 20.0
end 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 def draw
#glMatrixMode(matrix) indicates that following [matrix] is going to get used #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. 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 # 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(@vertical_angle,1,0,0) glRotatef(@pitch,1,0,0)
glRotatef(@horizontal_angle,0,1,0) glRotatef(@yaw,0,1,0)
glTranslatef(@x, @y, @z) glTranslatef(@x, @y, @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.
@@ -35,11 +71,13 @@ class IMICFPS
end end
def update def update
position_camera if @game_object
if @true_mouse_checked > 2 if @true_mouse_checked > 2
@horizontal_angle-=Float(@true_mouse.x-self.mouse_x)/(@mouse_sensitivity*@field_of_view)*70 @yaw-=Float(@true_mouse.x-self.mouse_x)/(@mouse_sensitivity*@field_of_view)*70 unless @game_object
@vertical_angle-=Float(@true_mouse.y-self.mouse_y)/(@mouse_sensitivity*@field_of_view)*70 @pitch-=Float(@true_mouse.y-self.mouse_y)/(@mouse_sensitivity*@field_of_view)*70 unless @game_object
@horizontal_angle %= 360.0 @yaw %= 360.0
@vertical_angle = @vertical_angle.clamp(-90.0, 90.0) @pitch = @pitch.clamp(-90.0, 90.0)
else else
@true_mouse_checked+=1 @true_mouse_checked+=1
@true_mouse.x = self.mouse_x @true_mouse.x = self.mouse_x

View File

@@ -27,8 +27,9 @@ class IMICFPS
# Model.new(type: :obj, file_path: "objects/sponza.obj", scale: 1, y: -0.2) # Model.new(type: :obj, file_path: "objects/sponza.obj", scale: 1, y: -0.2)
@terrain = Terrain.new(size: 20, height: 0) @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) @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_size = 10
@crosshair_thickness = 3 @crosshair_thickness = 3
@@ -81,9 +82,9 @@ class IMICFPS
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)}~
~ ~
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)} ~ 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} ~ Field Of View: #{@camera.field_of_view} ~
Mouse Sesitivity: #{@camera.mouse_sensitivity} ~ Mouse Sesitivity: #{@camera.mouse_sensitivity} ~
Faces: #{@number_of_faces} ~ Faces: #{@number_of_faces} ~