Refactored GameObject to Entity, replaced @x,@y,@z with @position, added @velocity vector to Entity, bricked Player terrain interaction while authoring Axis Aligned Bounding Box Tree for CollisionManager to handle all collision interaction. Added PhysicsManager stub.

This commit is contained in:
2019-02-20 10:49:56 -06:00
parent df05ec5150
commit 7b903fbdb9
27 changed files with 288 additions and 385 deletions

View File

@@ -0,0 +1,186 @@
class IMICFPS
class Camera
include CommonMethods
include OpenGL
include GLU
attr_accessor :field_of_view, :pitch, :yaw, :roll, :mouse_sensitivity
attr_reader :entity, :position
def initialize(x: 0, y: 0, z: 0, fov: 70.0, view_distance: 100.0)
@position = Vector.new(x,y,z)
@pitch = 0.0
@yaw = 0.0
@roll = 0.0
@field_of_view = fov
@view_distance = view_distance
@constant_pitch = 20.0
@entity = nil
@distance = 4
@origin_distance = @distance
self.mouse_x, self.mouse_y = $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_captured = true
@mouse_checked = 0
InputMapper.set(:camera, :ascend, Gosu::KbSpace)
InputMapper.set(:camera, :descend, [Gosu::KbLeftControl, Gosu::KbRightControl])
InputMapper.set(:camera, :release_mouse, [Gosu::KbLeftAlt, Gosu::KbRightAlt])
InputMapper.set(:camera, :capture_mouse, Gosu::MsLeft)
InputMapper.set(:camera, :turn_180, Gosu::KbX)
InputMapper.set(:camera, :increase_mouse_sensitivity, Gosu::KB_NUMPAD_PLUS)
InputMapper.set(:camera, :decrease_mouse_sensitivity, Gosu::KB_NUMPAD_MINUS)
InputMapper.set(:camera, :reset_mouse_sensitivity, Gosu::KB_NUMPAD_MULTIPLY)
InputMapper.set(:camera, :increase_view_distance, Gosu::MsWheelUp)
InputMapper.set(:camera, :decrease_view_distance, Gosu::MsWheelDown)
end
def attach_to(entity)
raise "Not a game object!" unless entity.is_a?(Entity)
@entity = entity
end
def detach
@entity = nil
end
def distance_from_object
@distance
end
def horizontal_distance_from_object
distance_from_object * Math.cos(@constant_pitch)
end
def vertical_distance_from_object
distance_from_object * Math.sin(@constant_pitch)
end
def position_camera
if defined?(@entity.first_person_view)
if @entity.first_person_view
@distance = 0
else
@distance = @origin_distance
end
end
x_offset = horizontal_distance_from_object * Math.sin(@entity.rotation.y.degrees_to_radians)
z_offset = horizontal_distance_from_object * Math.cos(@entity.rotation.y.degrees_to_radians)
# p @entity.x, @entity.z;exit
@position.x = @entity.position.x - x_offset
@position.y = @entity.position.y + 2
@position.z = @entity.position.z - z_offset
# @yaw = 180 - @entity.y_rotation
@entity.rotation.y = -@yaw + 180
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.
glLoadIdentity # Resets current modelview matrix
# 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(@pitch,1,0,0)
glRotatef(@yaw,0,1,0)
glTranslatef(-@position.x, -@position.y, -@position.z)
glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored.
glLoadIdentity
# if $debug && @entity
# glBegin(GL_LINES)
# glColor3f(1,0,0)
# glVector3f(@x, @y, @z)
# glVector3f(@entity.x, @entity.y, @entity.z)
# glEnd
# end
end
def update
if @mouse_captured
position_camera if @entity
delta = Float(@true_mouse.x-self.mouse_x)/(@mouse_sensitivity*@field_of_view)*70
@yaw -= delta
@yaw %= 360.0
@pitch -= Float(@true_mouse.y-self.mouse_y)/(@mouse_sensitivity*@field_of_view)*70
@pitch = @pitch.clamp(-90.0, 90.0)
@entity.rotation.y += delta if @entity
free_move unless @entity
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
@true_mouse.x, @true_mouse.y = self.mouse_x, self.mouse_y
end
end
def free_move
relative_y_rotation = (@yaw + 180)
relative_speed = 0.5
if InputMapper.down?(:character, :forward)
@z+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
@x-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
end
if InputMapper.down?(:character, :backward)
@z-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
@x+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
end
if InputMapper.down?(:character, :strife_left)
@z+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
@x+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
end
if InputMapper.down?(:character, :strife_right)
@z-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
@x-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
end
if InputMapper.down?(:camera, :ascend)
@y+=relative_speed
end
if InputMapper.down?(:camera, :descend)
@y-=relative_speed
end
end
def button_up(id)
if InputMapper.is?(:camera, :release_mouse, id)
@mouse_captured = false
$window.needs_cursor = true
elsif InputMapper.is?(:camera, :capture_mouse, id)
@mouse_captured = true
$window.needs_cursor = false
elsif InputMapper.is?(:camera, :increase_mouse_sensitivity, id)
@mouse_sensitivity+=1
@mouse_sensitivity = @mouse_sensitivity.clamp(1.0, 100.0)
elsif InputMapper.is?(:camera, :decrease_mouse_sensitivity, id)
@mouse_sensitivity-=1
@mouse_sensitivity = @mouse_sensitivity.clamp(1.0, 100.0)
elsif InputMapper.is?(:camera, :reset_mouse_sensitivity, id)
@mouse_sensitivity = 20.0
elsif InputMapper.is?(:camera, :increase_view_distance, id)
# @field_of_view += 1
# @field_of_view = @field_of_view.clamp(1, 100)
@view_distance += 1
@view_distance = @view_distance.clamp(1, 1000)
elsif InputMapper.is?(:camera, :decrease_view_distance, id)
# @field_of_view -= 1
# @field_of_view = @field_of_view.clamp(1, 100)
@view_distance -= 1
@view_distance = @view_distance.clamp(1, 1000)
elsif InputMapper.is?(:camera, :turn_180, id)
@rotation.y = @rotation.y+180
@rotation.y %= 360
end
end
end
end

