mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-16 08:02:36 +00:00
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:
@@ -4,10 +4,10 @@ class IMICFPS
|
||||
include OpenGL
|
||||
include GLU
|
||||
|
||||
attr_accessor :x,:y,:z, :field_of_view, :pitch, :yaw, :roll, :mouse_sensitivity
|
||||
attr_reader :game_object, :broken_mouse_centering
|
||||
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)
|
||||
@x,@y,@z = x,y,z
|
||||
@position = Vector.new(x,y,z)
|
||||
@pitch = 0.0
|
||||
@yaw = 0.0
|
||||
@roll = 0.0
|
||||
@@ -15,7 +15,7 @@ class IMICFPS
|
||||
@view_distance = view_distance
|
||||
@constant_pitch = 20.0
|
||||
|
||||
@game_object = nil
|
||||
@entity = nil
|
||||
@distance = 4
|
||||
@origin_distance = @distance
|
||||
|
||||
@@ -29,6 +29,7 @@ class IMICFPS
|
||||
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)
|
||||
@@ -36,13 +37,13 @@ class IMICFPS
|
||||
InputMapper.set(:camera, :decrease_view_distance, Gosu::MsWheelDown)
|
||||
end
|
||||
|
||||
def attach_to(game_object)
|
||||
raise "Not a game object!" unless game_object.is_a?(GameObject)
|
||||
@game_object = game_object
|
||||
def attach_to(entity)
|
||||
raise "Not a game object!" unless entity.is_a?(Entity)
|
||||
@entity = entity
|
||||
end
|
||||
|
||||
def detach
|
||||
@game_object = nil
|
||||
@entity = nil
|
||||
end
|
||||
|
||||
def distance_from_object
|
||||
@@ -58,23 +59,23 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def position_camera
|
||||
if defined?(@game_object.first_person_view)
|
||||
if @game_object.first_person_view
|
||||
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(@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
|
||||
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 - @game_object.y_rotation
|
||||
@game_object.y_rotation = -@yaw + 180
|
||||
# @yaw = 180 - @entity.y_rotation
|
||||
@entity.rotation.y = -@yaw + 180
|
||||
end
|
||||
|
||||
def draw
|
||||
@@ -86,22 +87,22 @@ class IMICFPS
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
|
||||
glRotatef(@pitch,1,0,0)
|
||||
glRotatef(@yaw,0,1,0)
|
||||
glTranslatef(-@x, -@y, -@z)
|
||||
glTranslatef(-@position.x, -@position.y, -@position.z)
|
||||
glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored.
|
||||
glLoadIdentity
|
||||
|
||||
# if $debug && @game_object
|
||||
# if $debug && @entity
|
||||
# glBegin(GL_LINES)
|
||||
# glColor3f(1,0,0)
|
||||
# glVertex3f(@x, @y, @z)
|
||||
# glVertex3f(@game_object.x, @game_object.y, @game_object.z)
|
||||
# glVector3f(@x, @y, @z)
|
||||
# glVector3f(@entity.x, @entity.y, @entity.z)
|
||||
# glEnd
|
||||
# end
|
||||
end
|
||||
|
||||
def update
|
||||
if @mouse_captured
|
||||
position_camera if @game_object
|
||||
position_camera if @entity
|
||||
|
||||
delta = Float(@true_mouse.x-self.mouse_x)/(@mouse_sensitivity*@field_of_view)*70
|
||||
@yaw -= delta
|
||||
@@ -110,8 +111,8 @@ class IMICFPS
|
||||
@pitch -= Float(@true_mouse.y-self.mouse_y)/(@mouse_sensitivity*@field_of_view)*70
|
||||
@pitch = @pitch.clamp(-90.0, 90.0)
|
||||
|
||||
@game_object.y_rotation += delta if @game_object
|
||||
free_move unless @game_object
|
||||
@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
|
||||
@@ -176,6 +177,9 @@ class IMICFPS
|
||||
# @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
|
||||
@@ -1,7 +1,7 @@
|
||||
require "etc"
|
||||
|
||||
class IMICFPS
|
||||
class Player < GameObject
|
||||
class Player < Entity
|
||||
|
||||
attr_accessor :speed
|
||||
attr_reader :name, :bound_model, :first_person_view
|
||||
@@ -17,14 +17,12 @@ class IMICFPS
|
||||
InputMapper.set(:character, :jump, Gosu::KbSpace)
|
||||
InputMapper.set(:character, :sprint, [Gosu::KbLeftControl])
|
||||
|
||||
InputMapper.set(:character, :turn_180, Gosu::KbX)
|
||||
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
|
||||
@y_velocity = 0
|
||||
@floor = 0
|
||||
@first_person_view = true
|
||||
|
||||
@@ -99,8 +97,6 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def update
|
||||
@floor = @terrain.height_at(self, 4.5)
|
||||
|
||||
relative_speed = @speed
|
||||
if InputMapper.down?(:character, :sprint)
|
||||
relative_speed = (@running_speed)*(delta_time)
|
||||
@@ -108,73 +104,64 @@ class IMICFPS
|
||||
relative_speed = @speed*(delta_time)
|
||||
end
|
||||
|
||||
relative_y_rotation = @y_rotation*-1
|
||||
relative_y_rotation = @rotation.y*-1
|
||||
|
||||
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
|
||||
@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)
|
||||
@z-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@x+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@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)
|
||||
@z+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@x+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@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)
|
||||
@z-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@x-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@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)
|
||||
@y_rotation+=(relative_speed*1000)*delta_time
|
||||
@rotation.y+=(relative_speed*1000)*delta_time
|
||||
end
|
||||
if InputMapper.down?(:character, :turn_right)
|
||||
@y_rotation-=(relative_speed*1000)*delta_time
|
||||
@rotation.y-=(relative_speed*1000)*delta_time
|
||||
end
|
||||
|
||||
if @_time_in_air
|
||||
air_time = ((Gosu.milliseconds-@_time_in_air)/1000.0)
|
||||
@y_velocity-=(IMICFPS::GRAVITY*air_time)*delta_time
|
||||
@velocity.y-=(IMICFPS::GRAVITY*air_time)*delta_time
|
||||
end
|
||||
|
||||
if InputMapper.down?(:character, :jump) && !@jumping
|
||||
@jumping = true
|
||||
@_time_in_air = Gosu.milliseconds
|
||||
elsif !@jumping && @y > @floor
|
||||
elsif !@jumping && @position.y > @floor
|
||||
@falling = true
|
||||
@_time_in_air ||= Gosu.milliseconds # FIXME
|
||||
else
|
||||
if @jumping
|
||||
if @y <= @floor
|
||||
@falling = false; @jumping = false; @y_velocity = 0
|
||||
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)
|
||||
@y_velocity = 1.5
|
||||
@velocity.y = 1.5
|
||||
@falling = true
|
||||
end
|
||||
end
|
||||
|
||||
@y+=@y_velocity*delta_time
|
||||
|
||||
@y = @floor if @y < @floor
|
||||
# distance = 2.0
|
||||
# x_offset = distance * Math.cos(@bound_model.y_rotation)
|
||||
# z_offset = distance * Math.sin(@bound_model.y_rotation)
|
||||
@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, :turn_180, id)
|
||||
@y_rotation = @y_rotation+180
|
||||
@y_rotation %= 360
|
||||
|
||||
elsif InputMapper.is?(:character, :toggle_first_person_view, id)
|
||||
if InputMapper.is?(:character, :toggle_first_person_view, id)
|
||||
@first_person_view = !@first_person_view
|
||||
puts "First Person? #{@first_person_view}"
|
||||
end
|
||||
@@ -1,5 +1,5 @@
|
||||
class IMICFPS
|
||||
class Skydome < GameObject
|
||||
class Skydome < Entity
|
||||
def setup
|
||||
bind_model("base", "skydome")
|
||||
end
|
||||
@@ -11,8 +11,8 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def update
|
||||
@y_rotation+=0.01
|
||||
@y_rotation%=360
|
||||
@rotation.y += 0.01
|
||||
@rotation.y %= 360
|
||||
super
|
||||
end
|
||||
end
|
||||
7
lib/objects/entities/terrain.rb
Normal file
7
lib/objects/entities/terrain.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class IMICFPS
|
||||
class Terrain < Entity
|
||||
def setup
|
||||
bind_model("base", "randomish_terrain")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,5 @@
|
||||
class IMICFPS
|
||||
class TestObject < GameObject
|
||||
class TestObject < Entity
|
||||
def setup
|
||||
bind_model("base", "war_factory")
|
||||
end
|
||||
7
lib/objects/entities/tree.rb
Normal file
7
lib/objects/entities/tree.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class IMICFPS
|
||||
class Tree < Entity
|
||||
def setup
|
||||
bind_model("base", "tree")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,39 +2,43 @@ class IMICFPS
|
||||
|
||||
|
||||
# A game object is any renderable thing
|
||||
class GameObject
|
||||
class Entity
|
||||
include OpenGL
|
||||
include GLU
|
||||
include CommonMethods
|
||||
attr_accessor :x, :y, :z, :scale
|
||||
attr_accessor :scale
|
||||
attr_accessor :visible, :renderable, :backface_culling
|
||||
attr_accessor :x_rotation, :y_rotation, :z_rotation
|
||||
attr_reader :model, :name, :debug_color, :terrain, :width, :height, :depth, :last_x, :last_y, :last_z, :normalized_bounding_box
|
||||
def initialize(x: 0, y: 0, z: 0, bound_model: nil, scale: MODEL_METER_SCALE, backface_culling: true, auto_manage: true, terrain: nil, game_state: nil)
|
||||
@x,@y,@z,@scale = x,y,z,scale
|
||||
attr_reader :position, :rotation, :velocity
|
||||
attr_reader :model, :name, :debug_color, :width, :height, :depth, :last_x, :last_y, :last_z, :normalized_bounding_box
|
||||
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)
|
||||
@scale = scale
|
||||
@bound_model = bound_model
|
||||
@backface_culling = backface_culling
|
||||
@visible = true
|
||||
@renderable = true
|
||||
@x_rotation,@y_rotation,@z_rotation = 0,0,0
|
||||
@debug_color = Color.new(0.0, 1.0, 0.0)
|
||||
@terrain = terrain
|
||||
@rotation = Vector.new(0, 0, 0)
|
||||
@velocity = Vector.new(0, 0, 0)
|
||||
|
||||
@game_state = game_state
|
||||
@debug_color = Color.new(0.0, 1.0, 0.0)
|
||||
|
||||
@collidable = [:static, :dynamic]
|
||||
@collision = :static # :dynamic, moves in response, :static, does not move ever, :none, entities can pass through
|
||||
@physics = false
|
||||
@mass = 100 # kg
|
||||
|
||||
@width, @height, @depth = 0,0,0
|
||||
@delta_time = Gosu.milliseconds
|
||||
@last_x, @last_y, @last_z = @x, @y, @z
|
||||
@last_position = Vector.new(@position.x, @position.y, @position.z)
|
||||
|
||||
game_state.add_object(self) if auto_manage && game_state
|
||||
setup
|
||||
|
||||
if @bound_model
|
||||
@bound_model.model.game_object = self
|
||||
@bound_model.model.entity = self
|
||||
@bound_model.model.objects.each {|o| o.scale = self.scale}
|
||||
@normalized_bounding_box = normalize_bounding_box_with_offset(model.bounding_box)
|
||||
@normalized_bounding_box = normalize_bounding_box_with_offset
|
||||
|
||||
box = normalize_bounding_box(@bound_model.model.bounding_box)
|
||||
box = normalize_bounding_box
|
||||
@width = box.max_x-box.min_x
|
||||
@height = box.max_y-box.min_y
|
||||
@depth = box.max_z-box.min_z
|
||||
@@ -43,16 +47,20 @@ class IMICFPS
|
||||
return self
|
||||
end
|
||||
|
||||
def collidable?
|
||||
@collidable.include?(@collision)
|
||||
end
|
||||
|
||||
def bind_model(package, name)
|
||||
model = ModelLoader.new(manifest_file: IMICFPS.assets_path + "/#{package}/#{name}/#{name}.yaml", game_object: @dummy_game_object)
|
||||
model = ModelLoader.new(manifest_file: IMICFPS.assets_path + "/#{package}/#{name}/#{name}.yaml", entity: @dummy_entity)
|
||||
|
||||
raise "model isn't a model!" unless model.is_a?(ModelLoader)
|
||||
@bound_model = model
|
||||
@bound_model.model.game_object = self
|
||||
@bound_model.model.entity = self
|
||||
@bound_model.model.objects.each {|o| o.scale = self.scale}
|
||||
@normalized_bounding_box = normalize_bounding_box_with_offset(@bound_model.model.bounding_box)
|
||||
@normalized_bounding_box = normalize_bounding_box_with_offset
|
||||
|
||||
box = normalize_bounding_box(@bound_model.model.bounding_box)
|
||||
box = normalize_bounding_box
|
||||
@width = box.max_x-box.min_x
|
||||
@height = box.max_y-box.min_y
|
||||
@depth = box.max_z-box.min_z
|
||||
@@ -79,7 +87,7 @@ class IMICFPS
|
||||
@delta_time = Gosu.milliseconds
|
||||
|
||||
unless at_same_position?
|
||||
@normalized_bounding_box = normalize_bounding_box_with_offset(@bound_model.model.bounding_box) if model
|
||||
@normalized_bounding_box = normalize_bounding_box_with_offset if model
|
||||
end
|
||||
|
||||
@last_x, @last_y, @last_z = @x, @y, @z
|
||||
@@ -90,27 +98,31 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def at_same_position?
|
||||
@x == @last_x &&
|
||||
@y == @last_y &&
|
||||
@z == @last_z
|
||||
@position == @last_position
|
||||
end
|
||||
|
||||
# Do two Axis Aligned Bounding Boxes intersect?
|
||||
def intersect(a, b)
|
||||
a = a.normalized_bounding_box
|
||||
b = b.normalized_bounding_box
|
||||
def intersect(other)
|
||||
me = normalized_bounding_box
|
||||
other = other.normalized_bounding_box
|
||||
|
||||
# puts "bounding boxes match!" if a == b
|
||||
if (a.min_x <= b.max_x && a.max_x >= b.min_x) &&
|
||||
(a.min_y <= b.max_y && a.max_y >= b.min_y) &&
|
||||
(a.min_z <= b.max_z && a.max_z >= b.min_z)
|
||||
if (me.min_x <= other.max_x && me.max_x >= other.min_x) &&
|
||||
(me.min_y <= other.max_y && me.max_y >= other.min_y) &&
|
||||
(me.min_z <= other.max_z && me.max_z >= other.min_z)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_bounding_box(box)
|
||||
def distance(vertex, other)
|
||||
return Math.sqrt((vertex.x-other.x)**2 + (vertex.y-other.y)**2 + (vertex.z-other.z)**2)
|
||||
end
|
||||
|
||||
def normalize_bounding_box
|
||||
box = @bound_model.model.bounding_box
|
||||
|
||||
temp = BoundingBox.new
|
||||
temp.min_x = box.min_x.to_f*scale
|
||||
temp.min_y = box.min_y.to_f*scale
|
||||
@@ -123,15 +135,17 @@ class IMICFPS
|
||||
return temp
|
||||
end
|
||||
|
||||
def normalize_bounding_box_with_offset(box)
|
||||
temp = BoundingBox.new
|
||||
temp.min_x = box.min_x.to_f*scale+x
|
||||
temp.min_y = box.min_y.to_f*scale+y
|
||||
temp.min_z = box.min_z.to_f*scale+z
|
||||
def normalize_bounding_box_with_offset
|
||||
box = @bound_model.model.bounding_box
|
||||
|
||||
temp.max_x = box.max_x.to_f*scale+x
|
||||
temp.max_y = box.max_y.to_f*scale+y
|
||||
temp.max_z = box.max_z.to_f*scale+z
|
||||
temp = BoundingBox.new
|
||||
temp.min_x = box.min_x.to_f*scale+@position.x
|
||||
temp.min_y = box.min_y.to_f*scale+@position.y
|
||||
temp.min_z = box.min_z.to_f*scale+@position.z
|
||||
|
||||
temp.max_x = box.max_x.to_f*scale+@position.x
|
||||
temp.max_y = box.max_y.to_f*scale+@position.y
|
||||
temp.max_z = box.max_z.to_f*scale+@position.z
|
||||
|
||||
return temp
|
||||
end
|
||||
@@ -1,172 +0,0 @@
|
||||
class IMICFPS
|
||||
class Terrain < GameObject
|
||||
def setup
|
||||
bind_model("base", "randomish_terrain")
|
||||
# bind_model(ModelLoader.new(type: :obj, file_path: "/home/cyberarm/Documents/blends/untitled.obj", game_object: self))
|
||||
self.scale = 1
|
||||
@nearest_vertex_lookup = {}
|
||||
|
||||
generate_optimized_lists
|
||||
end
|
||||
|
||||
def generate_optimized_lists
|
||||
x_slot,y_slot = 0,0
|
||||
model.vertices.each do |vert|
|
||||
x_slot = vert.x.round
|
||||
y_slot = vert.y.round
|
||||
|
||||
@nearest_vertex_lookup[x_slot] = {} unless @nearest_vertex_lookup[x_slot]
|
||||
@nearest_vertex_lookup[x_slot][y_slot] = [] unless @nearest_vertex_lookup[x_slot][y_slot]
|
||||
@nearest_vertex_lookup[x_slot][y_slot] << vert
|
||||
end
|
||||
end
|
||||
|
||||
def height_at(vertex, max_distance = Float::INFINITY)
|
||||
if vert = find_nearest_vertex(vertex, max_distance)
|
||||
return vert.y
|
||||
else
|
||||
-1
|
||||
end
|
||||
end
|
||||
|
||||
def find_nearest_vertex(vertex, max_distance)
|
||||
nearest = nil
|
||||
smaller_list = []
|
||||
smaller_list << @nearest_vertex_lookup.dig(vertex.x.round-1, vertex.y.round-1)
|
||||
smaller_list << @nearest_vertex_lookup.dig(vertex.x.round, vertex.y.round)
|
||||
smaller_list << @nearest_vertex_lookup.dig(vertex.x.round+1, vertex.y.round+1)
|
||||
smaller_list.flatten!
|
||||
|
||||
smaller_list.each do |vert|
|
||||
next if vert.nil?
|
||||
if nearest
|
||||
if distance(vert, vertex) < distance(vert, nearest) && distance(vert, vertex) <= max_distance
|
||||
nearest = vert
|
||||
end
|
||||
end
|
||||
|
||||
nearest = vert unless nearest && distance(vert, vertex) > max_distance
|
||||
end
|
||||
|
||||
return nearest
|
||||
end
|
||||
|
||||
def distance(vertex, other)
|
||||
return Math.sqrt((vertex.x-other.x)**2 + (vertex.y-other.y)**2 + (vertex.z-other.z)**2)
|
||||
end
|
||||
end
|
||||
end
|
||||
# class IMICFPS
|
||||
# class Terrain
|
||||
# TILE_SIZE = 0.5
|
||||
# include OpenGL
|
||||
# def initialize(size:, height: nil, width: nil, length: nil, heightmap: nil)
|
||||
# @size = size
|
||||
# @heightmap = heightmap
|
||||
# @map = []
|
||||
|
||||
# @height = height ? height : 1
|
||||
# @width = width ? width : @size
|
||||
# @length = length ? length : @size
|
||||
|
||||
# @vertices = []
|
||||
# @normals = []
|
||||
# @colors = []
|
||||
# generate
|
||||
# end
|
||||
|
||||
# def generate
|
||||
# #@width.times do |x|
|
||||
# # @length.times do |z|
|
||||
# # # TRIANGLE STRIP (BROKEN)
|
||||
# # @map << Vertex.new((x+1)-@width.to_f/2, 0, z-@legth.to_f/2)
|
||||
# # @map << Vertex.new(x-@width.to_f/2, 0, (z+1)-@length.to_f/2)
|
||||
# # end
|
||||
# #end
|
||||
# @width.times do |x|
|
||||
# @length.times do |z|
|
||||
# # WORKING TRIANGLES
|
||||
# @map << Vertex.new(x-@width.to_f/2, @height, z-@length.to_f/2)
|
||||
# @map << Vertex.new((x+1)-@width.to_f/2, @height, z-@length.to_f/2)
|
||||
# @map << Vertex.new(x-@width.to_f/2, @height, (z+1)-@length.to_f/2)
|
||||
# #
|
||||
# @map << Vertex.new(x-@width.to_f/2, @height, (z+1)-@length.to_f/2)
|
||||
# @map << Vertex.new((x+1)-@width.to_f/2, @height, z-@length.to_f/2)
|
||||
# @map << Vertex.new((x+1)-@width.to_f/2, @height, (z+1)-@length.to_f/2)
|
||||
# end
|
||||
# end
|
||||
|
||||
# @map.size.times do |i|
|
||||
# @vertices << @map[i].x
|
||||
# @vertices << @map[i].y
|
||||
# @vertices << @map[i].z
|
||||
# normal = Vertex.new(0,1,0)
|
||||
# @normals << normal.x
|
||||
# @normals << normal.y
|
||||
# @normals << normal.z
|
||||
# color = Color.new(rand(0.10..0.30),0,0)
|
||||
# @colors << color.red
|
||||
# @colors << color.green
|
||||
# @colors << color.blue
|
||||
# end
|
||||
|
||||
# @vertices_packed = @vertices.pack("f*")
|
||||
# @normals_packed = @normals.pack("f*")
|
||||
# @colors_packed = @colors.pack("f*")
|
||||
# end
|
||||
|
||||
# def draw
|
||||
# new_draw
|
||||
# # old_draw
|
||||
# end
|
||||
|
||||
# def old_draw
|
||||
# glEnable(GL_COLOR_MATERIAL)
|
||||
|
||||
# # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
||||
# glPointSize(5)
|
||||
# # glBegin(GL_LINES)
|
||||
# # glBegin(GL_POINTS)
|
||||
# glBegin(GL_TRIANGLES)
|
||||
# @map.each_with_index do |vertex, index|
|
||||
# glNormal3f(0,1,0)
|
||||
# glColor3f(0.0, 0.5, 0) if index.even?
|
||||
# glColor3f(0, 1.0, 0) if index.odd?
|
||||
# glVertex3f(vertex.x, vertex.y, vertex.z)
|
||||
# end
|
||||
# glEnd
|
||||
|
||||
# glDisable(GL_COLOR_MATERIAL)
|
||||
# glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
||||
# end
|
||||
|
||||
# def new_draw
|
||||
# glEnable(GL_NORMALIZE)
|
||||
# glPushMatrix
|
||||
|
||||
# glEnable(GL_COLOR_MATERIAL)
|
||||
# glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
|
||||
# glShadeModel(GL_FLAT)
|
||||
# glEnableClientState(GL_VERTEX_ARRAY)
|
||||
# glEnableClientState(GL_NORMAL_ARRAY)
|
||||
# glEnableClientState(GL_COLOR_ARRAY)
|
||||
|
||||
# glVertexPointer(3, GL_FLOAT, 0, @vertices_packed)
|
||||
# glNormalPointer(GL_FLOAT, 0, @normals_packed)
|
||||
# glColorPointer(3, GL_FLOAT, 0, @colors_packed)
|
||||
|
||||
# glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
||||
# glDrawArrays(GL_TRIANGLES, 0, @vertices.size/3)
|
||||
# glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
|
||||
|
||||
# # glDrawArrays(GL_TRIANGLE_STRIP, 0, @vertices.size/3)
|
||||
# $window.number_of_faces+=@vertices.size/3
|
||||
|
||||
# glDisableClientState(GL_VERTEX_ARRAY)
|
||||
# glDisableClientState(GL_NORMAL_ARRAY)
|
||||
# glDisableClientState(GL_COLOR_ARRAY)
|
||||
|
||||
# glPopMatrix
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
@@ -1,20 +0,0 @@
|
||||
class IMICFPS
|
||||
class Tree < GameObject
|
||||
def setup
|
||||
bind_model("base", "tree")
|
||||
vert = @terrain.find_nearest_vertex(self, 4.5)
|
||||
if vert
|
||||
self.x = vert.x
|
||||
self.y = vert.y
|
||||
self.z = vert.z
|
||||
end
|
||||
|
||||
# @y_rotation += rand(1..100)
|
||||
end
|
||||
|
||||
# def update
|
||||
# super
|
||||
# @y_rotation+=0.005
|
||||
# end
|
||||
end
|
||||
end
|
||||
@@ -4,9 +4,9 @@ class IMICFPS
|
||||
attr_reader :ambient, :diffuse, :specular, :position, :light_id
|
||||
attr_accessor :x, :y, :z, :intensity
|
||||
def initialize(x:,y:,z:, game_state:,
|
||||
ambient: Vertex.new(0.5, 0.5, 0.5, 1),
|
||||
diffuse: Vertex.new(1, 0.5, 0, 1), specular: Vertex.new(0.2, 0.2, 0.2, 1),
|
||||
position: Vertex.new(x, y, z, 0), intensity: 1)
|
||||
ambient: Vector.new(0.5, 0.5, 0.5, 1),
|
||||
diffuse: Vector.new(1, 0.5, 0, 1), specular: Vector.new(0.2, 0.2, 0.2, 1),
|
||||
position: Vector.new(x, y, z, 0), intensity: 1)
|
||||
@x,@y,@z = x,y,z
|
||||
@game_state = game_state
|
||||
@intensity = intensity
|
||||
|
||||
@@ -8,7 +8,7 @@ class IMICFPS
|
||||
|
||||
attr_reader :model, :name, :debug_color
|
||||
|
||||
def initialize(manifest_file:, game_object: nil)
|
||||
def initialize(manifest_file:, entity: nil)
|
||||
@manifest = YAML.load(File.read(manifest_file))
|
||||
# pp @manifest
|
||||
@file_path = File.expand_path("./../model/", manifest_file) + "/#{@manifest["model"]}"
|
||||
@@ -23,7 +23,7 @@ class IMICFPS
|
||||
unless load_model_from_cache
|
||||
case @type
|
||||
when :obj
|
||||
@model = Wavefront::Model.new(file_path: @file_path, game_object: game_object)
|
||||
@model = Wavefront::Model.new(file_path: @file_path, entity: entity)
|
||||
else
|
||||
raise "Unsupported model type, supported models are: #{@supported_models.join(', ')}"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user