Code Monkey home page Code Monkey logo

Comments (8)

fian46 avatar fian46 commented on May 10, 2024

hi i think it is unfair if i do not provide some test here the test.
let me know if i am doing wrong

`
import org.dyn4j.dynamics.Body;
import org.dyn4j.dynamics.BodyFixture;
import org.dyn4j.dynamics.World;
import org.dyn4j.geometry.Convex;
import org.dyn4j.geometry.Geometry;
import org.dyn4j.geometry.MassType;
import org.dyn4j.geometry.Vector2;
import org.junit.Test;

/**
*
*/
public class UpdateTest {

@Test
public void test() {
    System.out.println("start");
    World w = new World();
    w.setGravity(World.ZERO_GRAVITY);
    Body player = new Body();
    Convex c = Geometry.createCircle(1.0);
    BodyFixture bf = new BodyFixture(c);
    bf.setDensity(1);
    player.addFixture(bf);
    player.setLinearVelocity(new Vector2(10, 0.0));
    player.setAngularVelocity(0.0);
    player.setMass(MassType.NORMAL);
    player.setAutoSleepingEnabled(false);

    w.addBody(player);
    for (int i = 0; i < 10; i++) {
        w.update(0.100);
        System.out.println(player.getWorldCenter());
    }
}

@Test
public void test1() {
    System.out.println("start");
    World w = new World();
    w.setGravity(World.ZERO_GRAVITY);
    Body player = new Body();
    Convex c = Geometry.createCircle(1.0);
    BodyFixture bf = new BodyFixture(c);
    bf.setDensity(1);
    player.addFixture(bf);
    player.setLinearVelocity(new Vector2(10, 0.0));
    player.setAngularVelocity(0.0);
    player.setMass(MassType.NORMAL);
    player.setAutoSleepingEnabled(false);

    w.addBody(player);
    for (int i = 0; i < 10; i++) {
        w.update(0.010);
        System.out.println(player.getWorldCenter());
    }
}

}
`
the result

start
(0.16666666666666666, 0.0)
(0.3333333333333333, 0.0)
(0.5, 0.0)
(0.6666666666666666, 0.0)
(0.8333333333333333, 0.0)
(0.9999999999999999, 0.0)
(1.1666666666666665, 0.0)
(1.3333333333333333, 0.0)
(1.5, 0.0)
(1.6666666666666667, 0.0)
start
(0.0, 0.0)
(0.16666666666666666, 0.0)
(0.16666666666666666, 0.0)
(0.3333333333333333, 0.0)
(0.5, 0.0)
(0.5, 0.0)
(0.6666666666666666, 0.0)
(0.6666666666666666, 0.0)
(0.8333333333333333, 0.0)
(0.9999999999999999, 0.0)

the first test run on 1/10 second and end up with total time 1 second, if player moving 10 m/s then he should end up in 1 meter or so. but in second test simulation 1/100 second and end up with total time 0.1, if player moving 10 m/s then the should end less distance than first test.

from dyn4j.

fian46 avatar fian46 commented on May 10, 2024

if we increase the scale between delta let say first test 1 second and second test 0.1 second the result still wrong

from dyn4j.

wnbittle avatar wnbittle commented on May 10, 2024

I think the missing link here is that the World has its own internal clock. In the first case, 0.1 is larger than the default update frequency of 1/60. The update method in this case still only performs 1 iteration of the engine. The effect is that extra time continues to accumulate, but is never evaluated. There’s another version of the update method that allows you to specify the maximum number of iterations to perform per call.

from dyn4j.

fian46 avatar fian46 commented on May 10, 2024

can you give sample code for that update usage ? i think i am doing it wrong i am sorry. there no example in documentation-andvanced page please help me to pass the test

from dyn4j.

wnbittle avatar wnbittle commented on May 10, 2024

Have you read through the java docs for the world class, specifically the update methods?

I can post an example later.

from dyn4j.

wnbittle avatar wnbittle commented on May 10, 2024

The World class has an internal clock that determines when it performs a simulation step. The internal clock is defaulted to 1/60. This value is configurable via the world.getSettings().setStepFrequency(double) method.

Now, lets take your example of 0.01 and examine what it will do:

  • Iteration 0: elapsed time = 0.01, 0.01 < 1/60 so no simulation is performed, hence (0,0) coordinates.
  • Iteration 1: elapsed time = 0.02, 0.02 > 1/60 so a single simulation step is performed and the remaining time = 0.02 - 1/60 = 0.00333333333, coordinates = (0.16666666666666666, 0)
  • Iteration 2: elapsed time = 0.01333333333, 0.01333333333 < 1/60 so no simulation is performed, coordinates = (0.16666666666666666, 0)
  • Iteration 3: elapsed time = 0.02333333333, 0.02333333333 > 1/60 so a single simulation step is performed, coordinates = (0.3333333333333333, 0.0)
  • And so on...

So as you can see, the internal clock of the World determines when it actually does simulation calculations. If it hasn't been long enough, then nothing is done, the method immediately returns.

In the case of anything greater than 1/60, the World will perform a single simulation step, using up only 1/60 of the given elapsed time, with the rest stored in the internal time accumulator. If the elapsed time is consistently above 1/60, the simulation will never catch up - the slowness you are seeing.

Hopefully the problem is clearer now. The solution will depend on your use-case, but my guess is that you are looking for the update(double, int) method. The second parameter in the update method allows you to tell the engine the maximum number of steps to perform per call. By default its 1.

So changing the code to world.update(0.10, 10) should fix your problem:

  • Iteration 0: elapsed time = 0.1, 0.1 > 1/60, so we need to perform 6 steps to catch up, left over time = 0
  • Iteration 1: elapsed time = 0.1, 0.1 > 1/60, so we need to perform 6 steps to catch up, left over time = 0
  • And so on...

On each iteration we are actually performing 6 simulation steps each using 1/60 elapsed time for a total of 0.1 elapsed time.

The Javadoc has tons of information on these methods.

from dyn4j.

fian46 avatar fian46 commented on May 10, 2024

briliant it solve the problem πŸ‘πŸ‘πŸ‘πŸ‘πŸ‘. i going to close this issue. what a noob me. thanks for the assistance. i saw the forum is going to retire so i ask this question here is it ok ?

from dyn4j.

wnbittle avatar wnbittle commented on May 10, 2024

Yeah, feel free to ask any questions here.

from dyn4j.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.