Guest User

speedbike.lua

a guest
Feb 1st, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.99 KB | None | 0 0
  1. --number of DOWNWARDS thrusters you have on your ship
  2. local THRUSTERS = 8 --PUT THE NUMBER OF DOWNWARDS-FACING THRUSTERS YOU HAVE HERE
  3.  
  4. --set to nil if not using pocket computers
  5. local modem = "back" --If using pocket computer control, put where your wireless modem is here
  6.  
  7. --Make an empty table to allow any computer to control the bike
  8. --A list of computer ids (run "id" in the shell to get your computer id)
  9. local CONTROLS = {79} --A list of valid control computers, for inputting controls
  10.  
  11. --How quickly your bike rotates
  12. local sensitivity = 5 --How sensitive the bike is to rotation
  13.  
  14.  
  15. --Stuff you probably dont care about
  16. local hover = 5
  17. args = {...}
  18. if args[1] then hover = args[1] end
  19. local ship = peripheral.find("shipControlInterface")
  20. local raycasters = {}
  21. local perips = peripheral.getNames()
  22. for i=1, #perips do
  23.     local per = perips[i]
  24.     if string.find(per,"raycaster",1,true) then
  25.         table.insert(raycasters,peripheral.wrap(per))
  26.     end
  27.     --elseif string.find(per,"modem",1,true
  28. end
  29. if not modem or #modem < 1 then modem = false end
  30. if #raycasters < 1 then error("No raycasters found") end
  31. local mass = ship.getWeight()
  32. local tick = 0.001
  33. local maxForce = THRUSTERS*100000
  34. local vx,vz,theta = 0,0,0
  35. if modem then rednet.open(modem) end
  36.  
  37. local function getHeight()
  38.     local height = ship.getPosition()["y"]-63
  39.     for i=1,#raycasters do
  40.         local ray = raycasters[i]
  41.         local hit = ray.raycast(hover+2,{0,0,1})
  42.         --local hitpos = hit["hit_pos"]
  43.         --if hit["block_type"] == "block.minecraft.air" then
  44.         local dist = hit["distance"]
  45.         --print("dist",dist)
  46.         --if hitpos[2] < 63 then dis
  47.         if dist and dist < height then height = dist end
  48.     end
  49.     --print("height",height)
  50.     return height
  51. end
  52.  
  53. local function getYVel()
  54.     --local startTime = os.epoch("UTC")
  55.     local height = getHeight()
  56.     --dx = vt+0.5at^2
  57.     --2*(dx-vi*t)/(t^2) = a
  58.     --a=(vf-vi)/(t); a = (0-vi)/(t)
  59.     local v = ship.getVelocity()["y"]
  60.     local dx = hover-height
  61.     --local a = 2*(dx-v*tick)/(tick*tick)
  62.     local a = (dx-v)
  63.     --print(v)
  64.     a = a
  65.     local force = (a+10)*mass
  66.     --print(force,mass,a)
  67.     return force/maxForce
  68.     --local currentMvmnt = ship.getMovement()["y"]
  69.     --local U = 10*mass*(height-hover) --Gravitational Potential Energy
  70.     --local KE = 0.5*mass*currentVel*math.abs(currentVel) --"Kinetic Energy" but with sign for a funny
  71.     --local E = U-- + KE
  72.     --local sign = -1
  73.     --if E < 0 then sign = 1 end --Dir for velocity
  74.     --local v = sign*math.sqrt(math.abs(2*E/mass))
  75.     --return
  76. end
  77. --print(getHeight())
  78. local function setVel(x,z)
  79.     x = x or 0
  80.     z = z or 0
  81.     --local height = getHeight()
  82.     local y = getYVel()
  83.     ship.setMovement(x,y,z)
  84. end
  85.  
  86. ship.rotateAlongAxis(0,1,0)
  87. local function setRot(angle)
  88.     --rotation about
  89.     local oldRot = ship.getRotation()
  90.     local oldAng = math.acos(oldRot["w"])
  91.     --local newRot = oldAng + angle
  92.     local newRot = oldRot["w"] + angle
  93.     --print("Rotation: ", newRot)
  94.     --while newRot > 1 do
  95.         --newRot = newRot - 1
  96.     --end
  97.     ship.rotateAlongAxis(0,angle,0)
  98.     --if math.abs(math.sin(newRot/2)) < 0.001 then newRot = newRot+0.01 end
  99.     --ship.setRotation(0,math.sin(newRot/2),0,math.cos(newRot/2))
  100. end
  101.  
  102. local function update()
  103.     while true do
  104.         setVel(vx,vz)
  105.         setRot(theta)
  106.     end
  107. end
  108.  
  109. local function receive()
  110.     local isHeld,key,event = rednet.receive()
  111.     if #CONTROLS < 1 then return isHeld,key,event end
  112.     for i = 1, #CONTROLS do
  113.         if isHeld == CONTROLS[i] then return isHeld,key,event end
  114.     end
  115.     return isHeld,key,"invalid"
  116. end
  117.  
  118. local function readKeys()
  119.     while true do
  120.         local event,key,isHeld
  121.         if not rednet.isOpen() then
  122.             event,key,isHeld = os.pullEvent()
  123.         else
  124.             isHeld,key,event = receive()
  125.         end
  126.         if event == "key" then
  127.             if key == keys.w then
  128.                 vx = 1
  129.             elseif key == keys.s then
  130.                 vx = -1
  131.             elseif key == keys.a then
  132.                 vz = -1
  133.             elseif key == keys.d then
  134.                 vz = 1
  135.             elseif key == keys.up or key == keys.right then
  136.                 theta = -0.1*sensitivity
  137.             elseif key == keys.down or key == keys.left then
  138.                 theta = 0.1*sensitivity
  139.             elseif key == keys.q then
  140.                 ship.setMovement(0,0,0)
  141.                 os.reboot()
  142.             end
  143.         elseif event == "key_up" then
  144.             if key == keys.s or key == keys.w then
  145.                 vx = 0
  146.             elseif key == keys.a or key == keys.d then
  147.                 vz = 0
  148.             elseif key == keys.up or key == keys.down or key == keys.left or key == keys.right then
  149.                 theta = 0
  150.             end
  151.         end
  152.     end
  153. end
  154.  
  155. parallel.waitForAll( unpack({ function () update() end, function() readKeys() end}))
  156.  
  157. --while true do
  158.     --setVel()
  159. --end
  160.  
  161. --setVel(0.5,0.5)
  162.    
Advertisement
Add Comment
Please, Sign In to add comment