Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify world stepping #126

Closed
whoiskonstantin opened this issue Jan 1, 2022 · 3 comments · Fixed by #127
Closed

Simplify world stepping #126

whoiskonstantin opened this issue Jan 1, 2022 · 3 comments · Fixed by #127

Comments

@whoiskonstantin
Copy link

Why we pass only the timeStep as an argument when we invoke .step() for the first time?

// Start the simulation loop
const timeStep = 1 / 60 // seconds
let lastCallTime
function animate() {
  requestAnimationFrame(animate)

  const time = performance.now() / 1000 // seconds
  if (!lastCallTime) { // ???
    world.step(timeStep) // ???
  } else {
    const dt = time - lastCallTime
    world.step(timeStep, dt)
  }
  lastCallTime = time
}
// Start the simulation loop
animate()

The code below "seems" to work exactly the same.

// Start the simulation loop
const timeStep = 1 / 60 // seconds
let lastCallTime = performance.now() / 1000 // seconds
function animate() {
  requestAnimationFrame(animate)
  
  const time = performance.now() / 1000 // seconds
  const dt = time - lastCallTime
  world.step(timeStep, dt)
  lastCallTime = time
}
// Start the simulation loop
animate()
@marcofugaro
Copy link
Member

Because the second argument to the step function is the time elapsed since last call. This value has to be greather than the timeStep (1 / 60), othewise the world won't step.

In your example, the world isn't stepping on the first animate() call, but it starts at the second one.

@marcofugaro marcofugaro changed the title [Question] What is the rationale behind this code in the "Getting started" example? Simplify world stepping Jan 6, 2022
@marcofugaro
Copy link
Member

We could probably do this internally

@whoiskonstantin
Copy link
Author

@marcofugaro Oh wow! You have simplified it so much! For some reason I missed the notification from your response. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants