Advertisement
Descaii

ogm physiks working

Mar 10th, 2014
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | None | 0 0
  1.  
  2.  
  3.  
  4. Points = {}
  5. function GetDistance(P1,P2)
  6.     return math.sqrt((P1.X-P2.X)^2 + (P1.Y-P2.Y)^2)
  7. end
  8. Links = {}
  9. Mouse = game.Players.LocalPlayer:GetMouse()
  10. function Point(UD1,UD2)
  11.     local X=UD1.X-UD2.X
  12.     local Y=UD1.Y-UD2.Y
  13.     local Hyp=math.sqrt(X^2+Y^2)
  14.     local AS=math.atan2(Y,X)
  15.     return AS
  16. end
  17. function NewLink(Point1,Point2,Dis,Stiffness)
  18.     if Point1 and Point2 then else return end
  19.     local B = {}
  20.     B.Dis = Dis or 50
  21.     B.Stiffness = Stiffness or 0.5
  22.     B.P1 = Point1
  23.     B.P2 = Point2
  24.     function B:Update()
  25.         local P = B.P1.Position
  26.         local P2 = B.P2.Position
  27.         local PT1 = Point(P2,P)
  28.         local PT2 = Point(P,P2)
  29.         local Off1 = Vector2.new(math.cos(PT1),math.sin(PT1))
  30.         local Off2 = Vector2.new(math.cos(PT2),math.sin(PT2))
  31.         local Dis = GetDistance(P,P2)
  32.         if not B.P2.Locked then
  33.             B.P2.Position = B.P2.Position -((Off1*(Dis-B.Dis))*B.Stiffness)
  34.             B.P2.Velocity = B.P2.Velocity -(B.P2.LastPos-B.P2.Position)
  35.             B.P2.LastPos = B.P2.Position
  36.             B.P2.Velocity = B.P2.Velocity*0.95
  37.         end
  38.         if not B.P1.Locked then
  39.             B.P1.Position = B.P1.Position -((Off2*(Dis-B.Dis))*B.Stiffness)
  40.             B.P1.Velocity = B.P1.Velocity -(B.P1.LastPos-B.P1.Position)
  41.             B.P1.LastPos = B.P1.Position
  42.             B.P1.Velocity = B.P1.Velocity*0.95
  43.         end
  44.     end
  45.     table.insert(Links,B)
  46. end
  47. function MakePoint(Pos,Parent,Mass)
  48.     local A = {}
  49.     A.Position = Pos ~= nil and Pos or Vector2.new(0,0)
  50.     A.Velocity = Vector2.new(0,0)
  51.     A.Locked = false
  52.     A.Dragging = false
  53.     A.Draggable = true
  54.     A.UI = Instance.new("TextButton",Parent)
  55.     A.UI.Text = #Points+1
  56.     A.UI.Size = UDim2.new(0,10,0,10)
  57.     A.LastPos = A.Position
  58.     A.Mass = Mass or 10
  59.     function A:Update()
  60.         if A.Dragging then
  61.             A.Position = Vector2.new(Mouse.X,Mouse.Y)
  62.         end
  63.         if not A.Locked then
  64.             local Z = Parent.AbsoluteSize
  65.             A.Position = A.Position + A.Velocity
  66.             if (A.Position.Y-10) < Z.Y then
  67.                 --print("Drop")
  68.                 A.Velocity = Vector2.new(0,A.Mass)
  69.             else
  70.                 --print("Bounce")
  71.                 A.Position = Vector2.new(A.Position.X,Z.Y-10)
  72.                 A.Velocity = Vector2.new(A.Velocity.X,-A.Velocity.Y*0.5)
  73.             end
  74.             --A.Velocity = A.Velocity -(A.LastPos-A.Position)
  75.         end
  76.         --A.UI.Position = UDim2.new(0,A.Position.X,0,A.Position.Y)
  77.     end
  78.     A.UI.MouseButton1Down:connect(function()
  79.         if A.Draggable then
  80.             A.Dragging = true
  81.         end
  82.     end)
  83.     A.UI.MouseButton1Up:connect(function()
  84.         A.Dragging = false
  85.     end)
  86.     table.insert(Points,A)
  87.     return A
  88. end
  89. local B = Instance.new("ScreenGui",game.Players.LocalPlayer:WaitForChild("PlayerGui"))
  90. SS = 8
  91. SizePerPixel = 50
  92. StartPos = Vector2.new(300,50)
  93. for y = 0,SS-1 do
  94.     for x = 0,SS-1 do
  95.         local S = MakePoint(StartPos+Vector2.new(x*SizePerPixel,y*SizePerPixel),B,10)
  96.         if x ~= 0 then
  97.             NewLink(S,Points[#Points-1],SizePerPixel)
  98.         end
  99.         if y ~= 0 then
  100.             NewLink(S,Points[(#Points- SS)],SizePerPixel)
  101.         end
  102.         if y == 0 then
  103.             S.Locked = true
  104.         end
  105.     end
  106. end
  107.  
  108. --[[for i = 1,10 do
  109.     local S = MakePoint(Vector2.new(B.AbsoluteSize.X/2,50*i),B)
  110.     if i > 1 then
  111.         NewLink(S,Points[i-1],20,0.5)
  112.     end
  113.     if i == 1 then
  114.         S.Locked = true
  115.     end
  116. end]]
  117.  
  118. --[[local A = MakePoint(Vector2.new(500,500),B)
  119. A.Locked = true
  120. local C = MakePoint(Vector2.new(500,600),B)
  121. C:AddLink(A,100,1)
  122. ]]
  123.  
  124.  
  125. --[[local X1,X2,X3 = MakePoint(Vector2.new(400,50),B),MakePoint(Vector2.new(500,50),B),MakePoint(Vector2.new(600,50),B)
  126. X1.Locked,X2.Locked,X3.Locked = true,true,true
  127. local Y1,Y2,Y3 = MakePoint(Vector2.new(400,150),B),MakePoint(Vector2.new(500,150),B),MakePoint(Vector2.new(600,150),B)
  128. local Y4,Y5,Y6 = MakePoint(Vector2.new(400,250),B),MakePoint(Vector2.new(500,250),B),MakePoint(Vector2.new(600,250),B)
  129. NewLink(X1,Y1,100)NewLink(X2,Y2,100)NewLink(X3,Y3,100)
  130. NewLink(Y1,Y4,100)NewLink(Y2,Y5,100)NewLink(Y3,Y6,100)
  131. NewLink(Y1,Y2,100)NewLink(Y2,Y3,100)
  132. NewLink(Y4,Y5,100)NewLink(Y5,Y6,100)]]
  133.  
  134. --[[local A = MakePoint(Vector2.new(500,500),B)
  135. A.Locked = true
  136. local C = MakePoint(Vector2.new(500,600),B)
  137. NewLink(A,C,100,1)]]
  138.  
  139.  
  140. Mouse.Button1Up:connect(function()
  141.     for i,v in pairs(Points) do
  142.         v.Dragging = false
  143.     end
  144. end)
  145. game:GetService("RunService").RenderStepped:connect(function()
  146.     --print("P:",C.Position,' ',' V:',C.Velocity)
  147.     for i,v in pairs(Points) do
  148.         v:Update()
  149.     end
  150.     for i,v in pairs(Links) do
  151.         v:Update()
  152.     end
  153.     for i,v in pairs(Points) do
  154.         v.UI.Position = UDim2.new(0,v.Position.X,0,v.Position.Y)
  155.     end
  156. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement