Terrain is still broken, player moves in the correct relative directions now :)

This commit is contained in:
2018-03-23 08:54:23 -05:00
parent dfc6a0cfe8
commit 0b0d307d2a
3 changed files with 42 additions and 34 deletions

View File

@@ -20,28 +20,30 @@ class IMICFPS
relative_speed = @speed*(delta_time/60.0) relative_speed = @speed*(delta_time/60.0)
end end
relative_y_rotation = @y_rotation*-1
if button_down?(Gosu::KbUp) || button_down?(Gosu::KbW) if button_down?(Gosu::KbUp) || button_down?(Gosu::KbW)
@z-=Math.cos(@y_rotation * Math::PI / 180)*relative_speed @z+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
@x+=Math.sin(@y_rotation * Math::PI / 180)*relative_speed @x-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
end end
if button_down?(Gosu::KbDown) || button_down?(Gosu::KbS) if button_down?(Gosu::KbDown) || button_down?(Gosu::KbS)
@z+=Math.cos(@y_rotation * Math::PI / 180)*relative_speed @z-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
@x-=Math.sin(@y_rotation * Math::PI / 180)*relative_speed @x+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
end end
if button_down?(Gosu::KbA) if button_down?(Gosu::KbA)
@z-=Math.sin(@y_rotation * Math::PI / 180)*relative_speed @z+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
@x-=Math.cos(@y_rotation * Math::PI / 180)*relative_speed @x+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
end end
if button_down?(Gosu::KbD) if button_down?(Gosu::KbD)
@z+=Math.sin(@y_rotation * Math::PI / 180)*relative_speed @z-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
@x+=Math.cos(@y_rotation * Math::PI / 180)*relative_speed @x-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
end end
if button_down?(Gosu::KbLeft) if button_down?(Gosu::KbLeft)
@y_rotation-=relative_speed*100 @y_rotation+=relative_speed*100
end end
if button_down?(Gosu::KbRight) if button_down?(Gosu::KbRight)
@y_rotation+=relative_speed*100 @y_rotation-=relative_speed*100
end end
@y-=relative_speed if button_down?(Gosu::KbC) || button_down?(Gosu::KbLeftShift) unless @y <= 0 @y-=relative_speed if button_down?(Gosu::KbC) || button_down?(Gosu::KbLeftShift) unless @y <= 0

View File

@@ -1,11 +1,15 @@
class IMICFPS class IMICFPS
class Terrain class Terrain
include OpenGL include OpenGL
def initialize(size:, heightmap: nil) def initialize(size:, height: nil, width: nil, length: nil, heightmap: nil)
@size = size @size = size
@heightmap = heightmap @heightmap = heightmap
@map = [] @map = []
@height = height ? height : 1
@width = width ? width : @size
@length = length ? length : @size
@vertices = [] @vertices = []
@normals = [] @normals = []
@colors = [] @colors = []
@@ -14,18 +18,18 @@ class IMICFPS
def generate def generate
# x # x
@size.times do |x| row = []
height = 0 @width.times do |x|
# row = [] @length.times do |z|
@size.times do |z| @map << Vertex.new(x-@width.to_f/2, @height, z-@length.to_f/2)
@map << Vertex.new(x, height, z) @map << Vertex.new(x+1-@width.to_f/2, @height, z-@length.to_f/2)
@map << Vertex.new(x, height, z-1) # @map << Vertex.new(x+1, height, z)
@map << Vertex.new(x+1, height, z-1) # @map << Vertex.new(x+1, height, z-1)
#
@map << Vertex.new(x, height, z) # @map << Vertex.new(x, height, z)
@map << Vertex.new(x+1, height, z) # @map << Vertex.new(x+1, height, z)
@map << Vertex.new(x+1, height, z-1) # @map << Vertex.new(x+1, height, z-1)
# height +=0.5 # height +=0.5
end end
# @map << row # @map << row
@@ -56,18 +60,20 @@ class IMICFPS
end end
def old_draw def old_draw
height = 0
glEnable(GL_COLOR_MATERIAL) glEnable(GL_COLOR_MATERIAL)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glPointSize(5)
glBegin(GL_TRIANGLES) # glBegin(GL_LINES)
@map.each do |vertex| # glBegin(GL_POINTS)
glNormal3f(0,1,0) glBegin(GL_TRIANGLE_STRIP)
glColor3f(0.0, 0.5, 0) @map.each_with_index do |vertex, index|
glVertex3f(vertex.x, vertex.y, vertex.z) glNormal3f(0,1,0)
end glColor3f(0.0, 0.5, 0) if index.even?
glEnd glColor3f(0.5, 0, 0) if index.odd?
glVertex3f(vertex.x, vertex.y, vertex.z)
end
glEnd
glDisable(GL_COLOR_MATERIAL) glDisable(GL_COLOR_MATERIAL)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
@@ -89,7 +95,7 @@ class IMICFPS
glNormalPointer(GL_FLOAT, 0, @normals) glNormalPointer(GL_FLOAT, 0, @normals)
glColorPointer(3, GL_FLOAT, 0, @colors) glColorPointer(3, GL_FLOAT, 0, @colors)
glDrawArrays(GL_TRIANGLES, 0, @map.size/4) glDrawArrays(GL_TRIANGLE_STRIP, 0, @map.size/4)
glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY) glDisableClientState(GL_NORMAL_ARRAY)

View File

@@ -25,7 +25,7 @@ class IMICFPS
# Model.new(type: :obj, file_path: "objects/tree.obj", z: -5) # Model.new(type: :obj, file_path: "objects/tree.obj", z: -5)
# Model.new(type: :obj, file_path: "objects/tree.obj", x: -2, z: -6) # Model.new(type: :obj, file_path: "objects/tree.obj", x: -2, z: -6)
# 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) @terrain = Terrain.new(size: 20, height: 0)
@camera = Camera.new(x: 0, y: -2, z: 1) @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)