Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Made by DasEtwas
- All rights reserved
- ]]--
- -- global variables --
- airDensity = 2.75
- updateSleep = 80
- maxAcceleration = 100 -- m/s² max acceleration
- movementSleep = 10 -- how many ticks it takes for previously completely still wings to activate when velocity:length() > sleepVel
- sleepVel = 0.5 -- m/s
- -- utils --
- function vecString(vec)
- return "["..tostring(math.floor((sm.vec3.getX(vec) * 1000) + 0.5) / 1000)..",".. tostring(math.floor((sm.vec3.getY(vec) * 1000) + 0.5) / 1000)..",".. tostring(math.floor((sm.vec3.getZ(vec) * 1000) + 0.5) / 1000).."]"
- end
- function form(num)
- return math.floor((num * 100) + 0.5) / 100
- end
- function sign(num)
- if num < 0 then
- return -1
- elseif num > 0 then
- return 1
- else
- return 0
- end
- end
- function equals(vec1, vec2)
- return sm.vec3.getX(vec1) == sm.vec3.getX(vec2) and sm.vec3.getY(vec1) == sm.vec3.getY(vec2) and sm.vec3.getZ(vec1) == sm.vec3.getZ(vec2)
- end
- function doAirfoilStuff( self, timeStep )
- -- check if lastPosition was set AND
- -- check if after taking the creation from the lift the shape's worldpos isnt wrong AND
- -- check if something was built onto the creation, which would screw up the shape's position
- if self.lastPosition then
- local currentPos = sm.shape.getWorldPosition(self.shape)
- if equals(currentPos, self.lastBodyPos) or (self.parentBodyMass ~= sm.body.getMass(self.shape:getBody())) then
- self.enabled = -updateSleep
- end
- --print("currentPos: " .. vecString(currentPos) .. ", lastBodyPos: " .. vecString(self.lastBodyPos) .. ", enabled: " .. self.enabled)
- local globalVel = (currentPos - self.lastPosition) / timeStep
- local globalVelL = globalVel:length()
- if (globalVelL < sleepVel) then
- self.sleep = movementSleep
- elseif self.sleep > 0 then
- self.sleep = self.sleep - 1
- end
- local acceleration = globalVelL
- if self.lastVelocity then
- acceleration = (globalVelL - self.lastVelocity) / timeStep
- end
- self.lastVelocity = globalVelL
- if self.enabled >= 0 and self.sleep == 0 then
- if math.abs(acceleration) < maxAcceleration then
- --print("lastPos: " .. vecString(self.lastPosition) .. ", currentPos: " .. vecString(sm.shape.getWorldPosition(self.shape)))
- --print("parent body pos: " .. vecString(sm.body.getPosition(self.shape:getBody())))
- local localX = sm.shape.getRight(self.shape)
- local localY = sm.shape.getUp(self.shape)
- local localZ = localX:cross(localY) -- normal vector
- self.normalVel = globalVel:dot(localZ)
- self.lift = airDensity * self.normalVel * self.normalVel * 0.5 * self.area * sign(self.normalVel)
- --print("mass: " .. tostring(sm.body.getMass(self.shape:getBody())))
- --print("velocity: " .. vecString(globalVel) .. ", enabled: " .. tostring(self.enabled))
- sm.physics.applyImpulse(self.shape, sm.vec3.new(0, self.lift, 0)) -- minus for other direction than velocity
- else
- print("too much acceleration: " .. tostring(form(acceleration)))
- --print("globalVelL: " .. tostring(form(globalVelL)))
- end
- --print("acceleration: " .. tostring(form(acceleration)))
- else
- self.enabled = self.enabled + 1
- end
- end
- self.lastBodyPos = sm.body.getPosition(self.shape:getBody())
- self.lastPosition = sm.shape.getWorldPosition(self.shape)
- self.parentBodyMass = sm.body.getMass(self.shape:getBody())
- end
- -- Airfoil1 --
- Airfoil1 = class( nil )
- Airfoil1.area = 1 -- m², in this case 4 * 4 blocks / 16 (conversion unit)
- Airfoil1.enabled = -updateSleep -- prevent jitter from false lastPosition
- Airfoil1.sleep = movementSleep
- function Airfoil1.server_onCreate( self )
- self:server_init()
- end
- function Airfoil1.server_init( self )
- self.enabled = -updateSleep
- end
- function Airfoil1.server_onRefresh( self )
- self:server_init()
- end
- function Airfoil1.server_onFixedUpdate( self, timeStep )
- doAirfoilStuff(self, timeStep)
- end
- -- Airfoil2 --
- Airfoil2 = class( nil )
- Airfoil2.area = 0.25 -- m², in this case 4 * 4 blocks / 16 (conversion unit)
- Airfoil2.enabled = -updateSleep -- prevent jitter from false lastPosition
- Airfoil2.sleep = movementSleep
- function Airfoil2.server_onCreate( self )
- self:server_init()
- end
- function Airfoil2.server_init( self )
- self.enabled = -updateSleep
- end
- function Airfoil2.server_onRefresh( self )
- self:server_init()
- end
- function Airfoil2.server_onFixedUpdate( self, timeStep )
- doAirfoilStuff(self, timeStep)
- end
- -- end of script --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement