Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --number of DOWNWARDS thrusters you have on your ship
- local THRUSTERS = 8 --PUT THE NUMBER OF DOWNWARDS-FACING THRUSTERS YOU HAVE HERE
- --set to nil if not using pocket computers
- local modem = "back" --If using pocket computer control, put where your wireless modem is here
- --Make an empty table to allow any computer to control the bike
- --A list of computer ids (run "id" in the shell to get your computer id)
- local CONTROLS = {79} --A list of valid control computers, for inputting controls
- --How quickly your bike rotates
- local sensitivity = 5 --How sensitive the bike is to rotation
- --Stuff you probably dont care about
- local hover = 5
- args = {...}
- if args[1] then hover = args[1] end
- local ship = peripheral.find("shipControlInterface")
- local raycasters = {}
- local perips = peripheral.getNames()
- for i=1, #perips do
- local per = perips[i]
- if string.find(per,"raycaster",1,true) then
- table.insert(raycasters,peripheral.wrap(per))
- end
- --elseif string.find(per,"modem",1,true
- end
- if not modem or #modem < 1 then modem = false end
- if #raycasters < 1 then error("No raycasters found") end
- local mass = ship.getWeight()
- local tick = 0.001
- local maxForce = THRUSTERS*100000
- local vx,vz,theta = 0,0,0
- if modem then rednet.open(modem) end
- local function getHeight()
- local height = ship.getPosition()["y"]-63
- for i=1,#raycasters do
- local ray = raycasters[i]
- local hit = ray.raycast(hover+2,{0,0,1})
- --local hitpos = hit["hit_pos"]
- --if hit["block_type"] == "block.minecraft.air" then
- local dist = hit["distance"]
- --print("dist",dist)
- --if hitpos[2] < 63 then dis
- if dist and dist < height then height = dist end
- end
- --print("height",height)
- return height
- end
- local function getYVel()
- --local startTime = os.epoch("UTC")
- local height = getHeight()
- --dx = vt+0.5at^2
- --2*(dx-vi*t)/(t^2) = a
- --a=(vf-vi)/(t); a = (0-vi)/(t)
- local v = ship.getVelocity()["y"]
- local dx = hover-height
- --local a = 2*(dx-v*tick)/(tick*tick)
- local a = (dx-v)
- --print(v)
- a = a
- local force = (a+10)*mass
- --print(force,mass,a)
- return force/maxForce
- --local currentMvmnt = ship.getMovement()["y"]
- --local U = 10*mass*(height-hover) --Gravitational Potential Energy
- --local KE = 0.5*mass*currentVel*math.abs(currentVel) --"Kinetic Energy" but with sign for a funny
- --local E = U-- + KE
- --local sign = -1
- --if E < 0 then sign = 1 end --Dir for velocity
- --local v = sign*math.sqrt(math.abs(2*E/mass))
- --return
- end
- --print(getHeight())
- local function setVel(x,z)
- x = x or 0
- z = z or 0
- --local height = getHeight()
- local y = getYVel()
- ship.setMovement(x,y,z)
- end
- ship.rotateAlongAxis(0,1,0)
- local function setRot(angle)
- --rotation about
- local oldRot = ship.getRotation()
- local oldAng = math.acos(oldRot["w"])
- --local newRot = oldAng + angle
- local newRot = oldRot["w"] + angle
- --print("Rotation: ", newRot)
- --while newRot > 1 do
- --newRot = newRot - 1
- --end
- ship.rotateAlongAxis(0,angle,0)
- --if math.abs(math.sin(newRot/2)) < 0.001 then newRot = newRot+0.01 end
- --ship.setRotation(0,math.sin(newRot/2),0,math.cos(newRot/2))
- end
- local function update()
- while true do
- setVel(vx,vz)
- setRot(theta)
- end
- end
- local function receive()
- local isHeld,key,event = rednet.receive()
- if #CONTROLS < 1 then return isHeld,key,event end
- for i = 1, #CONTROLS do
- if isHeld == CONTROLS[i] then return isHeld,key,event end
- end
- return isHeld,key,"invalid"
- end
- local function readKeys()
- while true do
- local event,key,isHeld
- if not rednet.isOpen() then
- event,key,isHeld = os.pullEvent()
- else
- isHeld,key,event = receive()
- end
- if event == "key" then
- if key == keys.w then
- vx = 1
- elseif key == keys.s then
- vx = -1
- elseif key == keys.a then
- vz = -1
- elseif key == keys.d then
- vz = 1
- elseif key == keys.up or key == keys.right then
- theta = -0.1*sensitivity
- elseif key == keys.down or key == keys.left then
- theta = 0.1*sensitivity
- elseif key == keys.q then
- ship.setMovement(0,0,0)
- os.reboot()
- end
- elseif event == "key_up" then
- if key == keys.s or key == keys.w then
- vx = 0
- elseif key == keys.a or key == keys.d then
- vz = 0
- elseif key == keys.up or key == keys.down or key == keys.left or key == keys.right then
- theta = 0
- end
- end
- end
- end
- parallel.waitForAll( unpack({ function () update() end, function() readKeys() end}))
- --while true do
- --setVel()
- --end
- --setVel(0.5,0.5)
Advertisement
Add Comment
Please, Sign In to add comment