View File

@@ -0,0 +1,170 @@
require "etc"
class IMICFPS
class Player < Entity
attr_accessor :speed
attr_reader :name, :bound_model, :first_person_view
def setup
bind_model("base", "biped")
InputMapper.set(:character, :forward, [Gosu::KbUp, Gosu::KbW])
InputMapper.set(:character, :backward, [Gosu::KbDown, Gosu::KbS])
InputMapper.set(:character, :strife_left, Gosu::KbA)
InputMapper.set(:character, :strife_right, Gosu::KbD)
InputMapper.set(:character, :turn_left, Gosu::KbLeft)
InputMapper.set(:character, :turn_right, Gosu::KbRight)
InputMapper.set(:character, :jump, Gosu::KbSpace)
InputMapper.set(:character, :sprint, [Gosu::KbLeftControl])
InputMapper.set(:character, :toggle_first_person_view, Gosu::KbF)
@speed = 2.5 # meter's per second
@running_speed = 6.8 # meter's per second
@old_speed = @speed
@mass = 72 # kg
@floor = 0
@first_person_view = true
@devisor = 500.0
@name_image = Gosu::Image.from_text("#{Etc.getlogin}", 100, font: "Consolas", align: :center)
# @name_image.save("temp.png")
@name_tex = @name_image.gl_tex_info
array_of_pixels = @name_image.to_blob
tex_names_buf = ' ' * 8
glGenTextures(1, tex_names_buf)
@name_texture_id = tex_names_buf.unpack('L2').first
glBindTexture(GL_TEXTURE_2D, @name_texture_id)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, @name_image.width, @name_image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
glGenerateMipmap(GL_TEXTURE_2D)
end
def draw_nameplate
_height = (@name_image.height/@devisor)
_width = (@name_image.width/@devisor)/2
_y = 2#normalize_bounding_box(model.bounding_box).max_y+0.05
glPushMatrix
glRotatef(180, 0, 1, 0)
glDisable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_TEXTURE_2D)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND)
glBindTexture(GL_TEXTURE_2D, @name_texture_id)
glBegin(GL_TRIANGLES)
glColor3f(1.0,1.0,1.0)
# TOP LEFT
glTexCoord2f(0, 0)
glVertex3f(0-_width,_y+_height,0)
# TOP RIGHT
glTexCoord2f(1, 0)
glVertex3f(0+_width, _y+_height,0)
# BOTTOM LEFT
glTexCoord2f(0, 1)
glVertex3f(0-_width,_y,0)
# BOTTOM LEFT
glTexCoord2f(0, 1)
glVertex3f(0-_width,_y,0)
# BOTTOM RIGHT
glTexCoord2f(1, 1)
glVertex3f(0+_width, _y,0)
# TOP RIGHT
glTexCoord2f(1, 0)
glVertex3f(0+_width,_y+_height,0)
glEnd
# glDisable(GL_BLEND)
glDisable(GL_TEXTURE_2D)
glEnable(GL_LIGHTING)
glPopMatrix
end
def draw
if !@first_person_view
super
draw_nameplate
end
end
def update
relative_speed = @speed
if InputMapper.down?(:character, :sprint)
relative_speed = (@running_speed)*(delta_time)
else
relative_speed = @speed*(delta_time)
end
relative_y_rotation = @rotation.y*-1
if InputMapper.down?(:character, :forward)
@position.z+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
@position.x-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
end
if InputMapper.down?(:character, :backward)
@position.z-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
@position.x+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
end
if InputMapper.down?(:character, :strife_left)
@position.z+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
@position.x+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
end
if InputMapper.down?(:character, :strife_right)
@position.z-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
@position.x-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
end
if InputMapper.down?(:character, :turn_left)
@rotation.y+=(relative_speed*1000)*delta_time
end
if InputMapper.down?(:character, :turn_right)
@rotation.y-=(relative_speed*1000)*delta_time
end
if @_time_in_air
air_time = ((Gosu.milliseconds-@_time_in_air)/1000.0)
@velocity.y-=(IMICFPS::GRAVITY*air_time)*delta_time
end
if InputMapper.down?(:character, :jump) && !@jumping
@jumping = true
@_time_in_air = Gosu.milliseconds
elsif !@jumping && @position.y > @floor
@falling = true
@_time_in_air ||= Gosu.milliseconds # FIXME
else
if @jumping
if @position.y <= @floor
@falling = false; @jumping = false; @velocity.y = 0; @position.y = @floor
end
end
end
if @jumping && !@falling
if InputMapper.down?(:character, :jump)
@velocity.y = 1.5
@falling = true
end
end
@position.y+=@velocity.y*delta_time if @position.y >= @floor # TEMP fix to prevent falling forever, collision/physics managers should fix this in time.
super
end
def button_up(id)
if InputMapper.is?(:character, :toggle_first_person_view, id)
@first_person_view = !@first_person_view
puts "First Person? #{@first_person_view}"
end
end
end
end

View File

@@ -0,0 +1,19 @@
class IMICFPS
class Skydome < Entity
def setup
bind_model("base", "skydome")
end
def draw
glDisable(GL_LIGHTING)
super
glEnable(GL_LIGHTING)
end
def update
@rotation.y += 0.01
@rotation.y %= 360
super
end
end
end

View File

@@ -0,0 +1,7 @@
class IMICFPS
class Terrain < Entity
def setup
bind_model("base", "randomish_terrain")
end
end
end

View File

@@ -0,0 +1,7 @@
class IMICFPS
class TestObject < Entity
def setup
bind_model("base", "war_factory")
end
end
end

View File

@@ -0,0 +1,7 @@
class IMICFPS
class Tree < Entity
def setup
bind_model("base", "tree")
end
end
end