mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 21:22:33 +00:00
Improved Vector, use named arguments for Engine initializer
This commit is contained in:
@@ -15,10 +15,10 @@ module CyberarmEngine
|
|||||||
$window.last_frame_time/1000.0
|
$window.last_frame_time/1000.0
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(width = 800, height = 600, fullscreen = false, update_interval = 1000.0/60)
|
def initialize(width: 800, height: 600, fullscreen: false, update_interval: 1000.0/60, resizable: false)
|
||||||
@show_cursor = false
|
@show_cursor = false
|
||||||
|
|
||||||
super(width, height, fullscreen, update_interval)
|
super(width, height, fullscreen: fullscreen, update_interval: update_interval, resizable: resizable)
|
||||||
$window = self
|
$window = self
|
||||||
@last_frame_time = Gosu.milliseconds-1
|
@last_frame_time = Gosu.milliseconds-1
|
||||||
@current_frame_time = Gosu.milliseconds
|
@current_frame_time = Gosu.milliseconds
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
module CyberarmEngine
|
module CyberarmEngine
|
||||||
class Vector
|
class Vector
|
||||||
|
|
||||||
def initialize(x = 0, y = 0, z = 0, weight = 0)
|
def initialize(x = 0, y = 0, z = 0, weight = 0)
|
||||||
@x, @y, @z, @weight = x, y, z, weight
|
@x, @y, @z, @weight = x, y, z, weight
|
||||||
end
|
end
|
||||||
@@ -17,10 +16,6 @@ module CyberarmEngine
|
|||||||
def weight; @weight; end
|
def weight; @weight; end
|
||||||
def weight=(n); @weight = n; end
|
def weight=(n); @weight = n; end
|
||||||
|
|
||||||
# def xy=(nx, ny); @x = nx; @y = ny; end
|
|
||||||
# def xyz=(nx, ny, nz); @x = nx; @y = ny; @z = nz; end
|
|
||||||
# def xyzw=(nx, ny, nz, nw); @x = nx; @y = ny; @z = nz; @weight = nw; end
|
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
if other.is_a?(Numeric)
|
if other.is_a?(Numeric)
|
||||||
@x == other &&
|
@x == other &&
|
||||||
@@ -35,43 +30,85 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def +(other)
|
# Performs math operation, excluding @weight
|
||||||
Vector.new(
|
def operator(function, other)
|
||||||
@x + other.x,
|
if other.is_a?(Numeric)
|
||||||
@y + other.y,
|
Vector.new(
|
||||||
@z + other.z,
|
@x.send(:"#{function}", other),
|
||||||
@weight + other.weight
|
@y.send(:"#{function}", other),
|
||||||
)
|
@z.send(:"#{function}", other)
|
||||||
end
|
)
|
||||||
|
else
|
||||||
def -(other)
|
Vector.new(
|
||||||
Vector.new(
|
@x.send(:"#{function}", other.x),
|
||||||
@x - other.x,
|
@y.send(:"#{function}", other.y),
|
||||||
@y - other.y,
|
@z.send(:"#{function}", other.z)
|
||||||
@z - other.z,
|
|
||||||
@weight - other.weight
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def *(other)
|
|
||||||
Vector.new(
|
|
||||||
@x * other.x,
|
|
||||||
@y * other.y,
|
|
||||||
@z * other.z,
|
|
||||||
@weight * other.weight
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Adds Vector and Numberic or Vector and Vector, excluding @weight
|
||||||
|
def +(other)
|
||||||
|
operator("+", other)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Subtracts Vector and Numberic or Vector and Vector, excluding @weight
|
||||||
|
def -(other)
|
||||||
|
operator("-", other)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Multiplies Vector and Numberic or Vector and Vector, excluding @weight
|
||||||
|
def *(other)
|
||||||
|
operator("*", other)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Divides Vector and Numberic or Vector and Vector, excluding @weight
|
||||||
def /(other)
|
def /(other)
|
||||||
# Endeavors to prevent division by zero
|
# Duplicated to protect from DivideByZero
|
||||||
|
if other.is_a?(Numeric)
|
||||||
|
Vector.new(
|
||||||
|
(@x == 0 ? 0 : @x / other),
|
||||||
|
(@y == 0 ? 0 : @y / other),
|
||||||
|
(@z == 0 ? 0 : @z / other)
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Vector.new(
|
||||||
|
(@x == 0 ? 0 : @x / other.x),
|
||||||
|
(@y == 0 ? 0 : @y / other.y),
|
||||||
|
(@z == 0 ? 0 : @z / other.z)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def dot(other)
|
||||||
|
product = 0
|
||||||
|
|
||||||
|
a = self.to_a
|
||||||
|
b = other.to_a
|
||||||
|
|
||||||
|
3.times do |i|
|
||||||
|
product = product + (a[i] * b[i])
|
||||||
|
end
|
||||||
|
|
||||||
|
return product
|
||||||
|
end
|
||||||
|
|
||||||
|
def cross(other)
|
||||||
|
a = self.to_a
|
||||||
|
b = other.to_a
|
||||||
|
|
||||||
Vector.new(
|
Vector.new(
|
||||||
@x == 0 || other.x == 0 ? 0 : @x / other.x,
|
b[2] * a[1] - b[1] * a[2],
|
||||||
@y == 0 || other.y == 0 ? 0 : @y / other.y,
|
b[0] * a[2] - b[2] * a[0],
|
||||||
@z == 0 || other.z == 0 ? 0 : @z / other.z,
|
b[1] * a[0] - b[0] * a[1]
|
||||||
@weight == 0 || other.weight == 0 ? 0 : @weight / other.weight
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# returns degrees
|
||||||
|
def angle(other)
|
||||||
|
Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI
|
||||||
|
end
|
||||||
|
|
||||||
# returns magnitude of Vector, ignoring #weight
|
# returns magnitude of Vector, ignoring #weight
|
||||||
def magnitude
|
def magnitude
|
||||||
Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
|
Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
|
||||||
@@ -82,8 +119,38 @@ module CyberarmEngine
|
|||||||
self / Vector.new(mag, mag, mag)
|
self / Vector.new(mag, mag, mag)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def direction
|
||||||
|
# z is pitch
|
||||||
|
# y is yaw
|
||||||
|
# x is roll
|
||||||
|
_x = -Math.sin(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
||||||
|
_y = Math.sin(@z.degrees_to_radians)
|
||||||
|
_z = Math.cos(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
||||||
|
|
||||||
|
Vector.new(_x, _y, _z)
|
||||||
|
end
|
||||||
|
|
||||||
|
def inverse
|
||||||
|
Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
|
||||||
|
end
|
||||||
|
|
||||||
def sum
|
def sum
|
||||||
@x + @y + @z + @weight
|
@x + @y + @z
|
||||||
|
end
|
||||||
|
|
||||||
|
# 2D distance using X and Y
|
||||||
|
def distance(other)
|
||||||
|
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
|
||||||
|
end
|
||||||
|
|
||||||
|
# 2D distance using X and Z
|
||||||
|
def gl_distance2d(other)
|
||||||
|
Math.sqrt((@x-other.x)**2 + (@z-other.z)**2)
|
||||||
|
end
|
||||||
|
|
||||||
|
# 3D distance using X, Y, and Z
|
||||||
|
def distance3d(other)
|
||||||
|
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2 + (@z-other.z)**2)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_a
|
def to_a
|
||||||
|
|||||||
Reference in New Issue
Block a user