Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add time step contol based on maximum velocity.

  • Loading branch information...
commit b52aded6f4d829dac9038e6afba3faf29009e1aa 1 parent 93a4579
@endJunction authored
Showing with 27 additions and 7 deletions.
  1. +1 −1  cbits/bullet.cpp
  2. +2 −2 src/BulletFFI.hs
  3. +24 −4 src/Simulation.hs
View
2  cbits/bullet.cpp
@@ -78,7 +78,7 @@ void plStepSimulation(plDynamicsWorldHandle world, plReal timeStep)
{
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
btAssert(dynamicsWorld);
- dynamicsWorld->stepSimulation(timeStep);
+ dynamicsWorld->stepSimulation(timeStep, 0);
}
void plAddRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object)
View
4 src/BulletFFI.hs
@@ -71,8 +71,8 @@ foreign import ccall safe "plCreateDynamicsWorld" plCreateDynamicsWorld_
--
-- Step simulation.
--
-plStepSimulation :: PlDynamicsWorldHandle -> IO ()
-plStepSimulation dw = plStepSimulation_ dw 1.0
+plStepSimulation :: PlDynamicsWorldHandle -> Double -> IO ()
+plStepSimulation dw dt = plStepSimulation_ dw $ realToFrac dt
foreign import ccall safe "plStepSimulation" plStepSimulation_
:: PlDynamicsWorldHandle -> CFloat -> IO ()
View
28 src/Simulation.hs
@@ -112,19 +112,38 @@ updateLastMovingTime gs time =
) gs
freezeOldStaticGrains :: Int -> [Grain] -> IO Int
-freezeOldStaticGrains time gs = do
+freezeOldStaticGrains age gs = do
mapM_ (plMakeRigidBodyStatic . collisionObject) grainsToFreeze
return $ length grainsToFreeze
- where grainsToFreeze = filter ((time >) . lastMovingTime) gs
+ where grainsToFreeze = filter ((age >) . lastMovingTime) gs
+
+-- Simulation time step is choosen, such that maximum travel distance is less
+-- than the maxTravelDistance
+maxTravelDistance :: Double
+maxTravelDistance = 0.05
+
+timeStep :: [Double] -> Double
+timeStep vs = max minDT $ min maxDT $ maxTravelDistance / maxVelocity
+ where
+ minDT, maxDT :: Double
+ minDT = 1e-4
+ maxDT = 1e-2
+ maxVelocity :: Double
+ maxVelocity = if vs == [] then 0 else maximum vs
stepSimulation :: State -> IO Bool
stepSimulation s = do
+ velocities <- mapM (fmap magnitude . plGetVelocity . collisionObject)
+ =<< readMVar (grains s)
+ let dt = timeStep velocities
+
-- Compute next simulation timestep.
- plStepSimulation (dworld s)
+ plStepSimulation (dworld s) dt
modifyMVar_ (simulationStep s) $ return . (1+)
velocities <- mapM (fmap magnitude . plGetVelocity . collisionObject)
=<< readMVar (grains s)
+
staticGrains <- markStaticGrains =<< readMVar (grains s)
height <- computeGrainsHeight staticGrains
@@ -149,7 +168,8 @@ stepSimulation s = do
$ createNewGrain s (height+Config.generateGrainsOffset + 2*Config.grainsSizeMean)
when (Config.verbose) $ putStr $
- "total/moving/frozen/maxV/height:\t" ++
+ "dt/total/moving/frozen/maxV/height:\t" ++
+ show dt ++ "\t" ++
show totalGrains ++ "\t" ++
show nMovingGrains ++ "\t" ++
show nFrozenGrains ++ "\t" ++
Please sign in to comment.
Something went wrong with that request. Please try again.