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

Performance issues when rerunning same simulation #368

Closed
lukemunn opened this issue Feb 15, 2017 · 3 comments
Closed

Performance issues when rerunning same simulation #368

lukemunn opened this issue Feb 15, 2017 · 3 comments

Comments

@lukemunn
Copy link

Not sure if this is a bug - more likely something I'm not doing and should be. Basically I want to keep rerunning the same simulation over and over with different positions of objects.

The first time it runs, it runs perfectly, very smooth. After that it becomes more and more jumpy, essentially jumping from 'frame 1' of simulation to the end state, without showing any transitional frames.

I had a look at your demo.js to grab the reset code. Is there anything I'm missing here, or is performance when rerunning a simulation a known problem?

function removeBodies(arr) {
	for (var i=0;i<arr.length;i++) {
		var body = arr[i];
		console.log('removing body '+body);
		World.remove(engine.world, body);
	}
}

function resetSim() {
	
// reset vars
counter = 0;
timeScale = 1;
finished = false;
runslowdown = false;
numCasualties = 0;

// remove bodies
removeBodies(wallArray);
removeBodies(bodyArray);
removeBodies(particleArray);
World.remove(engine.world, boxA);

// clear arrays
bodyArray.length=0;
casualtyArray.length=0;
particleArray.length=0;

// clear world and engine
World.clear(engine.world);
Engine.clear(engine);

// stop render
Render.stop(render);
// remove events  
engine.events = {};

// reset id pool
Body._nextCollidingGroupId = 1;
Body._nextNonCollidingGroupId = -1;
Body._nextCategory = 0x0001;
Common._nextId = 0;
    
// reset random seed
Common._seed = 0;

setTimeout(startSim, 2000);
}
@liabru
Copy link
Owner

liabru commented Feb 16, 2017

Could it be that you do this:

// stop render
Render.stop(render);

Is there a reason you can't keep it running?

Also you may not want to do this:

// remove events  
engine.events = {};

Unless you are sure you always set up the events every time?

Depending on what you're doing, you may want to try Matter.Demo from matter-tools which has a built in reset button.

@lukemunn
Copy link
Author

Yes removing that Render.stop seems to have made a huge difference, thanks for the tip! :-)

Also, to tag something small on here, the timestamp doesn't seem to return anything when called on a Collision event. e.g.

function collisionStart(event) {
// this works perfectly
console.log(event.pairs.length);
// but this always returns undefined
console.log(event.timestamp)
}

@liabru
Copy link
Owner

liabru commented Feb 17, 2017

Hey, looks like the docs need to be updated since the timestamp is no longer passed. You should just use engine.timing.timestamp instead when you detect them.

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

No branches or pull requests

2 participants