Jump to content

Some help with P2 physics


3ddy
 Share

Recommended Posts

Hello,
It's my 2nd attempt to physics in game, first in P2. I want to create four lines in something like X shape and rotate it clockwise. Then I will be shooting some little balls and I would like to check if I hit my X obstacle - I do not want to destroy it, apply some gravity to it or more complex physics. On ball collision with my obstacle I just want to kill/destroy that ball. I don't want to move or destroy my obstacle.

I've enabled physics at the beginning and created two collision groups

game.physics.startSystem(Phaser.Physics.P2JS);
game.physics.p2.setImpactEvents(true);
this.ballCollisionGroup = game.physics.p2.createCollisionGroup();
this.obstacleCollisionGroup = game.physics.p2.createCollisionGroup();

I'm spawning my obstacles consisting of 4 lines

        var dot = game.add.sprite(700, 350, "dot");
		dot.anchor.setTo(0.5);
		this.obstacle.x = 700;
		this.obstacle.y = 350;
		var line = game.add.sprite(0, 0, "line");
		line.anchor.setTo(0.5, 0);
		this.obstacle.addChild(line);
		var line2 = game.add.sprite(0, 0, "line");
		line2.anchor.setTo(0.5, 0);
		line2.angle = 90;
		this.obstacle.addChild(line2);
		var line3 = game.add.sprite(0, 0, "line");
		line3.anchor.setTo(0.5, 0);
		line3.angle = -90;
		this.obstacle.addChild(line3);
		var line4 = game.add.sprite(0, 0, "line");
		line4.anchor.setTo(0.5, 0);
		line4.angle = 180;
		this.obstacle.addChild(line4);
		dot.bringToTop();
		
		this.obstacle.physicsBodyType = Phaser.Physics.P2JS;
		this.obstacle.enableBody = true;
		
		for (var i = 0; i < this.obstacle.children.length; i++) {
			game.physics.enable(this.obstacle.children[i], Phaser.Physics.P2JS);
            this.obstacle.children[i].body.angularVelocity = 30;
        }

and my ball

this.ball = game.add.sprite(this.activeClock.x, this.activeClock.y, "ball");
               this.ball.anchor.set(0.5);
               game.physics.enable(this.ball, Phaser.Physics.P2JS);
               this.ball.checkWorldBounds = true;
               this.ball.events.onOutOfBounds.add(function(){
                    game.state.start("Intro"); 
               });
               this.ballGroup.add(this.ball);
               this.ball.body.velocity = game.physics.arcade.velocityFromAngle(shootAngle, ballSpeed);
			   this.ball.body.setCollisionGroup(this.ballCollisionGroup);
               this.ball.body.collides([this.obstacleCollisionGroup], this.ballCollision, this);

Right now I'm stucked at spawning my obstacles, because they are spawned on top of eachother and they got hit at spawning and all fall down my game. How to make my obstacles not collide with themselves and apply no gravity to them? Previously I've tried using ARCADE physics but it didn't track the motion of my obstacles in collisions, so I had to change to P2. Thanks in advance for help

Link to comment
Share on other sites

In order to solve it by myself I created second project just for experimenting. Trying to found out the solution step by step, not by making mess in existing project.

How to make two objects not interact, not collide with itself? I guess I can't make them STATIC because I want them to rotate.

 

game.physics.startSystem(Phaser.Physics.P2JS);
		game.physics.p2.setImpactEvents(true);
		this.obstacle = this.add.sprite(700, 400, 'line');
		this.physics.p2.enable(this.obstacle);
		//this.obstacle.body.angularVelocity = 1;
		
		this.obstacle2 = this.add.sprite(700, 400, 'line');
		this.physics.p2.enable(this.obstacle2);
		this.obstacle2.body.angle = 90;
		//this.obstacle2.body.angularVelocity = 1;

Solved it by assigning different CollisionGroup on both objects - is there any other way to do it? If I would have 50 objects that would be...painful...

Link to comment
Share on other sites

I have solved my first post problem using high mass on my obstacle objects and setting unique CollisionGroup for eachother - is it the way it is normally done? Or there is some other not so complicated way?  Also, won't my obstacle move when hit multiple times if I set its mass to for example 10000 and hitting objects are of mass 1?

Link to comment
Share on other sites

The best way to make objects collide with certain objects and not with other objects is, as you say, to use collision groups.  You can reuse them, however, so add all your "obstacle" sprites to one group, and tell that group to collide with the ball group but not itself.

As for the second part, That's something I've been wondering myself.  I recently developed a game with P2 physics where "one-way" collisions would be extremely useful, and I solved it by making masses extremely large.  That caused some other physics bugginess that I really didn't like.  So I'm pretty stumped on that one, too.  

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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