Jump to content

Disable gravity on particles from emitter, how?


BdR
 Share

Recommended Posts

I'm working on a game with a tilemap and arcade physics. The gravity is set to 1000 for the player and enemies movements and interacting which all work fine.

However, when I add a particle emitter the particles also fall down? I've tried setting emitter.gravity to "0" but that doesn't help. The only thing that stops the particles from falling is disabling the line that sets game.physics.arcade.gravity.y, which is not what I want because the gravity is needed for the game.

So my question is: how to disable the gravity of emitter particles?

I've created a fiddle with example code here:
https://jsfiddle.net/pjjL7ecu/1/

Btw this is similar to this old thread which doesn't have a clear answer, as far as I can tell.

function create() {
	// set stage physics
	game.stage.backgroundColor = 0x808080;
	game.physics.startSystem(Phaser.Physics.ARCADE);
	game.physics.arcade.gravity.y = 1000;

	// add particle emmiter
	emitter = game.add.emitter(0, 0, 200); // x=0, y=0, maxParticles=200
	emitter.makeParticles('particles', [0, 1, 2, 3]);

	//emitter.setXSpeed(-120, +120);
	//emitter.setYSpeed(-120, +120);
	emitter.setXSpeed(0, 0); // testing; particles should not move at all
	emitter.setYSpeed(0, 0);
	
	emitter.setRotation(0, 0);
	emitter.gravity = 0; // no gravity on particles

	// test sparkles will still fall down
	emitter.x = CANVAS_WIDTH / 2;
	emitter.y = CANVAS_HEIGHT / 2;
	emitter.start(true, 2000, null, 10); // explode=true, lifespan=800, freq=null, quantity=10
}

function render() {
	var testparticle = emitter.getFirstExists();
	if (testparticle) game.debug.bodyInfo(testparticle, 10, 10);
}

 

phaser_physics_question.png

Link to comment
Share on other sites

Hi, emitter emits arcade bodies and setting gravity on emitter brings this gravity to every particle body. In update position is update with world.gravity + particle gravity. So, setting gravity to 0 on emitter is not enough as particle is still affected with world gravity.

 This works:

  // add a particle emmiter
  emitter = game.add.emitter(0, 0, 200); // x=0, y=0, maxParticles=200
  emitter.gravity = 0; // no gravity on particles? this doesn't work?
  emitter.makeParticles('particles', [0, 1, 2, 3]);

  emitter.forEach(function(particle) {
    particle.body.allowGravity = false;
  }, this);

 Just iterate throug all created particle bodies and disable gravity completely on them...

Link to comment
Share on other sites

6 hours ago, Tom Atom said:

 Just iterate through all created particle bodies and disable gravity completely on them...

Thanks, that worked :) I also found that the gravity is added up, i.e. it's world.gravity + particle gravity. So when I set the emitter.gravity to -1000 it also works. However I suspect that will waste CPU time on unneeded gravity calculations, so your solution with allowGravity=false is better.

Link to comment
Share on other sites

Yes, setting gravity to negative value of world gravity works, but you have to be careful: gravity for emitter is only one number - y gravity, while gravity for arcade body / world is defined as point (for both x and y). So, if you had game, where you played with gravity (changing its direction), your particles would start move in x direction unexpectibly :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...