Kaynağa Gözat

Physics example updates;

bjorn 1 yıl önce
ebeveyn
işleme
d8d45f26a8

+ 2 - 2
api/init.lua

@@ -36051,7 +36051,7 @@ return {
               description = "Sets the inertia of the Collider.\n\nInertia is kind of like \"angular mass\".  Regular mass determines how resistant the Collider is to linear forces (movement), whereas inertia determines how resistant the Collider is to torque (rotation).  Colliders with less inertia are more spinny.\n\nIn 3D, inertia is represented by a 3x3 matrix, called a tensor.  To make calculations easier, the physics engine stores the inertia using eigenvalue decomposition, splitting the matrix into a diagonal matrix and a rotation.  It's complicated!\n\nIn a realistic simulation, mass and inertia follow a linear relationship.  If the mass of an object increases, the diagonal part of its inertia should increase proportionally.",
               key = "Collider:setInertia",
               module = "lovr.physics",
-              notes = "By default, the inertia of the Collider is kept up to date automatically as the Collider's shapes change.  To disable this, use `Collider:setAutomaticMass`.\n\nUse `Collider:resetMassData` to reset the inertia and other mass properties based on the Collider's shapes.\n\nIf the Collider is kinematic or all rotation axes are disabled, this returns 0 for the diagonal and an identity quaternion for the rotation.",
+              notes = "By default, the inertia of the Collider is kept up to date automatically as the Collider's shapes change.  To disable this, use `Collider:setAutomaticMass`.\n\nUse `Collider:resetMassData` to reset the inertia and other mass properties based on the Collider's shapes.\n\nIf the Collider is kinematic or all rotation axes are disabled, the collider behaves as though it has infinite inertia, and this function will do nothing.",
               related = {
                 "Collider:getMass",
                 "Collider:setMass",
@@ -40082,7 +40082,7 @@ return {
               name = "newCollider",
               tag = "colliders",
               summary = "Add a Collider to the World.",
-              description = "Adds a new Collider to the World.\n\nThe Collider won't have any shapes attached to it.",
+              description = "Adds a new Collider to the World, without attaching any Shapes to it.  Use `Collider:addShape` to add shapes.",
               key = "World:newCollider",
               module = "lovr.physics",
               examples = {

+ 2 - 3
api/lovr/physics/World/newCollider.lua

@@ -2,9 +2,8 @@ return {
   tag = 'colliders',
   summary = 'Add a Collider to the World.',
   description = [[
-    Adds a new Collider to the World.
-
-    The Collider won't have any shapes attached to it.
+    Adds a new Collider to the World, without attaching any Shapes to it.  Use `Collider:addShape`
+    to add shapes.
   ]],
   arguments = {
     x = {

+ 1 - 1
examples/Effects/Billboards/main.lua

@@ -88,7 +88,7 @@ function lovr.draw(pass)
   pass:setMaterial(puffMaterial)
   pass:setDepthWrite(false)
   pass:setShader(redPuffShader)
-  pass:send('headPosition', lovr.headset.getPosition('head'))
+  pass:send('headPosition', { lovr.headset.getPosition('head') })
   for _, puff in pairs(redPuffs) do
     pass:plane(puff.position, .1)
   end

+ 3 - 3
examples/Interaction/Physics_Pointer/main.lua

@@ -28,10 +28,10 @@ function lovr.update(dt)
 
   local ox, oy, oz = lovr.headset.getPosition('hand/left/point')
   local dx, dy, dz = quat(lovr.headset.getOrientation('hand/left/point')):direction():mul(50):unpack()
-  local collider, x, y, z = world:raycast(ox, oy, oz, ox + dx, oy + dy, oz + dz)
+  local collider, shape, x, y, z = world:raycast(ox, oy, oz, ox + dx, oy + dy, oz + dz)
 
-  if shape then
-    selectedBox = shape:getCollider()
+  if collider then
+    selectedBox = collider
     hitpoint:set(x, y, z)
   end
 end

+ 7 - 7
examples/Locomotion/Teleportation_Colliders/main.lua

@@ -32,8 +32,8 @@ function motion.teleport(dt)
   local target = vec3(handPose:mul(0, 0, -motion.teleportDistance))
   local intersectionDistance = math.huge
   local closestHit
-  physicsWorld:raycast(origin, target,
-    function(shape, x, y, z, nx, ny, nz)
+  physicsWorld:raycast(origin, target, nil,
+    function(collider, shape, x, y, z, nx, ny, nz)
       local position = vec3(x, y, z)
       local normal = vec3(nx, ny, nz)
       local distance = (origin - position):length()
@@ -106,13 +106,13 @@ end
 local function makeColumn(x, z, height, color)
   height = math.abs(height)
   local collider = physicsWorld:newCylinderCollider(x, height / 2, z, 2, height)
-  local shape = collider:getShapes()[1]
+  local shape = collider:getShape()
   collider:setOrientation(math.pi/2,  1,0,0)
   collider:setKinematic(true)
   local column = {
-    collider=collider,
-    shape=shape,
-    color=color,
+    collider = collider,
+    shape = shape,
+    color = color,
   }
   table.insert(columns, column)
 end
@@ -159,7 +159,7 @@ function lovr.draw(pass)
   for i, column in ipairs(columns) do
     local x,y,z, angle, ax,ay,az = column.collider:getPose()
     local l, r = column.shape:getLength(), column.shape:getRadius()
-    pass:setColor(unpack(column.color))
+    pass:setColor(column.color)
     pass:cylinder(x,y,z, r,l, angle, ax,ay,az, true, 0,2*math.pi, 20)
   end
   -- Teleportation curve and target, rendering of blinking overlay

+ 3 - 4
examples/Physics/Boxes/main.lua

@@ -1,10 +1,10 @@
 function lovr.load()
   world = lovr.physics.newWorld()
-  world:setLinearDamping(.01)
-  world:setAngularDamping(.005)
 
   -- Create the ground
-  world:newBoxCollider(0, 0, 0, 50, .05, 50):setKinematic(true)
+  ground = world:newBoxCollider(0, -1, 0, 50, 2, 50)
+  ground:setKinematic(true)
+  ground:setFriction(1.0)
 
   -- Create boxes!
   boxes = {}
@@ -12,7 +12,6 @@ function lovr.load()
     for y = .125, 2, .2499 do
       local box = world:newBoxCollider(x, y, -2 - y / 5, .25)
       table.insert(boxes, box)
-      box:setFriction(.8)
     end
   end
 

+ 8 - 7
examples/Physics/Newtons_Cradle/main.lua

@@ -13,23 +13,24 @@ local gap = 0.01
 
 
 function lovr.load()
-  world = lovr.physics.newWorld(0, -9.8, 0, false)
+  world = lovr.physics.newWorld({ restitutionThreshold = .05 })
   -- a static geometry from which balls are suspended
   local size = vec3(1.2, 0.1, 0.3)
-  frame = world:newBoxCollider(vec3(0, 2, -1), size)
+  frame = world:newBoxCollider(vec3(0, 2, -2), size)
   frame:setKinematic(true)
   framePose = lovr.math.newMat4(frame:getPose()):scale(size)
   -- create balls along the length of frame and attach them with two distance joints to frame
   for x = -0.5, 0.5, 1 / count do
-    local ball = world:newSphereCollider(vec3(x, 1, -1), radius - gap)
-    ball:setRestitution(1)
-    lovr.physics.newDistanceJoint(frame, ball, vec3(x, 2, -1 + 0.25), vec3(x, 1, -1))
-    lovr.physics.newDistanceJoint(frame, ball, vec3(x, 2, -1 - 0.25), vec3(x, 1, -1))
+    local ball = world:newSphereCollider(vec3(x, 1, -2), radius - gap)
+    ball:setRestitution(1.0)
     table.insert(balls, ball)
+
+    lovr.physics.newDistanceJoint(frame, ball, vec3(x, 2, -2 + 0.25), vec3(x, 1, -2))
+    lovr.physics.newDistanceJoint(frame, ball, vec3(x, 2, -2 - 0.25), vec3(x, 1, -2))
   end
   -- displace the last ball to set the Newton's cradle in motion
   local lastBall = balls[#balls]
-  lastBall:setPosition(vec3(lastBall:getPosition()) + vec3(5 * radius, 5 * radius, 0))
+  lastBall:applyLinearImpulse(.6, 0, 0)
   lovr.graphics.setBackgroundColor(0.1, 0.1, 0.1)
 end