2014-10-18 12:49:39 +00:00
|
|
|
Class = require 'hump.class'
|
|
|
|
require 'entities/physicsentity'
|
2014-10-18 14:56:40 +00:00
|
|
|
require 'settings'
|
|
|
|
require 'entities/debug'
|
2014-10-18 16:33:32 +00:00
|
|
|
Matrix = require 'matrix'
|
2014-10-18 14:56:40 +00:00
|
|
|
VectorLight = require 'hump/vector-light'
|
2014-10-18 18:34:58 +00:00
|
|
|
require 'utils'
|
|
|
|
require 'machinegun'
|
2014-10-18 12:49:39 +00:00
|
|
|
|
2014-10-18 16:33:32 +00:00
|
|
|
local function sign(v)
|
|
|
|
if v >= 0 then
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return -1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-18 19:24:17 +00:00
|
|
|
local lift = function(angle)
|
|
|
|
return 1.68429 * math.exp(-math.pow(angle / math.pi * 180.0 -17.3801, 2.0) / (2.0 * math.pow(15.0, 2.0)))
|
|
|
|
end
|
|
|
|
|
2014-10-18 16:33:32 +00:00
|
|
|
local fwd_frict_coeff = 0.2
|
|
|
|
local nor_frict_coeff = 2.0
|
|
|
|
local tail_frict_coeff = 0.5
|
2014-10-18 20:00:26 +00:00
|
|
|
local turn_speed = 2.0
|
|
|
|
local wing_lift = 0.1 * 5
|
2014-10-18 19:24:17 +00:00
|
|
|
local accel_speed = 1000.0
|
|
|
|
local decel_speed = 2000.0
|
|
|
|
local max_motorPower = 4000.0
|
2014-10-18 20:00:26 +00:00
|
|
|
local plane_area = 10.0
|
|
|
|
local head_area = 1.0
|
2014-10-18 16:33:32 +00:00
|
|
|
|
2014-10-18 12:49:39 +00:00
|
|
|
Plane = Class{
|
|
|
|
__includes = PhysicsEntity,
|
|
|
|
|
2014-10-18 21:24:01 +00:00
|
|
|
frames = {},
|
2014-10-18 12:49:39 +00:00
|
|
|
|
2014-10-18 20:00:26 +00:00
|
|
|
motorPower = 100,
|
2014-10-18 14:56:40 +00:00
|
|
|
|
|
|
|
debugVectors = {},
|
|
|
|
|
|
|
|
turningCw = false,
|
|
|
|
turningCcw = false,
|
|
|
|
|
2014-10-18 19:24:17 +00:00
|
|
|
accelerating = false,
|
|
|
|
decelerating = false,
|
|
|
|
|
2014-10-18 12:49:39 +00:00
|
|
|
init = function(self, x, y, level)
|
2014-10-18 14:56:40 +00:00
|
|
|
local density = 50
|
2014-10-18 12:49:39 +00:00
|
|
|
PhysicsEntity.init(self, x, y, level, "dynamic", 0.2)
|
2014-10-18 20:00:26 +00:00
|
|
|
self.xsize = 5.5 * PIXELS_PER_METER
|
2014-10-18 21:24:01 +00:00
|
|
|
self.ysize = 4.0 * PIXELS_PER_METER
|
2014-10-18 12:49:39 +00:00
|
|
|
self.shape = love.physics.newRectangleShape(self.xsize, self.ysize)
|
|
|
|
PhysicsEntity.attachShape(self, density)
|
|
|
|
self.body:setX(self.x + self.xsize / 2)
|
|
|
|
self.body:setY(self.y - self.ysize / 2)
|
2014-10-18 20:00:26 +00:00
|
|
|
self.body:setLinearVelocity(100, 0)
|
|
|
|
self.body:setAngle(0)
|
2014-10-18 12:49:39 +00:00
|
|
|
self.angle = 0
|
|
|
|
|
2014-10-18 21:24:01 +00:00
|
|
|
for frame = 0,35 do
|
|
|
|
self.frames[frame] = love.graphics.newImage(string.format("resources/graphics/plane-%04d.png", frame))
|
|
|
|
end
|
|
|
|
self.quad = love.graphics.newQuad(0, 0, self.xsize, self.ysize, self.frames[0]:getWidth(), self.frames[0]:getHeight())
|
2014-10-18 18:34:58 +00:00
|
|
|
|
|
|
|
self.machinegun = MachineGun(self, "vickers77")
|
|
|
|
end;
|
|
|
|
|
|
|
|
getGunPosition = function(self)
|
|
|
|
local x = self.body:getX()
|
|
|
|
local y = self.body:getY()
|
|
|
|
|
|
|
|
local pos = rad_dist_to_xy(self.angle, self.xsize / 2)
|
|
|
|
return {x + pos[1], y + pos[2]}
|
|
|
|
end;
|
|
|
|
|
2014-10-18 19:24:17 +00:00
|
|
|
accelerate = function(self, down)
|
|
|
|
self.accelerating = down
|
|
|
|
end;
|
|
|
|
|
|
|
|
decelerate = function(self, down)
|
|
|
|
self.decelerating = down
|
|
|
|
end;
|
|
|
|
|
2014-10-18 18:34:58 +00:00
|
|
|
shoot = function(self, down)
|
|
|
|
if down then
|
|
|
|
self.machinegun:startShooting()
|
|
|
|
else
|
|
|
|
self.machinegun:stopShooting()
|
|
|
|
end
|
2014-10-18 12:49:39 +00:00
|
|
|
end;
|
|
|
|
|
2014-10-18 18:34:58 +00:00
|
|
|
update = function(self, dt)
|
2014-10-18 14:56:40 +00:00
|
|
|
debugVectors = {}
|
2014-10-18 12:49:39 +00:00
|
|
|
PhysicsEntity.update(self, dt)
|
2014-10-18 18:34:58 +00:00
|
|
|
self.machinegun:update(dt)
|
2014-10-18 12:49:39 +00:00
|
|
|
|
|
|
|
self.x, self.y = self.fixture:getBoundingBox()
|
|
|
|
self.angle = self.body:getAngle()
|
2014-10-18 14:56:40 +00:00
|
|
|
|
2014-10-18 16:33:32 +00:00
|
|
|
local vel_x, vel_y = self.body:getLinearVelocity()
|
2014-10-18 19:24:17 +00:00
|
|
|
local abs_vel = VectorLight.len(vel_x, vel_y)
|
|
|
|
|
|
|
|
if self.accelerating then
|
|
|
|
self.motorPower = math.min(max_motorPower, self.motorPower + dt * accel_speed)
|
|
|
|
end
|
|
|
|
if self.decelerating then
|
|
|
|
self.motorPower = math.max(0.0, self.motorPower - dt * accel_speed)
|
|
|
|
end
|
2014-10-18 16:33:32 +00:00
|
|
|
|
|
|
|
-- -- local base =
|
|
|
|
-- let base = V.base (V.vec_of_ang (~-(body#get_angle))) in
|
2014-10-18 19:24:17 +00:00
|
|
|
local basde = Matrix{{math.cos(-self.angle),
|
|
|
|
math.sin(-self.angle)},
|
|
|
|
{math.cos(-self.angle + math.pi / 2),
|
|
|
|
math.sin(-self.angle + math.pi / 2)}}
|
2014-10-18 16:33:32 +00:00
|
|
|
local to_base = function(x, y)
|
2014-10-18 19:24:17 +00:00
|
|
|
local m = Matrix.mul(basde, Matrix{{x}, {y}})
|
|
|
|
-- print("to_base", x, y, "->", Matrix.tostring(m), " = ", m[1][1], m[2][1])
|
|
|
|
return m
|
2014-10-18 16:33:32 +00:00
|
|
|
end
|
2014-10-18 19:24:17 +00:00
|
|
|
|
|
|
|
-- print("-")
|
|
|
|
-- print(Matrix.tostring(base))
|
2014-10-18 16:33:32 +00:00
|
|
|
|
|
|
|
local fwd_x, fwd_y = VectorLight.rotate(self.angle, 1, 0)
|
2014-10-18 19:24:17 +00:00
|
|
|
local normal_x, normal_y = VectorLight.perpendicular(fwd_x, fwd_y)
|
2014-10-18 16:33:32 +00:00
|
|
|
local fwd_vel = VectorLight.dot(vel_x, vel_y, fwd_x, fwd_y)
|
|
|
|
local normal_vel = VectorLight.dot(vel_x, vel_y, normal_x, normal_y)
|
2014-10-18 19:24:17 +00:00
|
|
|
-- print("fwd_vel", fwd_vel, "normal_vel", normal_vel, "vel_x", vel_x, "vel_y", vel_y, "normal_x", normal_x, "normal_y", normal_y)
|
|
|
|
table.insert(debugVectors, DebugVector("vel", self.body:getX(), self.body:getY(), vel_x, vel_y))
|
|
|
|
table.insert(debugVectors, DebugVector("fwd", self.body:getX(), self.body:getY(), fwd_x, fwd_y))
|
|
|
|
table.insert(debugVectors, DebugVector("normal", self.body:getX(), self.body:getY(), normal_x, normal_y))
|
2014-10-18 16:33:32 +00:00
|
|
|
|
|
|
|
local tail_speed = self.body:getAngularVelocity() * math.pi * 2.0 * self.xsize / 2.0
|
|
|
|
local tail_vel = to_base(0, tail_speed) -- hmm?! not tail_speed, 0?
|
|
|
|
local abs_tail_vel_x, abs_tail_vel_y = VectorLight.add(vel_x, vel_y, tail_vel[1][1], tail_vel[2][1])
|
2014-10-18 19:24:17 +00:00
|
|
|
table.insert(debugVectors, DebugVector("tail", self.body:getX(), self.body:getY(), tail_vel[1][1], tail_vel[2][1]))
|
2014-10-18 16:33:32 +00:00
|
|
|
--print("absolute tail veloicty: ", abs_tail_vel_x, abs_tail_vel_y)
|
|
|
|
|
|
|
|
local head_angle = self.angle
|
|
|
|
|
2014-10-18 19:24:17 +00:00
|
|
|
local rel_force = function(label, force_x, force_y, rel_at_x, rel_at_y)
|
|
|
|
-- rdx, rdy = VectorLight.add(rdx, rdy, self.body:getX(), self.body:getY())
|
|
|
|
local base_force = to_base(force_x, force_y)
|
|
|
|
local base_rel_at = to_base(rel_at_x, rel_at_y)
|
|
|
|
base_rel_at_x, base_rel_at_y = VectorLight.add(base_rel_at[1][1], base_rel_at[2][1], self.body:getX(), self.body:getY())
|
|
|
|
self.body:applyForce(base_force[1][1], base_force[2][1], base_rel_at_x, base_rel_at_y)
|
|
|
|
|
|
|
|
at_x, at_y = VectorLight.add(base_rel_at_x, base_rel_at_y, base_force[1][1], base_force[2][1])
|
|
|
|
|
|
|
|
--print(at_x, at_y)
|
|
|
|
-- print("base matrix", Matrix.tostring(base))
|
|
|
|
-- print("force", force_x, force_y)
|
|
|
|
-- print("force after transformation", Matrix.tostring(to_base(force_x, force_y)))
|
|
|
|
-- print("base_force", base_force[1][1], base_force[2][1])
|
|
|
|
-- print("base_force'", Matrix.tostring(base_force))
|
|
|
|
|
|
|
|
table.insert(debugVectors, DebugVector(label, base_rel_at_x, base_rel_at_y, base_force[1][1], base_force[2][1]))
|
2014-10-18 16:33:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local speed_angle
|
|
|
|
if abs_vel < 1.0 then
|
|
|
|
speed_angle = head_angle
|
|
|
|
else
|
|
|
|
speed_angle = math.atan2(vel_y, vel_x)
|
|
|
|
end
|
|
|
|
|
|
|
|
local air_wing_angle
|
|
|
|
local tmp
|
|
|
|
tmp = head_angle - speed_angle
|
|
|
|
if tmp > math.pi then
|
|
|
|
air_wing_angle = tmp - 2.0 * math.pi
|
|
|
|
else
|
|
|
|
air_wing_angle = tmp
|
|
|
|
end
|
|
|
|
|
|
|
|
local lift_coeff = lift(air_wing_angle)
|
|
|
|
|
2014-10-18 16:59:37 +00:00
|
|
|
-- print("lift coeff: ", lift_coeff)
|
2014-10-18 16:33:32 +00:00
|
|
|
|
|
|
|
-- motor
|
2014-10-18 19:24:17 +00:00
|
|
|
local dx, dy = VectorLight.rotate(self.angle, self.motorPower * 10.0 * PIXELS_PER_METER, 0)
|
2014-10-18 14:56:40 +00:00
|
|
|
self.body:applyForce(dx, dy);
|
2014-10-18 16:59:37 +00:00
|
|
|
-- table.insert(debugVectors, DebugVector(self.body:getX(), self.body:getY(), dx, dy))
|
2014-10-18 14:56:40 +00:00
|
|
|
|
2014-10-18 20:00:26 +00:00
|
|
|
-- (* self#add_force "fwddrag" *)
|
|
|
|
-- (* (Gg.V2.smul (fwd_frict_coeff *. fwd_vel ** 2.0 *. head_area) (V.unit (negate vel))) *)
|
|
|
|
-- (* (to_base (Gg.V2.v 0.0 0.0)); *)
|
|
|
|
local airdrag = -fwd_frict_coeff * math.pow(fwd_vel, 2.0) * head_area
|
|
|
|
rel_force("airdrag", airdrag, 0, 0, 0)
|
|
|
|
|
2014-10-18 16:33:32 +00:00
|
|
|
-- Air friction (and drag?) opposes movement towards plane velocity normal also
|
|
|
|
-- hdd drag
|
2014-10-18 20:00:26 +00:00
|
|
|
local hddrag_x, hddrag_y = VectorLight.mul(nor_frict_coeff * math.pow(normal_vel, 2.0) * plane_area * sign(normal_vel), 0, -1.0)
|
2014-10-18 16:33:32 +00:00
|
|
|
local b = to_base(-1.0, 0.0)
|
2014-10-18 19:24:17 +00:00
|
|
|
rel_force("hddrag", hddrag_x, hddrag_y, -self.xsize / 2, 0);
|
|
|
|
-- self.body:applyForce(hddrag_x, hddrag_y, b[1][1], b[2][1])
|
2014-10-18 16:33:32 +00:00
|
|
|
|
2014-10-18 20:00:26 +00:00
|
|
|
local lift_x, lift_y = VectorLight.mul(wing_lift * math.pow(fwd_vel, 2.0) * lift_coeff, 0, -1)
|
|
|
|
rel_force("lift", lift_x, lift_y, self.xsize * 0.8, 0)
|
2014-10-18 16:33:32 +00:00
|
|
|
|
2014-10-18 19:24:17 +00:00
|
|
|
-- if self.turningCcw then
|
|
|
|
-- dx, dy = VectorLight.rotate(self.angle, 0, 50000)
|
|
|
|
-- elseif self.turningCw then
|
|
|
|
-- dx, dy = VectorLight.rotate(self.angle, 0, -50000)
|
|
|
|
-- end
|
2014-10-18 14:56:40 +00:00
|
|
|
if self.turningCcw then
|
2014-10-18 20:00:26 +00:00
|
|
|
dx, dy = 0, -turn_speed
|
2014-10-18 14:56:40 +00:00
|
|
|
elseif self.turningCw then
|
2014-10-18 20:00:26 +00:00
|
|
|
dx, dy = 0, turn_speed
|
2014-10-18 19:24:17 +00:00
|
|
|
end
|
|
|
|
if self.turningCw or self.turningCcw then
|
2014-10-18 20:00:26 +00:00
|
|
|
dx, dy = VectorLight.mul(turn_speed * math.pow(fwd_vel, 2.0) * sign(fwd_vel), dx, dy)
|
2014-10-18 19:24:17 +00:00
|
|
|
-- print("dx", dx, "dy", dy, "tail_speed", tail_speed)
|
|
|
|
-- self.body:applyForce(dx, dy, self.body:getX() + rdx, self.body:getY() + rdy)
|
2014-10-18 20:00:26 +00:00
|
|
|
rel_force("turn", dx, dy, -0.4 * self.xsize, 0);
|
2014-10-18 14:56:40 +00:00
|
|
|
end
|
2014-10-18 16:59:57 +00:00
|
|
|
-- print(dx, dy)
|
2014-10-18 14:56:40 +00:00
|
|
|
|
|
|
|
-- table.insert(debugVectors, DebugVector(self.x + width / 2, self.y + height / 2, 0, -100))
|
|
|
|
-- table.insert(debugVectors, DebugVector(self.x, self.y, -100, 0))
|
|
|
|
-- table.insert(debugVectors, DebugVector(self.x + 55, self.y + 10, dx, dy)
|
2014-10-18 12:49:39 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
draw = function(self)
|
|
|
|
PhysicsEntity.draw(self)
|
2014-10-18 21:24:01 +00:00
|
|
|
love.graphics.push()
|
|
|
|
|
|
|
|
love.graphics.translate(self.body:getX(), self.body:getY())
|
|
|
|
love.graphics.scale(-1, 1)
|
|
|
|
love.graphics.draw(self.frames[0], self.quad, 0, 0, -self.angle, 1, 1, self.xsize / 2, self.ysize / 2)
|
|
|
|
love.graphics.pop()
|
2014-10-18 20:16:11 +00:00
|
|
|
drawDebug(debugVectors)
|
2014-10-18 12:49:39 +00:00
|
|
|
end;
|
2014-10-18 17:00:13 +00:00
|
|
|
|
|
|
|
cw = function(self, isTurning)
|
|
|
|
self.turningCw = isTurning
|
|
|
|
end;
|
|
|
|
|
|
|
|
ccw = function(self, isTurning)
|
|
|
|
self.turningCcw = isTurning
|
|
|
|
end;
|
2014-10-18 12:49:39 +00:00
|
|
|
}
|
|
|
|
|