Added untested plane physics

This commit is contained in:
Erkki Seppälä 2014-10-18 19:33:32 +03:00
parent cf15237fd7
commit 2bca8ace54
3 changed files with 1366 additions and 0 deletions

View file

@ -2,8 +2,26 @@ Class = require 'hump.class'
require 'entities/physicsentity' require 'entities/physicsentity'
require 'settings' require 'settings'
require 'entities/debug' require 'entities/debug'
Matrix = require 'matrix'
VectorLight = require 'hump/vector-light' VectorLight = require 'hump/vector-light'
local function sign(v)
if v >= 0 then
return 1
else
return -1
end
end
local fwd_frict_coeff = 0.2
local nor_frict_coeff = 2.0
local tail_frict_coeff = 0.5
local head_area = 1.0
local plane_area = 10.0
local tail_area = 0.2
local turn_speed = 0.2
local wing_lift = 0.1
Plane = Class{ Plane = Class{
__includes = PhysicsEntity, __includes = PhysicsEntity,
@ -38,10 +56,72 @@ Plane = Class{
self.x, self.y = self.fixture:getBoundingBox() self.x, self.y = self.fixture:getBoundingBox()
self.angle = self.body:getAngle() self.angle = self.body:getAngle()
local vel_x, vel_y = self.body:getLinearVelocity()
local abs_vel = VectorLight.len2(vel_x, vel_y)
-- -- local base =
-- let base = V.base (V.vec_of_ang (~-(body#get_angle))) in
local base = Matrix{{math.cos(-self.angle), math.sin(-self.angle)}, {math.cos(-self.angle + math.pi / 2), math.sin(-self.angle + math.pi / 2)}}
local to_base = function(x, y)
return Matrix.mul(Matrix{{x}, {y}}, base)
end
local fwd_x, fwd_y = VectorLight.rotate(self.angle, 1, 0)
local normal_x, normal_y = VectorLight.rotate(self.angle, 0, 1)
local normal_unit_x, normal_unit_y = VectorLight.div(VectorLight.len(normal_x, normal_y), normal_x, normal_y)
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)
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])
--print("absolute tail veloicty: ", abs_tail_vel_x, abs_tail_vel_y)
local head_angle = self.angle
local rel_force = function(label, force, at)
self.body:applyForce(force, at)
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 = function(angle)
return 1.68429 * math.exp(-math.pow(angle / math.pi * 180.0 -17.3801, 2.0) / math.pow(2.0*15.0, 2.0))
end
local lift_coeff = lift(air_wing_angle)
print("lift coeff: ", lift_coeff)
-- motor
local dx, dy = VectorLight.rotate(self.angle, self.motor_power * 10.0 * PIXELS_PER_METER, 0) local dx, dy = VectorLight.rotate(self.angle, self.motor_power * 10.0 * PIXELS_PER_METER, 0)
self.body:applyForce(dx, dy); self.body:applyForce(dx, dy);
table.insert(debugVectors, DebugVector(self.body:getX(), self.body:getY(), dx, dy)) table.insert(debugVectors, DebugVector(self.body:getX(), self.body:getY(), dx, dy))
-- Air friction (and drag?) opposes movement towards plane velocity normal also
-- hdd drag
local hddrag_x, hddrag_y = VectorLight.mul(nor_frict_coeff * math.pow(normal_vel, 2.0) * sign(normal_vel), -normal_unit_x, -normal_unit_y)
-- self.body:applyForce()
local b = to_base(-1.0, 0.0)
self.body:applyForce(hddrag_x, hddrag_y, b[1][1], b[2][1])
local lift_x, lift_y = VectorLight.mul(wing_lift * math.pow(fwd_vel, 2.0) * lift_coeff, normal_unit_x, normal_unit_y)
self.body:applyForce(lift_x, lift_y, self.body:getX(), self.body:getY())
if self.turningCcw then if self.turningCcw then
dx, dy = VectorLight.rotate(self.angle - math.pi / 2, 1000, 0) dx, dy = VectorLight.rotate(self.angle - math.pi / 2, 1000, 0)
elseif self.turningCw then elseif self.turningCw then

35
matrix.LICENSE Normal file
View file

@ -0,0 +1,35 @@
LuaMatrix License
-----------
LuaMatrix ( http://luamatrix.luaforge.net/ ) is licensed under the
same terms as Lua (MIT license) reproduced below. This means that
LuaMatrix is free software and can be used for both academic and
commercial purposes at absolutely no cost.
For details and rationale, see http://www.lua.org/license.html .
===============================================================================
Copyright (C) 2007-2010 Michael Lutz.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================================================
(end of COPYRIGHT)

1251
matrix.lua Normal file

File diff suppressed because it is too large Load diff